A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
2.9 KiB
Python
67 lines
2.9 KiB
Python
"""
|
|
Replaces a stretch of a finished render without encoding the rest again: the stretch is cut on keyframes, rendered anew and joined.
|
|
py -I splice.py edl.py film.mp4 out_from out_to seconds of the film
|
|
"""
|
|
import importlib.util
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
edl, film, lo, hi = sys.argv[1], sys.argv[2], float(sys.argv[3]), float(sys.argv[4])
|
|
spec = importlib.util.spec_from_file_location("edl", edl)
|
|
plan = importlib.util.module_from_spec(spec)
|
|
sys.modules["edl"] = plan
|
|
spec.loader.exec_module(plan)
|
|
|
|
|
|
def song(t):
|
|
"""The film's second t in the song's time, through the kept stretches."""
|
|
for a, b in plan.KEEP:
|
|
if t <= b - a:
|
|
return a + t
|
|
t -= b - a
|
|
raise ValueError("past the end of the song")
|
|
|
|
|
|
tmp = os.path.join(os.path.dirname(film), "splice")
|
|
shutil.rmtree(tmp, ignore_errors=True)
|
|
os.makedirs(tmp)
|
|
|
|
|
|
def run(*a):
|
|
return subprocess.run(a, check=True, capture_output=True, text=True).stdout
|
|
|
|
|
|
def frames(path):
|
|
return int(run("ffprobe", "-v", "error", "-select_streams", "v", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", path).strip().strip(","))
|
|
|
|
|
|
keys = [float(x.strip(",")) for x in run("ffprobe", "-v", "error", "-select_streams", "v", "-skip_frame", "nokey", "-show_entries", "frame=pts_time", "-of", "csv=p=0", film).split()]
|
|
a = max(k for k in keys if k <= lo)
|
|
b = min(k for k in keys if k >= hi)
|
|
print("keyframes", a, b, flush=True)
|
|
part = os.path.join(tmp, "part.mp4")
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
subprocess.run([sys.executable, "-I", os.path.join(here, "cut.py"), edl, part, f"{song(a) + 0.002:.3f}", f"{song(b) + 0.08:.3f}"], check=True, stdout=subprocess.DEVNULL)
|
|
run("ffmpeg", "-y", "-v", "error", "-i", film, "-map", "0:v", "-c", "copy", "-f", "segment", "-segment_times", f"{a - 0.004:.6f},{b - 0.004:.6f}", "-reset_timestamps", "1", os.path.join(tmp, "seg%d.mp4"))
|
|
partv = os.path.join(tmp, "partv.mp4")
|
|
# the render runs a little long so that the encoder's tail can't fall short
|
|
run("ffmpeg", "-y", "-v", "error", "-i", part, "-map", "0:v", "-c", "copy", "-frames:v", str(round((b - a) * 60)), partv)
|
|
seg = [os.path.join(tmp, f"seg{i}.mp4") for i in range(3)]
|
|
counts = [frames(p) for p in seg]
|
|
new = frames(partv)
|
|
print("frames", counts, "new", new, flush=True)
|
|
assert new == counts[1], "the new stretch must be as long as the old one"
|
|
listing = os.path.join(tmp, "list.txt")
|
|
with open(listing, "w", encoding="utf-8") as f:
|
|
for p in (seg[0], partv, seg[2]):
|
|
f.write("file '" + p.replace("\\", "/") + "'\n")
|
|
joined = os.path.join(tmp, "joined.mp4")
|
|
run("ffmpeg", "-y", "-v", "error", "-f", "concat", "-safe", "0", "-i", listing, "-i", film, "-map", "0:v", "-map", "1:a", "-c", "copy", "-movflags", "+faststart", joined)
|
|
assert frames(joined) == frames(film)
|
|
os.replace(joined, film)
|
|
shutil.rmtree(tmp)
|
|
print("done", flush=True)
|