Spaces:
Running
Running
Download app/scripts/encode-demo.py from FormaLau/DuckAgochi: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/FormaLau/DuckAgochi/resolve/main/app/scripts/encode-demo.py
- Command line
-
hf download hf://spaces/FormaLau/DuckAgochi/app/scripts/encode-demo.py
-
curl -L -o encode-demo.py https://huggingface.co/spaces/FormaLau/DuckAgochi/resolve/main/app/scripts/encode-demo.py
2.14 kB
| """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) | |