Spaces:
Runtime error
Runtime error
File size: 25,119 Bytes
75120e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
<script lang="ts">
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
import { Block } from '@gradio/atoms';
import { StatusTracker } from '@gradio/statustracker';
import type { LoadingStatus } from "@gradio/statustracker";
import type { Gradio } from "@gradio/utils";
import fixWebmDuration from 'fix-webm-duration';
// Type definitions
interface MediaRecorderOptions {
mimeType?: string;
audioBitsPerSecond?: number;
videoBitsPerSecond?: number;
bitsPerSecond?: number;
}
interface MediaTrackConstraints {
displaySurface?: 'browser' | 'monitor' | 'window';
cursor?: 'always' | 'motion' | 'never';
}
// Type definitions
interface RecordingData {
video: string;
duration: number;
audio_enabled?: boolean;
status?: string;
orig_name?: string;
size?: number | null;
data?: string; // Base64 encoded data for Gradio
name?: string; // Alias for orig_name for Gradio compatibility
is_file?: boolean;
type?: string; // MIME type of the recording
}
interface Position {
x: number;
y: number;
}
// Event types for the component
type EventMap = {
'error': { message: string; error: string };
'recording-started': void;
'recording-stopped': RecordingData;
'record_stop': RecordingData;
'change': RecordingData;
'webcam-error': { message: string; error: string };
};
// Component props with proper types and defaults
export let gradio: Gradio<any>;
export let value: Partial<RecordingData> | null = null;
export const elem_id = ''; // Marked as const since it's not modified
export let elem_classes: string[] = [];
export let scale: number | null = null;
export let min_width: number | null = null;
export let visible = true;
export let interactive = true;
export let loading_status: LoadingStatus | null = null;
export let audio_enabled = false;
export let webcam_overlay = false;
export let webcam_position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' = 'bottom-right';
export let recording_format: 'webm' | 'mp4' | 'gif' = 'webm';
export let max_duration: number | null = null;
// Computed styles for the container
let containerStyle = '';
// Component methods interface
interface ComponentMethods {
startRecording: () => Promise<void>;
stopRecording: () => void;
togglePause: () => void;
cleanup: () => void;
}
// Component state with explicit types and initial values
let isPaused = false;
let isRecording = false;
let recordingTime = 0;
let recordingTimer: number | null = null;
let recordedChunks: Blob[] = [];
// Media streams and elements
let screenStream: MediaStream | null = null;
let webcamStream: MediaStream | null = null;
let combinedStream: MediaStream | null = null;
let canvas: HTMLCanvasElement | null = null;
let ctx: CanvasRenderingContext2D | null = null;
let animationFrameId: number | null = null;
let previewVideo: HTMLVideoElement | null = null;
let webcamVideo: HTMLVideoElement | null = null;
let recordingStartTime = 0;
let mediaRecorder: MediaRecorder | null = null;
// Internal video elements
let webcamVideoInternal: HTMLVideoElement | null = null;
let screenVideoInternal: HTMLVideoElement | null = null;
// Bind canvas element
function bindCanvas(node: HTMLCanvasElement) {
canvas = node;
if (canvas) {
const context = canvas.getContext('2d', { willReadFrequently: true });
if (context) {
ctx = context;
// Set canvas dimensions with null checks
const width = canvas.offsetWidth;
const height = canvas.offsetHeight;
if (width && height) {
canvas.width = width;
canvas.height = height;
}
}
}
return {
destroy() {
canvas = null;
ctx = null;
}
};
}
// Canvas binding is now handled by the bindCanvas function
// Configuration
const webcam_size = 200;
const webcam_border = 10;
const webcam_radius = '50%';
// Ensure max_duration has a default value if null
$: effectiveMaxDuration = max_duration ?? 0;
// Computed styles for the container
$: containerStyle = [
scale !== null ? `--scale: ${scale};` : '',
min_width !== null ? `min-width: ${min_width}px;` : ''
].filter(Boolean).join(' ');
onDestroy(() => {
if (isRecording) {
componentMethods.stopRecording();
}
componentMethods.cleanup();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
});
// Component state and props are already declared above
// Event dispatcher with proper typing
const dispatch = createEventDispatcher<EventMap>();
// Type guard for error handling
function isErrorWithMessage(error: unknown): error is Error {
return error instanceof Error;
}
// Component methods implementation
const componentMethods: ComponentMethods = {
startRecording: async (): Promise<void> => {
if (isRecording) return;
isRecording = true;
recordedChunks = [];
recordingTime = 0;
try {
// Composite screen and optional webcam overlay via hidden canvas
const screenStreamCapture = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false });
screenStream = screenStreamCapture;
// Assign to hidden video for composition
if (screenVideoInternal) {
screenVideoInternal.srcObject = screenStreamCapture;
await screenVideoInternal.play().catch(() => {});
}
let captureStream: MediaStream;
if (webcam_overlay && webcamVideoInternal && canvas && ctx) {
try {
webcamStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
webcamVideoInternal.srcObject = webcamStream;
await webcamVideoInternal.play().catch(() => {});
// Resize canvas to match screen video
canvas.width = screenVideoInternal!.videoWidth;
canvas.height = screenVideoInternal!.videoHeight;
const overlaySize = Math.min(canvas.width, canvas.height) / 4;
const posMap: Record<string, [number, number]> = {
'top-left': [10, 10],
'top-right': [canvas.width - overlaySize - 10, 10],
'bottom-left': [10, canvas.height - overlaySize - 10],
'bottom-right': [canvas.width - overlaySize - 10, canvas.height - overlaySize - 10]
};
const [ox, oy] = posMap[webcam_position];
function draw() {
ctx!.drawImage(screenVideoInternal!, 0, 0, canvas.width, canvas.height);
ctx!.drawImage(webcamVideoInternal!, ox, oy, overlaySize, overlaySize);
animationFrameId = requestAnimationFrame(draw);
}
draw();
const canvasStream = canvas.captureStream(30);
const audioTracks = audio_enabled
? (await navigator.mediaDevices.getUserMedia({ audio: true })).getAudioTracks()
: screenStreamCapture.getAudioTracks();
combinedStream = new MediaStream([...canvasStream.getVideoTracks(), ...audioTracks]);
captureStream = combinedStream;
} catch (err) {
console.warn('Webcam overlay failed, falling back to screen only', err);
captureStream = screenStreamCapture;
}
} else {
// No overlay: combine audio if enabled with screen
const audioTracks = audio_enabled
? (await navigator.mediaDevices.getUserMedia({ audio: true })).getAudioTracks()
: screenStreamCapture.getAudioTracks();
combinedStream = new MediaStream([...screenStreamCapture.getVideoTracks(), ...audioTracks]);
captureStream = combinedStream;
}
// Handle track ended event
screenStreamCapture.getVideoTracks()[0].onended = () => {
if (isRecording) {
componentMethods.stopRecording();
}
};
// Start recording
const options: MediaRecorderOptions = {
mimeType: recording_format === 'webm' ? 'video/webm;codecs=vp9' : 'video/mp4'
};
mediaRecorder = new MediaRecorder(captureStream, options);
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.onstop = handleRecordingStop;
mediaRecorder.start();
recordingStartTime = Date.now();
updateRecordingTime();
dispatch('recording-started');
} catch (error) {
isRecording = false;
if (isErrorWithMessage(error)) {
dispatch('error', {
message: 'Failed to start recording',
error: error.message
});
}
}
},
stopRecording: (): void => {
if (!isRecording || !mediaRecorder) return;
try {
mediaRecorder.stop();
isRecording = false;
// Stop all tracks
[screenStream, webcamStream, combinedStream].forEach(stream => {
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
});
if (recordingTimer) {
clearTimeout(recordingTimer);
recordingTimer = null;
}
const recordingData: RecordingData = {
video: '',
duration: recordingTime / 1000,
audio_enabled: audio_enabled,
status: 'completed'
};
dispatch('recording-stopped', recordingData);
dispatch('record_stop', recordingData);
dispatch('change', recordingData);
} catch (error) {
isRecording = false;
if (isErrorWithMessage(error)) {
dispatch('error', {
message: 'Error stopping recording',
error: error.message
});
}
}
},
togglePause: (): void => {
if (!mediaRecorder) return;
isPaused = !isPaused;
if (isPaused) {
mediaRecorder.pause();
if (recordingTimer) {
clearTimeout(recordingTimer);
recordingTimer = null;
}
} else {
mediaRecorder.resume();
updateRecordingTime();
}
if (isPaused) {
// Pause logic
} else {
// Resume logic
}
},
cleanup: (): void => {
// Stop all media streams
[screenStream, webcamStream, combinedStream].forEach(stream => {
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
});
// Clear media recorder
if (mediaRecorder) {
if (mediaRecorder.state !== 'inactive') {
mediaRecorder.stop();
}
mediaRecorder = null;
}
// Clear canvas
if (ctx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
// Reset state
isRecording = false;
isPaused = false;
recordingTime = 0;
recordedChunks = [];
// Clear timers
if (recordingTimer) {
clearInterval(recordingTimer);
recordingTimer = null;
}
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
}
};
// Handle data available event
function handleDataAvailable(event: BlobEvent): void {
if (event.data && event.data.size > 0) {
recordedChunks.push(event.data);
}
}
// Handle recording stop
function handleRecordingStop(): void {
if (recordedChunks.length === 0) {
console.warn('No recording data available');
return;
}
const mimeType = recording_format === 'webm' ? 'video/webm' : 'video/mp4';
const blob = new Blob(recordedChunks, { type: mimeType });
const url = URL.createObjectURL(blob);
console.log('Recording stopped. Blob size:', blob.size, 'bytes');
// Create a file reader to read the blob as base64
const reader = new FileReader();
reader.onload = (e) => {
const base64data = e.target?.result as string;
// Extract the base64 data (remove the data URL prefix)
const base64Content = base64data.split(',')[1];
const fileName = `recording_${Date.now()}.${recording_format}`;
// Dispatch event with recording data
const recordingData: RecordingData = {
video: url,
duration: recordingTime,
audio_enabled: audio_enabled,
status: 'completed',
size: blob.size > 0 ? blob.size : undefined,
orig_name: fileName,
name: fileName, // Alias for Gradio compatibility
is_file: true,
type: mimeType,
data: base64Content
};
console.log('Dispatching recording-stopped event');
dispatch('recording-stopped', recordingData);
dispatch('record_stop', recordingData);
dispatch('change', recordingData);
// Update the value prop to trigger re-render
value = { ...value, ...recordingData };
};
reader.onerror = (error) => {
console.error('Error reading blob:', error);
dispatch('error', {
message: 'Failed to process recording',
error: 'Could not read recording data'
});
};
// Read the blob as data URL
reader.readAsDataURL(blob);
}
// Update recording time
function updateRecordingTime(): void {
if (!isRecording) return;
recordingTime = Math.floor((Date.now() - recordingStartTime) / 1000);
// Check if max duration has been reached
if (max_duration !== null && max_duration > 0 && recordingTime >= max_duration) {
console.log('Max duration reached, stopping');
componentMethods.stopRecording();
return;
}
// Schedule the next update
recordingTimer = window.setTimeout(updateRecordingTime, 1000);
}
function stopTimer(): void {
if (recordingTimer) {
clearTimeout(recordingTimer);
recordingTimer = null;
}
}
// Format time as MM:SS
function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
// Format file size in human-readable format
function formatFileSize(bytes: number | string | null | undefined): string {
if (bytes === null || bytes === undefined) return '0 B';
const numBytes = Number(bytes);
if (isNaN(numBytes) || numBytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(numBytes) / Math.log(k));
return parseFloat((numBytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
</script>
<div class="screen-recorder-container {!visible ? 'invisible' : ''} {elem_classes.join(' ')}" style="{containerStyle}">
{#if loading_status}
<StatusTracker
autoscroll={gradio.autoscroll}
i18n={gradio.i18n}
{...loading_status}
/>
{/if}
<div class="screen-recorder">
<div class="controls">
{#if !isRecording}
<button
class="record-btn start"
on:click={componentMethods.startRecording}
disabled={!interactive}
>
<span class="recording-icon">β</span> Start Recording
</button>
{:else}
<button
class="record-btn stop"
on:click={componentMethods.stopRecording}
>
<span class="stop-icon">β </span> Stop Recording
</button>
<span class="recording-time">
{formatTime(recordingTime)}
</span>
{#if max_duration}
<span class="max-duration">/ {formatTime(max_duration)}</span>
{/if}
{/if}
</div>
<!-- Live Preview - Always show when recording -->
{#if isRecording}
<div class="preview-container">
<video
bind:this={previewVideo}
class="preview-video"
autoplay
muted
playsinline
aria-label="Live preview"
on:loadedmetadata={() => {
if (previewVideo) {
previewVideo.play().catch(console.warn);
}
}}
>
<track kind="captions" />
</video>
{#if webcam_overlay}
<video
bind:this={webcamVideo}
class="webcam-overlay {webcam_position}"
style="width: 200px; height: 200px;"
autoplay
muted
playsinline
aria-label="Webcam overlay"
>
<track kind="captions" />
</video>
{/if}
<div class="recording-indicator">
<span class="pulse">β</span> RECORDING
</div>
</div>
{/if}
{#if value?.video}
<div class="recording-preview" style="position: relative;">
{#if audio_enabled}
<div class="speaker-overlay">π</div>
{/if}
<video
src={value.video}
controls
class="preview-video"
aria-label="Recording preview"
on:loadedmetadata
on:loadeddata
on:error={(e) => console.error('Video error:', e)}
>
<track kind="captions" />
</video>
<div class="recording-info">
<div>Duration: {value.duration ? value.duration.toFixed(1) : '0.0'}s</div>
{#if value.size}
<div>Size: {formatFileSize(value.size)}</div>
{/if}
</div>
</div>
{/if}
<!-- Configuration Display -->
<div class="config-info">
<span>Audio: {audio_enabled ? 'π' : 'π'}</span>
<span>Format: {recording_format.toUpperCase()}</span>
{#if max_duration}
<span>Max: {formatTime(max_duration)}</span>
{/if}
</div>
<!-- Debug info -->
{#if value}
<div class="debug-info">
<small>Last recording: {value.orig_name} ({Math.round(value.size / 1024)}KB)</small>
</div>
{/if}
</div>
<video bind:this={screenVideoInternal} hidden muted playsinline style="display:none"></video>
{#if webcam_overlay}
<video bind:this={webcamVideoInternal} hidden muted playsinline style="display:none"></video>
{/if}
<canvas bind:this={canvas} use:bindCanvas hidden style="display:none"></canvas>
</div>
<style>
.screen-recorder-container {
display: block;
width: 100%;
box-sizing: border-box;
}
.screen-recorder-container.invisible {
display: none;
}
.screen-recorder {
border: 2px solid #e0e0e0;
border-radius: 8px;
padding: 16px;
background: #f9f9f9;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.controls {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 12px;
flex-wrap: wrap;
}
.record-btn {
padding: 10px 20px;
border: none;
border-radius: 6px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.record-btn.start {
background: #4CAF50;
color: white;
}
.record-btn.start:hover {
background: #45a049;
}
.record-btn.stop {
background: #f44336;
color: white;
}
.record-btn.stop:hover {
background: #da190b;
}
.record-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.recording-time {
font-family: 'Courier New', monospace;
font-size: 18px;
font-weight: bold;
color: #f44336;
}
.max-duration {
font-family: 'Courier New', monospace;
font-size: 14px;
color: #666;
}
.preview-container {
position: relative;
margin: 12px 0;
border-radius: 6px;
overflow: hidden;
background: black;
min-height: 200px;
}
.preview-video {
width: 100%;
max-height: 400px;
display: block;
object-fit: contain;
}
.recording-indicator {
position: absolute;
top: 10px;
left: 10px;
background: rgba(244, 67, 54, 0.9);
color: white;
padding: 6px 12px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
animation: pulse 1s infinite;
box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.config-info {
display: flex;
gap: 8px;
font-size: 12px;
color: #666;
margin-top: 8px;
flex-wrap: wrap;
}
.config-info span {
padding: 4px 8px;
background: #e8e8e8;
border-radius: 4px;
border: 1px solid #ddd;
}
.debug-info {
margin-top: 8px;
padding: 8px;
background: #e8f5e8;
border-radius: 4px;
border: 1px solid #c8e6c8;
}
.speaker-overlay {
position: absolute;
top: 8px;
right: 8px;
background: rgba(0,0,0,0.5);
color: white;
padding: 4px;
border-radius: 4px;
font-size: 14px;
pointer-events: none;
}
</style>
|