```javascript // ========================================== // 1. DOM Element References & State // ========================================== const dropzone = document.getElementById('dropzone'); const videoInput = document.getElementById('videoInput'); const cropWrapper = document.getElementById('cropWrapper'); const cropCanvas = document.getElementById('cropCanvas'); const ctx = cropCanvas.getContext('2d'); const coordX = document.getElementById('coordX'); const coordY = document.getElementById('coordY'); const coordW = document.getElementById('coordW'); const coordH = document.getElementById('coordH'); const removalMode = document.getElementById('removalMode'); const processBtn = document.getElementById('processBtn'); const progressBox = document.getElementById('progressBox'); const progressBar = document.getElementById('progressBar'); const statusText = document.getElementById('statusText'); const percentText = document.getElementById('percentText'); const originalVideo = document.getElementById('originalVideo'); const processedVideo = document.getElementById('processedVideo'); const downloadVideoLink = document.getElementById('downloadVideoLink'); const vttTrack = document.getElementById('vttTrack'); const vttOutput = document.getElementById('vttOutput'); const copyVttBtn = document.getElementById('copyVttBtn'); const downloadVttBtn = document.getElementById('downloadVttBtn'); const hiddenCanvas = document.getElementById('hiddenRenderCanvas'); const hiddenCtx = hiddenCanvas.getContext('2d', { willReadFrequently: true }); let selectedFile = null; let isDrawing = false; let startX = 0, startY = 0; let rect = { x: 0, y: 0, w: 0, h: 0 }; let previewFrame = document.createElement('video'); let realCaptions = []; // ========================================== // 2. File Upload & Frame Preview // ========================================== dropzone.addEventListener('click', () => videoInput.click()); videoInput.addEventListener('change', (e) => { if (e.target.files.length) { handleFile(e.target.files[0]); } }); function handleFile(file) { if (!file.type.startsWith('video/')) { alert('Please select a valid video file.'); return; } selectedFile = file; const url = URL.createObjectURL(file); originalVideo.src = url; processBtn.disabled = false; previewFrame.src = url; previewFrame.currentTime = 0.5; previewFrame.onloadeddata = () => { cropCanvas.width = previewFrame.videoWidth; cropCanvas.height = previewFrame.videoHeight; cropWrapper.style.display = 'flex'; redrawCanvas(); }; } // ========================================== // 3. Interactive Cropper Canvas // ========================================== function redrawCanvas() { ctx.drawImage(previewFrame, 0, 0, cropCanvas.width, cropCanvas.height); if (rect.w > 0 && rect.h > 0) { ctx.strokeStyle = '#38bdf8'; ctx.lineWidth = 3; ctx.strokeRect(rect.x, rect.y, rect.w, rect.h); ctx.fillStyle = 'rgba(56, 189, 248, 0.3)'; ctx.fillRect(rect.x, rect.y, rect.w, rect.h); } } cropCanvas.addEventListener('mousedown', (e) => { const bounds = cropCanvas.getBoundingClientRect(); startX = (e.clientX - bounds.left) * (cropCanvas.width / bounds.width); startY = (e.clientY - bounds.top) * (cropCanvas.height / bounds.height); isDrawing = true; }); cropCanvas.addEventListener('mousemove', (e) => { if (!isDrawing) return; const bounds = cropCanvas.getBoundingClientRect(); const currentX = (e.clientX - bounds.left) * (cropCanvas.width / bounds.width); const currentY = (e.clientY - bounds.top) * (cropCanvas.height / bounds.height); rect.x = Math.max(0, Math.floor(Math.min(startX, currentX))); rect.y = Math.max(0, Math.floor(Math.min(startY, currentY))); rect.w = Math.floor(Math.abs(currentX - startX)); rect.h = Math.floor(Math.abs(currentY - startY)); coordX.value = rect.x; coordY.value = rect.y; coordW.value = rect.w; coordH.value = rect.h; redrawCanvas(); }); window.addEventListener('mouseup', () => { isDrawing = false; }); [coordX, coordY, coordW, coordH].forEach((input) => { input.addEventListener('input', () => { rect.x = parseInt(coordX.value) || 0; rect.y = parseInt(coordY.value) || 0; rect.w = parseInt(coordW.value) || 0; rect.h = parseInt(coordH.value) || 0; redrawCanvas(); }); }); // ========================================== // 4. Progress Updater // ========================================== function setProgress(status, percent) { statusText.textContent = status; percentText.textContent = `${percent}%`; progressBar.style.width = `${percent}%`; } // ========================================== // 5. Video Processing & Speech Recognition // ========================================== processBtn.addEventListener('click', async () => { if (!selectedFile) return; processBtn.disabled = true; progressBox.style.display = 'block'; setProgress('Initializing Delogo & Speech Recognition...', 5); realCaptions = []; const renderVideo = document.createElement('video'); renderVideo.src = URL.createObjectURL(selectedFile); renderVideo.muted = false; await new Promise((r) => (renderVideo.onloadedmetadata = r)); const vw = renderVideo.videoWidth; const vh = renderVideo.videoHeight; const duration = renderVideo.duration || 10; hiddenCanvas.width = vw; hiddenCanvas.height = vh; // 1. Initialize Web Speech Recognition const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; let recognition = null; let lastSpeechTime = 0; if (SpeechRecognition) { recognition = new SpeechRecognition(); recognition.continuous = true; recognition.interimResults = false; recognition.lang = 'en-US'; recognition.onresult = (event) => { for (let i = event.resultIndex; i < event.results.length; ++i) { if (event.results[i].isFinal) { const text = event.results[i][0].transcript.trim(); const startTime = lastSpeechTime; const endTime = renderVideo.currentTime; lastSpeechTime = endTime; if (text.length > 0) { realCaptions.push({ start: startTime, end: Math.max(endTime, startTime + 1.5), text: text }); } } } }; try { recognition.start(); } catch (e) { console.warn(e); } } // 2. Setup Canvas Stream & Audio Processing const canvasStream = hiddenCanvas.captureStream(30); const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); const source = audioCtx.createMediaElementSource(renderVideo); const destination = audioCtx.createMediaStreamDestination(); source.connect(destination); source.connect(audioCtx.destination); if (destination.stream.getAudioTracks().length > 0) { canvasStream.addTrack(destination.stream.getAudioTracks()[0]); } let mimeType = 'video/webm;codecs=vp8,opus'; if (!MediaRecorder.isTypeSupported(mimeType)) { mimeType = 'video/webm'; } const recorder = new MediaRecorder(canvasStream, { mimeType, videoBitsPerSecond: 3000000 }); const chunks = []; recorder.ondataavailable = (e) => { if (e.data.size > 0) chunks.push(e.data); }; recorder.onstop = () => { if (recognition) { try { recognition.stop(); } catch (e) {} } const blob = new Blob(chunks, { type: 'video/webm' }); const videoUrl = URL.createObjectURL(blob); processedVideo.src = videoUrl; downloadVideoLink.href = videoUrl; downloadVideoLink.download = 'delogo_processed.webm'; downloadVideoLink.style.display = 'block'; downloadVideoLink.textContent = 'Download Processed Video (.webm)'; // Generate WebVTT string const vttString = formatWebVTT(realCaptions, duration); vttOutput.value = vttString; const vttBlob = new Blob([vttString], { type: 'text/vtt' }); vttTrack.src = URL.createObjectURL(vttBlob); setProgress('Processing Complete!', 100); processBtn.disabled = false; }; recorder.start(); await renderVideo.play(); const x = parseInt(coordX.value) || 0; const y = parseInt(coordY.value) || 0; const w = Math.max(1, parseInt(coordW.value) || 10); const h = Math.max(1, parseInt(coordH.value) || 10); const mode = removalMode.value; function renderFrame() { if (renderVideo.paused || renderVideo.ended) { if (renderVideo.ended) recorder.stop(); return; } hiddenCtx.drawImage(renderVideo, 0, 0, vw, vh); if (w > 0 && h > 0) { if (mode === 'blur') { hiddenCtx.save(); hiddenCtx.filter = 'blur(10px)'; hiddenCtx.drawImage(renderVideo, x, y, w, h, x, y, w, h); hiddenCtx.restore(); } else if (mode === 'pixelate') { hiddenCtx.imageSmoothingEnabled = false; hiddenCtx.drawImage(renderVideo, x, y, w, h, x, y, Math.max(2, w / 8), Math.max(2, h / 8)); hiddenCtx.drawImage(hiddenCanvas, x, y, Math.max(2, w / 8), Math.max(2, h / 8), x, y, w, h); hiddenCtx.imageSmoothingEnabled = true; } else if (mode === 'black') { hiddenCtx.fillStyle = '#000000'; hiddenCtx.fillRect(x, y, w, h); } } const pct = Math.min(90, Math.floor((renderVideo.currentTime / duration) * 85) + 5); setProgress(`Processing frame: ${Math.floor(renderVideo.currentTime)}s / ${Math.floor(duration)}s`, pct); requestAnimationFrame(renderFrame); } renderFrame(); }); // ========================================== // 6. WebVTT Formatting & Timestamps // ========================================== function formatWebVTT(captions, totalDuration) { let vtt = "WEBVTT\n\n"; if (!captions || captions.length === 0) { vtt += `1\n00:00:00.000 --> ${formatTime(totalDuration)}\n[No speech detected or microphone permission needed]\n\n`; return vtt; } captions.forEach((cap, index) => { vtt += `${index + 1}\n`; vtt += `${formatTime(cap.start)} --> ${formatTime(cap.end)}\n`; vtt += `${cap.text}\n\n`; }); return vtt; } function formatTime(s) { const pad = (n, z = 2) => String(Math.floor(n)).padStart(z, '0'); return `${pad(s / 3600)}:${pad((s % 3600) / 60)}:${pad(s % 60)}.${String(Math.floor((s % 1) * 1000)).padStart(3, '0')}`; } // ========================================== // 7. Clipboard & Download Helpers // ========================================== copyVttBtn.addEventListener('click', () => { navigator.clipboard.writeText(vttOutput.value); copyVttBtn.textContent = 'Copied!'; setTimeout(() => (copyVttBtn.textContent = 'Copy WebVTT'), 1500); }); downloadVttBtn.addEventListener('click', () => { const blob = new Blob([vttOutput.value], { type: 'text/vtt' }); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'captions.vtt'; a.click(); }); ```