-
Notifications
You must be signed in to change notification settings - Fork 0
/
srt_to_sub_time_burn.py
77 lines (63 loc) · 3 KB
/
srt_to_sub_time_burn.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
import os
import sys
import subprocess
def get_frame_rate(video_file):
"""Use FFmpeg to get the frame rate of the video."""
result = subprocess.run(
['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=r_frame_rate', '-of', 'default=noprint_wrappers=1:nokey=1', video_file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
rate = result.stdout.decode().strip()
num, denom = map(int, rate.split('/'))
return num / denom
def burn_subtitles(video_file, srt_file=None, font_size=None, smpte_only=False, subs_only=False):
"""Burn subtitles and/or SMPTE timecode into the video."""
if not os.path.isfile(video_file):
print(f"Error: The video file '{video_file}' does not exist.")
return
frame_rate = get_frame_rate(video_file)
video_dir, video_name = os.path.split(video_file)
output_prefix = "tc_" if smpte_only else "subs_" if subs_only else "burn_"
output_file = os.path.join(video_dir, f"{output_prefix}{video_name}")
filters = []
if not subs_only:
filters.append(f"drawtext=fontfile=/Library/Fonts/DroidSansMono.ttf:timecode='00\\:00\\:00\\:00':rate={frame_rate}:fontsize=30:fontcolor=white:x=10:y=10:box=1:boxcolor=0x000000AA")
if not smpte_only:
if not os.path.isfile(srt_file):
print(f"Error: The subtitle file '{srt_file}' does not exist.")
return
subtitles_filter = f"subtitles={srt_file}"
if font_size:
subtitles_filter += f":force_style='FontSize={font_size},PrimaryColour=&H00FFFFFF&,OutlineColour=&H00000000&,BackColour=&H80000000&,Outline=2,Shadow=3,MarginV=50'"
filters.append(subtitles_filter)
ffmpeg_command = [
'ffmpeg',
'-i', video_file,
'-vf', ",".join(filters),
'-c:a', 'copy',
output_file
]
try:
subprocess.run(ffmpeg_command, check=True)
print(f"Subtitles and SMPTE timecode burned into video successfully. Output file: '{output_file}'")
except subprocess.CalledProcessError as e:
print(f"Error: FFmpeg failed with exit code {e.returncode}")
print(e.output)
if __name__ == "__main__":
if len(sys.argv) < 2 or (len(sys.argv) < 3 and '--smpte-only' not in sys.argv and '--subs-only' not in sys.argv):
print("Usage: python burn_subtitles.py <video_file> [<srt_file> [font_size]] [--smpte-only | --subs-only]")
elif '--smpte-only' in sys.argv and '--subs-only' in sys.argv:
print("Error: Cannot use both '--smpte-only' and '--subs-only' at the same time.")
else:
video_file = sys.argv[1]
smpte_only = '--smpte-only' in sys.argv
subs_only = '--subs-only' in sys.argv
if smpte_only or subs_only:
srt_file = None
font_size = None
else:
srt_file = sys.argv[2]
font_size = int(sys.argv[3]) if len(sys.argv) >= 4 else None
burn_subtitles(video_file, srt_file, font_size, smpte_only, subs_only)