PulseCanvas / app · pulsecanvas.ai / scripts / label-reel.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Label a native reel with one subtitle renderer; retain its existing soundtrack.""" |
| 3 | import json |
| 4 | import subprocess |
| 5 | import sys |
| 6 | import uuid |
| 7 | from pathlib import Path |
| 8 | |
| 9 | video = Path(sys.argv[1]) |
| 10 | meta = json.loads(video.with_suffix('.json').read_text()) |
| 11 | # Display-only renames do not change pixels or permanent IDs. Use the current registry. |
| 12 | registry = json.loads(subprocess.check_output([sys.executable, str(Path(__file__).with_name('native-visualizer-catalog.py'))], text=True)) |
| 13 | meta['names'] = [effect['name'] for effect in registry['effects']] |
| 14 | video.with_suffix('.json').write_text(json.dumps(meta, indent=2) + '\n') |
| 15 | probe = json.loads(subprocess.check_output(['ffprobe', '-v', 'error', '-select_streams', 'v:0', |
| 16 | '-show_entries', 'stream=width,height', '-of', 'json', str(video)], text=True))['streams'][0] |
| 17 | width, height = probe['width'], probe['height'] |
| 18 | |
| 19 | def timestamp(seconds): |
| 20 | ticks = round(seconds * 100) |
| 21 | return f'{ticks // 360000}:{ticks // 6000 % 60:02d}:{ticks // 100 % 60:02d}.{ticks % 100:02d}' |
| 22 | |
| 23 | script = video.with_suffix('.ass') |
| 24 | margin = max(24, height // 30) |
| 25 | header = f'''[Script Info] |
| 26 | ScriptType: v4.00+ |
| 27 | PlayResX: {width} |
| 28 | PlayResY: {height} |
| 29 | WrapStyle: 2 |
| 30 | |
| 31 | [V4+ Styles] |
| 32 | Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding |
| 33 | Style: Default,Helvetica,{height // 27},&H00FFFFFF,&H00FFFFFF,&H88000000,&H88000000,0,0,0,0,100,100,0,0,3,12,0,1,{margin},{margin},{margin},1 |
| 34 | |
| 35 | [Events] |
| 36 | Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text |
| 37 | ''' |
| 38 | rows = [] |
| 39 | for segment in meta['segments']: |
| 40 | scene = int(segment['scene']) |
| 41 | name = meta['names'][scene].replace('\\', '').replace('{', '').replace('}', '').replace('\n', ' ') |
| 42 | text = f'{scene} {name}' |
| 43 | rows.append(f"Dialogue: 0,{timestamp(segment['start'])},{timestamp(segment['end'])},Default,,0,0,0,,{text}") |
| 44 | script.write_text(header + '\n'.join(rows) + '\n') |
| 45 | labeled = video.with_name(video.stem + '-labeled.mp4') |
| 46 | render_target = video.with_name(video.stem + '-labels-' + uuid.uuid4().hex + '.mp4') |
| 47 | # One libass filter avoids evaluating hundreds of drawtext filters on every 4K frame. |
| 48 | subprocess.run(['ffmpeg', '-y', '-loglevel', 'warning', '-i', str(video), |
| 49 | '-vf', f"ass=filename='{script}'", '-c:v', 'h264_videotoolbox', '-b:v', '24M' if max(width, height) >= 3840 else '8M', |
| 50 | '-pix_fmt', 'yuv420p', '-c:a', 'copy', '-movflags', '+faststart', str(render_target)], check=True) |
| 51 | render_target.replace(labeled) |
| 52 | labeled.with_suffix('.json').write_text(json.dumps(meta, indent=2) + '\n') |
| 53 | print(f'Labeled {labeled}') |