{ "id": "video-slider", "name": "Video Slider", "description": "A high-performance video comparison component for Gradio apps. It allows users to compare two videos side-by-side with a draggable slider, featuring frame-accurate synchronization and built-in audio/fullscreen controls.", "author": "elismasilva", "tags": [ "video", "video-comparison", "slider" ], "html_template": "\n
\n
\n
\n \n
\n
\ud83c\udf9e\ufe0f
\n
Video A (Original)
\n
\n
Video Loaded
\n
\n
\n \n
\n
\ud83e\ude84
\n
Video B (Processed)
\n
\n
Video Loaded
\n
\n
\n\n
\n \n \n
\n
\n \n \n
\n
\u25b6
\n
\n
\n ", "css_template": "\n .vs-container {\n width: 100%;\n font-family: 'Inter', sans-serif;\n min-height: 50px;\n }\n\n .vs-upload-grid {\n display: grid;\n grid-template-columns: 1fr 1fr;\n gap: 15px;\n margin-bottom: 15px;\n }\n\n .vs-box {\n border: 2px dashed var(--border-color-primary);\n border-radius: 16px;\n height: 110px;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n background: var(--background-fill-secondary);\n cursor: pointer;\n transition: all 0.3s ease;\n }\n\n .vs-box:hover {\n border-color: var(--color-accent);\n background: var(--background-fill-primary);\n transform: translateY(-2px);\n }\n\n .vs-def,\n .vs-ok {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n gap: 10px;\n width: 100%;\n height: 100%;\n }\n\n .vs-icon-placeholder {\n font-size: 1.2rem;\n line-height: 1;\n }\n\n .vs-label-text {\n font-size: 1.05rem;\n font-weight: 500;\n color: var(--body-text-color);\n }\n\n .vs-box.has-v {\n border: 2.5px solid #10b981;\n background: rgba(16, 185, 129, 0.08);\n }\n\n .vs-box.has-v .vs-def {\n display: none;\n }\n\n .vs-ok {\n display: none;\n }\n\n .vs-box.has-v .vs-ok {\n display: flex;\n }\n\n .vs-player {\n position: relative;\n width: 100%;\n background: #000;\n border-radius: 12px;\n overflow: hidden;\n cursor: ew-resize;\n border: 1px solid #444;\n }\n\n .vs-player video {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: contain;\n }\n\n .v-top {\n z-index: 2;\n }\n\n .v-handle {\n position: absolute;\n top: 0;\n bottom: 0;\n width: 4px;\n margin-left: -2px;\n background: var(--color-accent);\n z-index: 10;\n pointer-events: none;\n }\n\n .v-handle::after {\n content: '\u2194';\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 44px;\n height: 44px;\n background: var(--color-accent);\n border-radius: 50%;\n border: 3px solid white;\n color: white;\n display: flex;\n align-items: center;\n justify-content: center;\n font-weight: bold;\n font-size: 20px;\n box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);\n }\n\n .v-btns {\n position: absolute;\n top: 15px;\n right: 15px;\n z-index: 30;\n display: flex;\n gap: 8px;\n }\n\n .vs-btn,\n .v-btn {\n background: #111 !important;\n border: 2px solid #444;\n border-radius: 10px;\n width: 48px;\n height: 48px;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .v-btn svg,\n .vs-btn svg {\n width: 26px;\n height: 26px;\n color: white !important;\n stroke: white !important;\n stroke-width: 2.5px !important;\n }\n\n .v-play-overlay {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n display: flex;\n align-items: center;\n justify-content: center;\n background: rgba(0, 0, 0, 0.3);\n z-index: 20;\n color: white;\n font-size: 60px;\n pointer-events: none;\n opacity: 1;\n transition: 0.2s;\n }\n\n .vs-player.playing .v-play-overlay {\n opacity: 0;\n }\n ", "js_on_load": "\n const c = element.querySelector('.vs-player');\n if (!c) return;\n const v1 = c.querySelector('.v-base');\n const v2 = c.querySelector('.v-top');\n const h = c.querySelector('.v-handle');\n const bxs = element.querySelectorAll('.vs-box');\n \n const iVol = ``;\n const iMute = ``;\n const iMax = ``;\n const iMin = ``;\n \n const applyVideos = (data) => {\n if (data && data.length === 2 && data[0] && data[1]) {\n v1.src = data[0]; \n v2.src = data[1];\n c.style.display = 'block';\n bxs.forEach(b => b.classList.add('has-v'));\n }\n };\n\n const observer = new MutationObserver(() => {\n try {\n const newValue = JSON.parse(c.getAttribute('data-value'));\n applyVideos(newValue);\n } catch(e) {}\n });\n observer.observe(c, { attributes: true, attributeFilter: ['data-value'] });\n \n applyVideos(props.value);\n\n const iL = element.querySelector('#in-l');\n const iR = element.querySelector('#in-r');\n if (iL && iR) {\n const up = (file, idx) => {\n const r = new FileReader();\n r.onload = (e) => {\n const b64 = e.target.result;\n if(idx===0) v1.src = b64; else v2.src = b64;\n bxs[idx].classList.add('has-v');\n if(v1.src && v2.src) {\n c.style.display = 'block';\n props.value = [v1.src, v2.src];\n trigger('change');\n }\n };\n r.readAsDataURL(file);\n };\n iL.onchange = e => up(e.target.files[0], 0);\n iR.onchange = e => up(e.target.files[0], 1);\n bxs[0].onclick = () => iL.click();\n bxs[1].onclick = () => iR.click();\n }\n \n const bM = element.querySelector('#btn-mute');\n bM.onclick = e => { e.stopPropagation(); v1.muted = !v1.muted; bM.innerHTML = v1.muted ? iMute : iVol; };\n const bF = element.querySelector('#btn-fs');\n bF.onclick = e => { e.stopPropagation(); if(!document.fullscreenElement) c.requestFullscreen(); else document.exitFullscreen(); };\n document.addEventListener('fullscreenchange', () => { bF.innerHTML = document.fullscreenElement ? iMin : iMax; });\n\n function refresh(p) {\n p = Math.max(0, Math.min(100, p));\n h.style.left = p + \"%\";\n v2.style.clipPath = `polygon(${p}% 0, 100% 0, 100% 100%, ${p}% 100%)`;\n }\n refresh(50);\n\n c.onmousemove = e => { if(e.buttons===1) refresh(((e.clientX - c.getBoundingClientRect().left)/c.offsetWidth)*100); };\n c.onmousedown = e => { if(!e.target.closest('button')) refresh(((e.clientX - c.getBoundingClientRect().left)/c.offsetWidth)*100); };\n c.onclick = e => { if(e.target.closest('button')) return; if(v1.paused) v1.play(); else v1.pause(); };\n v1.onplay = () => { v2.currentTime = v1.currentTime; v2.play(); c.classList.add('playing'); };\n v1.onpause = () => { v2.pause(); c.classList.remove('playing'); };\n v1.ontimeupdate = () => { if(Math.abs(v1.currentTime - v2.currentTime) > 0.2) v2.currentTime = v1.currentTime; };\n ", "default_props": { "value": "" }, "python_code": "class VideoSlider(gr.HTML):\n EVENTS = [\"change\"]\n\n def __init__(\n self,\n value: Optional[Tuple[str, str]] = None,\n height: int = 400,\n interactive: bool = True,\n label: str = None,\n **kwargs\n ):\n self.height = height\n self.interactive = interactive\n self.label = label\n\n # Icons\n icons = {\n \"success\": '',\n \"mute\": '',\n \"volume\": '',\n \"maximize\": '',\n \"minimize\": ''\n }\n\n up_display = \"grid\" if self.interactive else \"none\"\n\n html_template = f\"\"\"\n
\n
\n
\n \n
\n
\ud83c\udf9e\ufe0f
\n
Video A (Original)
\n
\n
{icons['success']}
Video Loaded
\n
\n
\n \n
\n
\ud83e\ude84
\n
Video B (Processed)
\n
\n
{icons['success']}
Video Loaded
\n
\n
\n\n
\n \n \n
\n
\n \n \n
\n
\u25b6
\n
\n
\n \"\"\"\n\n css_template = \"\"\"\n .vs-container {\n width: 100%;\n font-family: 'Inter', sans-serif;\n min-height: 50px;\n }\n\n .vs-upload-grid {\n display: grid;\n grid-template-columns: 1fr 1fr;\n gap: 15px;\n margin-bottom: 15px;\n }\n\n .vs-box {\n border: 2px dashed var(--border-color-primary);\n border-radius: 16px;\n height: 110px;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n background: var(--background-fill-secondary);\n cursor: pointer;\n transition: all 0.3s ease;\n }\n\n .vs-box:hover {\n border-color: var(--color-accent);\n background: var(--background-fill-primary);\n transform: translateY(-2px);\n }\n\n .vs-def,\n .vs-ok {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n gap: 10px;\n width: 100%;\n height: 100%;\n }\n\n .vs-icon-placeholder {\n font-size: 1.2rem;\n line-height: 1;\n }\n\n .vs-label-text {\n font-size: 1.05rem;\n font-weight: 500;\n color: var(--body-text-color);\n }\n\n .vs-box.has-v {\n border: 2.5px solid #10b981;\n background: rgba(16, 185, 129, 0.08);\n }\n\n .vs-box.has-v .vs-def {\n display: none;\n }\n\n .vs-ok {\n display: none;\n }\n\n .vs-box.has-v .vs-ok {\n display: flex;\n }\n\n .vs-player {\n position: relative;\n width: 100%;\n background: #000;\n border-radius: 12px;\n overflow: hidden;\n cursor: ew-resize;\n border: 1px solid #444;\n }\n\n .vs-player video {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: contain;\n }\n\n .v-top {\n z-index: 2;\n }\n\n .v-handle {\n position: absolute;\n top: 0;\n bottom: 0;\n width: 4px;\n margin-left: -2px;\n background: var(--color-accent);\n z-index: 10;\n pointer-events: none;\n }\n\n .v-handle::after {\n content: '\u2194';\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 44px;\n height: 44px;\n background: var(--color-accent);\n border-radius: 50%;\n border: 3px solid white;\n color: white;\n display: flex;\n align-items: center;\n justify-content: center;\n font-weight: bold;\n font-size: 20px;\n box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);\n }\n\n .v-btns {\n position: absolute;\n top: 15px;\n right: 15px;\n z-index: 30;\n display: flex;\n gap: 8px;\n }\n\n .vs-btn,\n .v-btn {\n background: #111 !important;\n border: 2px solid #444;\n border-radius: 10px;\n width: 48px;\n height: 48px;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .v-btn svg,\n .vs-btn svg {\n width: 26px;\n height: 26px;\n color: white !important;\n stroke: white !important;\n stroke-width: 2.5px !important;\n }\n\n .v-play-overlay {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n display: flex;\n align-items: center;\n justify-content: center;\n background: rgba(0, 0, 0, 0.3);\n z-index: 20;\n color: white;\n font-size: 60px;\n pointer-events: none;\n opacity: 1;\n transition: 0.2s;\n }\n\n .vs-player.playing .v-play-overlay {\n opacity: 0;\n }\n \"\"\"\n\n js_on_load = f\"\"\"\n const c = element.querySelector('.vs-player');\n if (!c) return;\n const v1 = c.querySelector('.v-base');\n const v2 = c.querySelector('.v-top');\n const h = c.querySelector('.v-handle');\n const bxs = element.querySelectorAll('.vs-box');\n\n const iVol = `{icons['volume']}`;\n const iMute = `{icons['mute']}`;\n const iMax = `{icons['maximize']}`;\n const iMin = `{icons['minimize']}`;\n\n const applyVideos = (data) => {{\n if (data && data.length === 2 && data[0] && data[1]) {{\n v1.src = data[0]; \n v2.src = data[1];\n c.style.display = 'block';\n bxs.forEach(b => b.classList.add('has-v'));\n }}\n }};\n\n const observer = new MutationObserver(() => {{\n try {{\n const newValue = JSON.parse(c.getAttribute('data-value'));\n applyVideos(newValue);\n }} catch(e) {{}}\n }});\n observer.observe(c, {{ attributes: true, attributeFilter: ['data-value'] }});\n\n applyVideos(props.value);\n\n const iL = element.querySelector('#in-l');\n const iR = element.querySelector('#in-r');\n if (iL && iR) {{\n const up = (file, idx) => {{\n const r = new FileReader();\n r.onload = (e) => {{\n const b64 = e.target.result;\n if(idx===0) v1.src = b64; else v2.src = b64;\n bxs[idx].classList.add('has-v');\n if(v1.src && v2.src) {{\n c.style.display = 'block';\n props.value = [v1.src, v2.src];\n trigger('change');\n }}\n }};\n r.readAsDataURL(file);\n }};\n iL.onchange = e => up(e.target.files[0], 0);\n iR.onchange = e => up(e.target.files[0], 1);\n bxs[0].onclick = () => iL.click();\n bxs[1].onclick = () => iR.click();\n }}\n\n const bM = element.querySelector('#btn-mute');\n bM.onclick = e => {{ e.stopPropagation(); v1.muted = !v1.muted; bM.innerHTML = v1.muted ? iMute : iVol; }};\n const bF = element.querySelector('#btn-fs');\n bF.onclick = e => {{ e.stopPropagation(); if(!document.fullscreenElement) c.requestFullscreen(); else document.exitFullscreen(); }};\n document.addEventListener('fullscreenchange', () => {{ bF.innerHTML = document.fullscreenElement ? iMin : iMax; }});\n\n function refresh(p) {{\n p = Math.max(0, Math.min(100, p));\n h.style.left = p + \"%\";\n v2.style.clipPath = `polygon(${{p}}% 0, 100% 0, 100% 100%, ${{p}}% 100%)`;\n }}\n refresh(50);\n\n c.onmousemove = e => {{ if(e.buttons===1) refresh(((e.clientX - c.getBoundingClientRect().left)/c.offsetWidth)*100); }};\n c.onmousedown = e => {{ if(!e.target.closest('button')) refresh(((e.clientX - c.getBoundingClientRect().left)/c.offsetWidth)*100); }};\n c.onclick = e => {{ if(e.target.closest('button')) return; if(v1.paused) v1.play(); else v1.pause(); }};\n v1.onplay = () => {{ v2.currentTime = v1.currentTime; v2.play(); c.classList.add('playing'); }};\n v1.onpause = () => {{ v2.pause(); c.classList.remove('playing'); }};\n v1.ontimeupdate = () => {{ if(Math.abs(v1.currentTime - v2.currentTime) > 0.2) v2.currentTime = v1.currentTime; }};\n \"\"\"\n\n super().__init__(\n value=value,\n html_template=html_template,\n css_template=css_template,\n js_on_load=js_on_load,\n **kwargs\n )\n\n def _encode(self, path):\n if not path or not os.path.exists(path): return None\n mime, _ = mimetypes.guess_type(path)\n with open(path, \"rb\") as f:\n b64 = base64.b64encode(f.read()).decode()\n return f\"data:{mime or 'video/mp4'};base64,{b64}\"\n\n def postprocess(self, value):\n if value and isinstance(value, (list, tuple)) and len(value) == 2:\n return [self._encode(v) for v in value]\n return value\n\n def api_info(self): return {\"type\": \"string\"}\n", "head": "", "repo_url": "https://huggingface.co/spaces/elismasilva/gradio_video_slider" }