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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# A Gradio Space that (1) extracts frames with FFmpeg and (2) upscales each frame with Real-ESRGAN.
|
| 3 |
+
# - CPU-friendly (works without GPU). GPU will be faster if available.
|
| 4 |
+
# - Choose extraction mode, then choose upscale model/scale, JPG quality, and get a ZIP of upscaled frames.
|
| 5 |
+
# - Shows both the FFmpeg command and the ESRGAN settings used.
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import io
|
| 9 |
+
import re
|
| 10 |
+
import math
|
| 11 |
+
import json
|
| 12 |
+
import time
|
| 13 |
+
import shutil
|
| 14 |
+
import zipfile
|
| 15 |
+
import tempfile
|
| 16 |
+
import subprocess
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from typing import List, Optional
|
| 19 |
+
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# ─────────────────────────────────────────────────────────────
|
| 23 |
+
# FFmpeg detection
|
| 24 |
+
# ─────────────────────────────────────────────────────────────
|
| 25 |
+
|
| 26 |
+
def _which(name: str) -> Optional[str]:
|
| 27 |
+
from shutil import which
|
| 28 |
+
return which(name)
|
| 29 |
+
|
| 30 |
+
FFMPEG = _which("ffmpeg")
|
| 31 |
+
FFPROBE = _which("ffprobe")
|
| 32 |
+
|
| 33 |
+
if not FFMPEG or not FFPROBE:
|
| 34 |
+
MISSING_MSG = (
|
| 35 |
+
"⚠️ FFmpeg not found. Add a 'packages.txt' with just: \nffmpeg\nlibsm6\nlibxext6\nThen restart the Space."
|
| 36 |
+
)
|
| 37 |
+
else:
|
| 38 |
+
MISSING_MSG = ""
|
| 39 |
+
|
| 40 |
+
# ─────────────────────────────────────────────────────────────
|
| 41 |
+
# ffprobe helpers
|
| 42 |
+
# ─────────────────────────────────────────────────────────────
|
| 43 |
+
|
| 44 |
+
def ffprobe_json(input_path: str) -> dict:
|
| 45 |
+
if not FFPROBE:
|
| 46 |
+
return {}
|
| 47 |
+
cmd = [FFPROBE, "-v", "error", "-print_format", "json", "-show_streams", "-show_format", input_path]
|
| 48 |
+
res = subprocess.run(cmd, capture_output=True, text=True)
|
| 49 |
+
if res.returncode != 0:
|
| 50 |
+
return {}
|
| 51 |
+
try:
|
| 52 |
+
return json.loads(res.stdout)
|
| 53 |
+
except Exception:
|
| 54 |
+
return {}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def parse_video_info(meta: dict) -> dict:
|
| 58 |
+
info = {"duration": None, "fps": None, "width": None, "height": None}
|
| 59 |
+
if not meta:
|
| 60 |
+
return info
|
| 61 |
+
try:
|
| 62 |
+
info["duration"] = float(meta.get("format", {}).get("duration", None))
|
| 63 |
+
except Exception:
|
| 64 |
+
pass
|
| 65 |
+
vstreams = [s for s in meta.get("streams", []) if s.get("codec_type") == "video"]
|
| 66 |
+
if vstreams:
|
| 67 |
+
v = vstreams[0]
|
| 68 |
+
rfr = v.get("r_frame_rate") or v.get("avg_frame_rate")
|
| 69 |
+
if rfr and "/" in rfr:
|
| 70 |
+
num, den = rfr.split("/")
|
| 71 |
+
try:
|
| 72 |
+
num = float(num)
|
| 73 |
+
den = float(den)
|
| 74 |
+
if den != 0:
|
| 75 |
+
info["fps"] = num / den
|
| 76 |
+
except Exception:
|
| 77 |
+
pass
|
| 78 |
+
info["width"] = v.get("width")
|
| 79 |
+
info["height"] = v.get("height")
|
| 80 |
+
return info
|
| 81 |
+
|
| 82 |
+
# ─────────────────────────────────────────────────────────────
|
| 83 |
+
# FFmpeg command builder
|
| 84 |
+
# ─────────────────────────────────────────────────────────────
|
| 85 |
+
|
| 86 |
+
def build_ffmpeg_command(
|
| 87 |
+
input_path: str,
|
| 88 |
+
mode: str,
|
| 89 |
+
every_seconds: float,
|
| 90 |
+
nth_frame: int,
|
| 91 |
+
exact_fps: float,
|
| 92 |
+
start_time: str,
|
| 93 |
+
end_time: str,
|
| 94 |
+
long_side: int,
|
| 95 |
+
out_format: str,
|
| 96 |
+
jpg_quality: int,
|
| 97 |
+
png_level: int,
|
| 98 |
+
scene_detect: bool,
|
| 99 |
+
scene_thresh: float,
|
| 100 |
+
out_pattern: str,
|
| 101 |
+
) -> List[str]:
|
| 102 |
+
if not FFMPEG:
|
| 103 |
+
raise RuntimeError("FFmpeg not available")
|
| 104 |
+
|
| 105 |
+
cmd = [FFMPEG, "-y"]
|
| 106 |
+
if start_time:
|
| 107 |
+
cmd += ["-ss", start_time]
|
| 108 |
+
cmd += ["-i", input_path]
|
| 109 |
+
if end_time:
|
| 110 |
+
cmd += ["-to", end_time]
|
| 111 |
+
|
| 112 |
+
vf = []
|
| 113 |
+
if mode == "Every N seconds":
|
| 114 |
+
vf.append(f"fps={max(1e-6, 1.0/float(every_seconds or 1))}")
|
| 115 |
+
elif mode == "Every Nth frame":
|
| 116 |
+
vf.append(f"select='not(mod(n,{max(1, int(nth_frame or 1))}))'")
|
| 117 |
+
vf.append("setpts=N/FRAME_RATE/TB")
|
| 118 |
+
elif mode == "Exact FPS":
|
| 119 |
+
vf.append(f"fps={max(1e-6, float(exact_fps or 1))}")
|
| 120 |
+
elif mode == "All frames":
|
| 121 |
+
pass
|
| 122 |
+
else:
|
| 123 |
+
vf.append("fps=1")
|
| 124 |
+
|
| 125 |
+
if scene_detect:
|
| 126 |
+
vf.append(f"select='gt(scene,{float(scene_thresh)})',showinfo")
|
| 127 |
+
vf.append("setpts=N/FRAME_RATE/TB")
|
| 128 |
+
|
| 129 |
+
if long_side and long_side > 0:
|
| 130 |
+
vf.append("scale='if(gt(iw,ih),%d,-1)':'if(gt(iw,ih),-1,%d)':force_original_aspect_ratio=decrease" % (long_side, long_side))
|
| 131 |
+
|
| 132 |
+
if vf:
|
| 133 |
+
cmd += ["-vf", ",".join(vf)]
|
| 134 |
+
|
| 135 |
+
if out_format == "jpg":
|
| 136 |
+
cmd += ["-q:v", str(jpg_quality)]
|
| 137 |
+
elif out_format == "png":
|
| 138 |
+
cmd += ["-compression_level", str(png_level)]
|
| 139 |
+
|
| 140 |
+
cmd += ["-frame_pts", "1", out_pattern]
|
| 141 |
+
return cmd
|
| 142 |
+
|
| 143 |
+
# ──────────────────��──────────────────────────────────────────
|
| 144 |
+
# Real-ESRGAN setup (CPU-friendly)
|
| 145 |
+
# ─────────────────────────────────────────────────────────────
|
| 146 |
+
try:
|
| 147 |
+
from realesrgan import RealESRGANer
|
| 148 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 149 |
+
_HAVE_REALESRGAN = True
|
| 150 |
+
except Exception:
|
| 151 |
+
_HAVE_REALESRGAN = False
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def get_realesrganer(model_name: str, scale: int, tile: int, half: bool, device: str = "cpu"):
|
| 155 |
+
if not _HAVE_REALESRGAN:
|
| 156 |
+
raise RuntimeError("realesrgan is not installed. Check requirements.txt")
|
| 157 |
+
# Model selection
|
| 158 |
+
if model_name == "x4plus":
|
| 159 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 160 |
+
model_scale = 4
|
| 161 |
+
netscale = 4
|
| 162 |
+
model_path = None # let the lib auto-download
|
| 163 |
+
elif model_name == "x4plus-anime":
|
| 164 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 165 |
+
model_scale = 4
|
| 166 |
+
netscale = 4
|
| 167 |
+
model_path = None
|
| 168 |
+
elif model_name == "x2plus":
|
| 169 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 170 |
+
model_scale = 2
|
| 171 |
+
netscale = 2
|
| 172 |
+
model_path = None
|
| 173 |
+
else:
|
| 174 |
+
raise ValueError("Unknown model")
|
| 175 |
+
|
| 176 |
+
if scale not in (2, 4):
|
| 177 |
+
scale = model_scale
|
| 178 |
+
|
| 179 |
+
upsampler = RealESRGANer(
|
| 180 |
+
scale=model_scale,
|
| 181 |
+
model_path=model_path,
|
| 182 |
+
model=model,
|
| 183 |
+
tile=tile,
|
| 184 |
+
tile_pad=10,
|
| 185 |
+
pre_pad=0,
|
| 186 |
+
half=half,
|
| 187 |
+
device=device,
|
| 188 |
+
)
|
| 189 |
+
return upsampler, model_scale
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def upscale_images(img_paths: List[Path], out_dir: Path, model_name: str, scale: int, tile: int, precision: str, progress=gr.Progress(track_tqdm=True)) -> List[str]:
|
| 193 |
+
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 194 |
+
half = (precision == "half") and (device == "cuda")
|
| 195 |
+
|
| 196 |
+
upsampler, model_scale = get_realesrganer(model_name, scale, tile, half, device=device)
|
| 197 |
+
|
| 198 |
+
out_paths: List[str] = []
|
| 199 |
+
for i, p in enumerate(img_paths, 1):
|
| 200 |
+
try:
|
| 201 |
+
from PIL import Image
|
| 202 |
+
img = Image.open(p).convert("RGB")
|
| 203 |
+
output, _ = upsampler.enhance(np.array(img), outscale=scale)
|
| 204 |
+
out_img = Image.fromarray(output)
|
| 205 |
+
out_file = out_dir / p.name
|
| 206 |
+
out_img.save(out_file, quality=95)
|
| 207 |
+
out_paths.append(str(out_file))
|
| 208 |
+
except Exception as e:
|
| 209 |
+
# Skip problematic files, continue
|
| 210 |
+
print(f"Upscale failed for {p}: {e}")
|
| 211 |
+
progress(i / max(1, len(img_paths)))
|
| 212 |
+
return out_paths
|
| 213 |
+
|
| 214 |
+
# numpy import (used above)
|
| 215 |
+
import numpy as np
|
| 216 |
+
|
| 217 |
+
# ─────────────────────────────────────────────────────────────
|
| 218 |
+
# Pipeline: extract → upscale → zip
|
| 219 |
+
# ─────────────────────────────────────────────────────────────
|
| 220 |
+
|
| 221 |
+
def run_pipeline(
|
| 222 |
+
video: gr.File | None,
|
| 223 |
+
mode: str,
|
| 224 |
+
every_seconds: float,
|
| 225 |
+
nth_frame: int,
|
| 226 |
+
exact_fps: float,
|
| 227 |
+
start_time: str,
|
| 228 |
+
end_time: str,
|
| 229 |
+
long_side: int,
|
| 230 |
+
out_format: str,
|
| 231 |
+
jpg_quality: int,
|
| 232 |
+
png_level: int,
|
| 233 |
+
scene_detect: bool,
|
| 234 |
+
scene_thresh: float,
|
| 235 |
+
prefix: str,
|
| 236 |
+
# Upscale
|
| 237 |
+
do_upscale: bool,
|
| 238 |
+
model_name: str,
|
| 239 |
+
scale: int,
|
| 240 |
+
tile: int,
|
| 241 |
+
precision: str,
|
| 242 |
+
):
|
| 243 |
+
if not video or not video.name:
|
| 244 |
+
return None, None, None, "Upload a video.", ""
|
| 245 |
+
if not FFMPEG or not FFPROBE:
|
| 246 |
+
return None, None, None, "FFmpeg missing. See note below.", MISSING_MSG
|
| 247 |
+
if do_upscale and not _HAVE_REALESRGAN:
|
| 248 |
+
return None, None, None, "realesrgan is not installed (see requirements.txt)", ""
|
| 249 |
+
|
| 250 |
+
# Work dirs
|
| 251 |
+
work = Path(tempfile.mkdtemp(prefix="vid2up_"))
|
| 252 |
+
raw_dir = work / "frames_raw"
|
| 253 |
+
up_dir = work / "frames_upscaled"
|
| 254 |
+
raw_dir.mkdir(parents=True, exist_ok=True)
|
| 255 |
+
up_dir.mkdir(parents=True, exist_ok=True)
|
| 256 |
+
|
| 257 |
+
# Build and run FFmpeg
|
| 258 |
+
pattern = str(raw_dir / f"{prefix}_%05d.{out_format}")
|
| 259 |
+
cmd = build_ffmpeg_command(
|
| 260 |
+
input_path=video.name,
|
| 261 |
+
mode=mode,
|
| 262 |
+
every_seconds=every_seconds,
|
| 263 |
+
nth_frame=nth_frame,
|
| 264 |
+
exact_fps=exact_fps,
|
| 265 |
+
start_time=start_time.strip(),
|
| 266 |
+
end_time=end_time.strip(),
|
| 267 |
+
long_side=long_side,
|
| 268 |
+
out_format=out_format,
|
| 269 |
+
jpg_quality=jpg_quality,
|
| 270 |
+
png_level=png_level,
|
| 271 |
+
scene_detect=scene_detect,
|
| 272 |
+
scene_thresh=scene_thresh,
|
| 273 |
+
out_pattern=pattern,
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
cmd_preview = " ".join([s if " " not in s else f'"{s}"' for s in cmd])
|
| 277 |
+
|
| 278 |
+
proc = subprocess.run(cmd, capture_output=True, text=True)
|
| 279 |
+
if proc.returncode != 0:
|
| 280 |
+
return None, None, None, f"FFmpeg error:\n{proc.stderr}", cmd_preview
|
| 281 |
+
|
| 282 |
+
frames = sorted(raw_dir.glob(f"{prefix}_*.{out_format}"))
|
| 283 |
+
if not frames:
|
| 284 |
+
return None, None, None, "No frames extracted.", cmd_preview
|
| 285 |
+
|
| 286 |
+
# Optionally upscale
|
| 287 |
+
if do_upscale:
|
| 288 |
+
up_paths = upscale_images(frames, up_dir, model_name, scale, tile, precision)
|
| 289 |
+
gallery = up_paths[:60]
|
| 290 |
+
final_zip_dir = up_dir
|
| 291 |
+
detail = f"Frames extracted: {len(frames)} | Upscaled: {len(up_paths)} | Model: {model_name} | Scale: x{scale} | Tile: {tile} | Precision: {precision}"
|
| 292 |
+
else:
|
| 293 |
+
gallery = [str(p) for p in frames[:60]]
|
| 294 |
+
final_zip_dir = raw_dir
|
| 295 |
+
detail = f"Frames extracted: {len(frames)} | (Upscale: off)"
|
| 296 |
+
|
| 297 |
+
# Zip
|
| 298 |
+
zip_path = work / ("upscaled_frames.zip" if do_upscale else "frames.zip")
|
| 299 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 300 |
+
for p in sorted(final_zip_dir.glob("*")):
|
| 301 |
+
zf.write(p, p.name)
|
| 302 |
+
|
| 303 |
+
return gallery, str(zip_path), detail, "", cmd_preview
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
# ─────────────────────────────────────────────────────────────
|
| 307 |
+
# UI
|
| 308 |
+
# ─────────────────────────────────────────────────────────────
|
| 309 |
+
|
| 310 |
+
def build_ui():
|
| 311 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 312 |
+
.cf-title { font-size: 1.6rem; font-weight: 800; }
|
| 313 |
+
.cmdbox textarea { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
|
| 314 |
+
""") as demo:
|
| 315 |
+
gr.Markdown("""
|
| 316 |
+
<div class="cf-title">Video → JPG → Upscale (FFmpeg + Real-ESRGAN)</div>
|
| 317 |
+
Upload a video, extract frames, and optionally upscale each image.
|
| 318 |
+
""")
|
| 319 |
+
|
| 320 |
+
with gr.Row():
|
| 321 |
+
video = gr.File(label="Upload video", file_types=[".mp4", ".mov", ".mkv", ".avi", ".webm", ".m4v"], type="filepath")
|
| 322 |
+
|
| 323 |
+
with gr.Accordion("Extraction", open=True):
|
| 324 |
+
with gr.Row():
|
| 325 |
+
mode = gr.Dropdown(["Every N seconds", "Every Nth frame", "Exact FPS", "All frames"], value="Every N seconds", label="Mode")
|
| 326 |
+
every_seconds = gr.Number(value=1.0, label="Every N seconds")
|
| 327 |
+
nth_frame = gr.Number(value=30, label="Every Nth frame")
|
| 328 |
+
exact_fps = gr.Number(value=1.0, label="Exact FPS")
|
| 329 |
+
with gr.Row():
|
| 330 |
+
start_time = gr.Textbox(value="", label="Start (HH:MM:SS.mmm)")
|
| 331 |
+
end_time = gr.Textbox(value="", label="End (HH:MM:SS.mmm)")
|
| 332 |
+
long_side = gr.Number(value=0, label="Resize long side px (0 = none)")
|
| 333 |
+
with gr.Row():
|
| 334 |
+
out_format = gr.Dropdown(["jpg", "png"], value="jpg", label="Output format")
|
| 335 |
+
jpg_quality = gr.Slider(2, 31, value=3, step=1, label="JPG quality (2=best)")
|
| 336 |
+
png_level = gr.Slider(0, 9, value=2, step=1, label="PNG compression level")
|
| 337 |
+
with gr.Row():
|
| 338 |
+
scene_detect = gr.Checkbox(False, label="Scene-change detect")
|
| 339 |
+
scene_thresh = gr.Slider(0.0, 1.0, value=0.3, step=0.01, label="Scene threshold")
|
| 340 |
+
prefix = gr.Textbox(value="frame", label="Filename prefix")
|
| 341 |
+
|
| 342 |
+
with gr.Accordion("Upscaling", open=True):
|
| 343 |
+
with gr.Row():
|
| 344 |
+
do_upscale = gr.Checkbox(True, label="Upscale frames with Real-ESRGAN")
|
| 345 |
+
model_name = gr.Dropdown(["x4plus", "x4plus-anime", "x2plus"], value="x4plus", label="Model")
|
| 346 |
+
scale = gr.Dropdown([2, 4], value=4, label="Output scale")
|
| 347 |
+
with gr.Row():
|
| 348 |
+
tile = gr.Number(value=0, label="Tile size (0 = auto)») # tiling reduces RAM; try 256/512 on GPU, 0 for auto
|
| 349 |
+
precision = gr.Dropdown(["auto", "half", "full"], value="auto", label="Precision (GPU=half, CPU=full)")
|
| 350 |
+
|
| 351 |
+
run_btn = gr.Button("Run: Extract → (Upscale) → ZIP", variant="primary")
|
| 352 |
+
gallery = gr.Gallery(label="Preview (first 60)", columns=6, height=300)
|
| 353 |
+
zip_out = gr.File(label="Download ZIP")
|
| 354 |
+
details = gr.Markdown("Ready.")
|
| 355 |
+
|
| 356 |
+
with gr.Accordion("Show FFmpeg command", open=False):
|
| 357 |
+
cmd_preview = gr.Textbox(label="ffmpeg command", lines=4, elem_classes=["cmdbox"])
|
| 358 |
+
if MISSING_MSG:
|
| 359 |
+
gr.Markdown(f"<span style='color:#b45309'>{MISSING_MSG}</span>")
|
| 360 |
+
|
| 361 |
+
def _toggle(mode_val, fmt):
|
| 362 |
+
return (
|
| 363 |
+
gr.update(visible=(mode_val == "Every N seconds")),
|
| 364 |
+
gr.update(visible=(mode_val == "Every Nth frame")),
|
| 365 |
+
gr.update(visible=(mode_val == "Exact FPS")),
|
| 366 |
+
gr.update(visible=(fmt == "jpg")),
|
| 367 |
+
gr.update(visible=(fmt == "png")),
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
mode.change(_toggle, [mode, out_format], [every_seconds, nth_frame, exact_fps, jpg_quality, png_level])
|
| 371 |
+
out_format.change(_toggle, [mode, out_format], [every_seconds, nth_frame, exact_fps, jpg_quality, png_level])
|
| 372 |
+
demo.load(_toggle, [mode, out_format], [every_seconds, nth_frame, exact_fps, jpg_quality, png_level])
|
| 373 |
+
|
| 374 |
+
run_btn.click(
|
| 375 |
+
run_pipeline,
|
| 376 |
+
inputs=[
|
| 377 |
+
video, mode, every_seconds, nth_frame, exact_fps,
|
| 378 |
+
start_time, end_time, long_side, out_format, jpg_quality, png_level,
|
| 379 |
+
scene_detect, scene_thresh, prefix,
|
| 380 |
+
do_upscale, model_name, scale, tile, precision,
|
| 381 |
+
],
|
| 382 |
+
outputs=[gallery, zip_out, details, gr.Textbox(), cmd_preview],
|
| 383 |
+
api_name="extract_and_upscale",
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
return demo
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
if __name__ == "__main__":
|
| 390 |
+
demo = build_ui()
|
| 391 |
+
demo.queue().launch()
|