Spaces:
Running
Running
File size: 2,137 Bytes
4263d4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | """Encode the close-up recording, detecting its opening caption in the video."""
from pathlib import Path
import json
import os
import subprocess
root = Path(__file__).resolve().parents[2]
recording = os.environ.get('DEMO_RECORDING', 'lake-sound')
out = root / 'demo' / recording
meta = json.loads((out / 'chapters.json').read_text(encoding='utf-8'))
assert not meta.get('failed') and not meta['errors'], 'Recording must finish successfully'
assert meta['scene']['rolls'] > 0 and meta['scene']['goals'] > 0
assert (out / 'audio.webm').stat().st_size > 10000
ffmpeg = os.environ.get('FFMPEG', str(Path.home() / 'AppData/Local/Programs/Python/Python311/Lib/site-packages/imageio_ffmpeg/binaries/ffmpeg-win-x86_64-v7.1.exe'))
video = (out / 'video-path.txt').read_text().strip()
assert Path(video).stat().st_mtime >= (out / 'chapters.json').stat().st_mtime, 'Wait for the browser recording to finish writing'
# Browser capture starts before the app loads. Detect the opaque caption banner
# from actual video pixels instead of assuming wall time matches capture time.
pixels = subprocess.run([ffmpeg, '-v', 'error', '-i', video, '-t', '90',
'-vf', 'fps=2,format=rgb24,crop=1:1:400:120', '-f', 'rawvideo', '-'],
capture_output=True, check=True).stdout
matches = [i // 3 / 2 for i in range(0, len(pixels), 3)
if all(abs(pixels[i+j] - value) < 12 for j, value in enumerate((23,45,40)))]
assert matches, 'Opening caption not found'
lead = matches[0]
destination = root / 'demo' / f'DuckAgochi-demo-{recording}.mp4'
subprocess.run([ffmpeg, '-hide_banner', '-loglevel', 'error', '-y', '-ss', str(lead),
'-i', video, '-ss', str(max(0, meta['demoStart']-meta['audioStart']/1000)), '-i', str(out/'audio.webm'), '-map', '0:v:0', '-map', '1:a:0', '-af', 'aresample=async=1:first_pts=0,apad', '-c:a', 'aac', '-b:a', '192k', '-shortest', '-c:v', 'libx264', '-preset', 'fast',
'-crf', '20', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', str(destination)], check=True)
(out / 'encoding.json').write_text(json.dumps({'trimSeconds':lead,'output':str(destination)},indent=2))
print(destination)
|