Skip to content

Reverse-engineer any video, shot by shot: filmed, AI-generated or coded, and how to remake it. Breakdowns in the open VIDEO.md format. 逆向一切视频,逐个镜头看是拍的、AI 生成的还是代码做的,再告诉你怎么复刻;拆解结果按 VIDEO.md 格式输出。

Read clip lengths more reliably

june0449f76b151c
diff --git a/rea_video/gemini.py b/rea_video/gemini.py
index 986d292..d5fdd24 100644
--- a/rea_video/gemini.py
+++ b/rea_video/gemini.py
@@ -11,13 +11,13 @@ from __future__ import annotations
import base64
import json
import os
-import subprocess
import time
import urllib.error
import urllib.request
from pathlib import Path
from . import __version__
+from .media import duration_s
from .util import LOG
API = "https://generativelanguage.googleapis.com"
@@ -34,15 +34,6 @@ def configured() -> bool:
return bool(api_key())
-def _duration(p: Path) -> float:
- r = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", str(p)],
- capture_output=True, text=True)
- try:
- return float(r.stdout.strip())
- except ValueError:
- return 0.0
-
-
class GeminiError(RuntimeError):
pass
@@ -124,7 +115,9 @@ class GeminiClient:
uploaded.append(info["name"])
part = {"file_data": {"mime_type": info.get("mimeType") or mime, "file_uri": info["uri"]}}
if mime.startswith("video/"):
- d = _duration(p)
+ d = duration_s(p)
+ if d <= 0:
+ LOG.warning("could not read the length of %s; Gemini samples it at its default rate", p.name)
fps = next((f for lim, f in CLIP_FPS if 0 < d <= lim), None)
if fps:
part["video_metadata"] = {"fps": fps}
diff --git a/rea_video/media.py b/rea_video/media.py
index 7730104..82a1ee0 100644
--- a/rea_video/media.py
+++ b/rea_video/media.py
@@ -52,6 +52,24 @@ def ffprobe(path: str | Path) -> dict:
return json.loads(out)
+def duration_s(path: str | Path) -> float:
+ """A file's length in seconds: the container's, else its video stream's; 0.0 when neither can be read."""
+ cmd = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "format=duration:stream=duration",
+ "-of", "json", str(path)]
+ try:
+ data = json.loads(subprocess.run(cmd, check=True, capture_output=True, text=True).stdout or "{}")
+ except (subprocess.CalledProcessError, OSError, ValueError):
+ return 0.0
+ for v in (data.get("format", {}).get("duration"), *(s.get("duration") for s in data.get("streams") or [])):
+ try:
+ d = float(v)
+ except (TypeError, ValueError):
+ continue
+ if d > 0:
+ return d
+ return 0.0
+
+
def _ratio(value: str | None) -> float:
if not value or value in ("0/0", "N/A"):
return 0.0
diff --git a/rea_video/remake.py b/rea_video/remake.py
index 3ff3950..2ca5723 100644
--- a/rea_video/remake.py
+++ b/rea_video/remake.py
@@ -18,6 +18,7 @@ from pathlib import Path
import cv2
import numpy as np
+from .media import duration_s
from .util import LOG, read_json, read_image
@@ -72,12 +73,10 @@ def fit_speed(clip_s: float, duration: float) -> float:
def _clip_seconds(src: Path) -> float:
- try:
- r = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", str(src)],
- capture_output=True, text=True, check=True)
- return float(r.stdout.strip() or 0)
- except (subprocess.CalledProcessError, ValueError, FileNotFoundError):
- return 0.0
+ d = duration_s(src)
+ if d <= 0:
+ LOG.warning("could not read the length of %s; it plays at normal speed", src.name)
+ return d
def frame_bounds(shots: list[dict], fps: float) -> dict[int, tuple[int, int]]: