-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFF_Video_Transcoder.py
More file actions
313 lines (273 loc) · 13.7 KB
/
FF_Video_Transcoder.py
File metadata and controls
313 lines (273 loc) · 13.7 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
import os
import subprocess
from tqdm import tqdm
import platform
import json
import re
# Cleans the input path by removing escaped characters and quotes.
def clean_path_input(path):
# Removes escape characters from spaces.
path = path.replace("\\ ", " ")
# Removes escape characters from hash symbols.
path = path.replace("\\#", "#")
# Removes quotes around the path (if any).
path = path.strip('" ').strip()
return path
while True:
FootageFolderPath = input('Footage Folder Path:')
# Corrects the path input for any escaped spaces.
FootageFolderPath = clean_path_input(FootageFolderPath)
if FootageFolderPath.strip():
break
else:
print("Please enter Footage Folder Path...")
while True:
ProxyFolderPath = input('Proxy Folder Path:')
# Corrects the path input for any escaped spaces.
ProxyFolderPath = clean_path_input(ProxyFolderPath)
if ProxyFolderPath.strip():
break
else:
print("Please enter Proxy Folder Path...")
# Enlarges the Terminal window depending on the platform.
current_platform = platform.system()
if current_platform == "Darwin": # macOS
script = """
tell application "Terminal"
activate
set bounds of front window to {0, 0, 1280, 720}
end tell
"""
os.system(f"osascript -e '{script}'")
# Path to the Courier TrueType font on Windows, used for drawing text on the video.
font_win_path = r"C:\Windows\Fonts\Cour.ttf"
# Prompts the user to choose the codec until a valid choice is made
while True:
print()
print("Choose the video codec:")
print("1. HEVC")
print("2. ProRes Proxy")
print("3. Auto (Choose ProRes if footage has more than 4 audio streams)")
choice = input("Enter the number of your choice: ")
if choice in ["1", "2", "3"]:
break
else:
print("Invalid choice. Please enter 1 for HEVC, 2 for ProRes, or 3 for Auto.")
print()
# Video codec specifics
if platform.system() == "Windows":
font = font_win_path.replace('\\', '/').replace(':', '\\:')
elif platform.system() == "Darwin":
font = "Courier New"
def determine_codec(metadata):
audio_streams = sum(1 for track in metadata["media"]["track"] if track["@type"] == "Audio")
return "2" if audio_streams > 4 else "1"
class VideoTranscoder:
# Initializes VideoTranscoder with directory paths for footage and proxies.
def __init__(self, FootageFolderPath, ProxyFolderPath):
self.FootageFolderPath = FootageFolderPath
self.ProxyFolderPath = ProxyFolderPath
# Extracts metadata from the footage file using mediainfo.
def extract_metadata(self):
command = [
"mediainfo", "--Output=JSON", self.FootageFile
]
try:
output = subprocess.check_output(command).decode("utf-8")
metadata = json.loads(output)
timecode = None
burnin_timecode = None
total_frames = None
frame_rate = None
# Checks if timecode exists in the metadata.
# First pass to prioritize Video track for FrameCount and FrameRate
if "media" in metadata and "track" in metadata["media"]:
for track in metadata["media"]["track"]:
if track["@type"] == "Video":
if "FrameCount" in track:
total_frames = int(track["FrameCount"])
if "FrameRate" in track:
frame_rate = round(float(track["FrameRate"]))
break # Stop after finding the Video track
# Second pass to get TimeCode from any track and fallback for FrameCount and FrameRate
for track in metadata["media"]["track"]:
if timecode is None and "TimeCode_FirstFrame" in track:
timecode = track["TimeCode_FirstFrame"]
burnin_timecode = timecode
if total_frames is None and "FrameCount" in track:
total_frames = int(track["FrameCount"])
if frame_rate is None and "FrameRate" in track:
frame_rate = round(float(track["FrameRate"]))
if frame_rate not in [30, 60] and burnin_timecode and timecode:
# Converts drop-frame timecode to non-drop-frame timecode if MediaInfo gone wrong.
if ";" in timecode:
timecode = timecode.replace(';', ':')
# Converst drop-frame timecode to non-drop-frame timecode if MediaInfo gone wrong.
if ";" in burnin_timecode:
burnin_timecode = burnin_timecode.replace(';', ':')
# Escapes the colons in the timecode.
burnin_timecode = burnin_timecode.replace(':', '\\:')
elif burnin_timecode:
# Escapes both colons and semicolons in the timecode.
burnin_timecode = burnin_timecode.replace(':', '\\:').replace(';', '\\;')
return total_frames, burnin_timecode, frame_rate, metadata, timecode
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
return None, None, None, None, None
# Iterates through the footage directory, generating a corresponding structure in the proxy directory.
def iteration(self):
for root, dirs, files in os.walk(self.FootageFolderPath):
for file in files:
if file.lower().endswith((".mp4", ".mov", ".mxf")):
footage_file = os.path.join(root, file)
relative_path = os.path.relpath(root, self.FootageFolderPath)
path_parts = relative_path.split(os.sep)
if len(path_parts) > 0:
subfolder = path_parts[0]
else:
subfolder = ""
proxy_dir = os.path.join(self.ProxyFolderPath, subfolder)
os.makedirs(proxy_dir, exist_ok=True)
proxy_file = os.path.join(proxy_dir, os.path.splitext(file)[0] + ".mov")
self.FootageFile = footage_file
self.footage_name = os.path.basename(footage_file)
self.ProxyFile = proxy_file
self.total_frames, self.burnin_timecode, self.frame_rate, self.metadata, self.timecode = self.extract_metadata()
# Check if metadata extraction was successful
if self.metadata is None:
print(f"Skipping corrupt file: {self.FootageFile}")
continue
yield self
# Transcodes a video using FFmpeg, displaying the progress with tqdm.
def ffmpeg_tqdm(self):
if self.metadata is None:
print(f"Skipping {self.FootageFile} due to metadata extraction error.")
return
if choice == "3":
codec_choice = determine_codec(self.metadata)
else:
codec_choice = choice
if platform.system() == "Windows":
if codec_choice == "1":
vcodec = "hevc_nvenc"
codec_specific_params = [
"-map", "0:v", # Maps every streams
"-map", "0:a", # Maps every streams
"-map_metadata", "0", # Maps metadata
"-timecode", self.timecode, # Adds original timecode
"-b:v", "5000k", # video bitrate
"-color_range", "1", # Sets color range / data level flag
"-colorspace", "1", # Sets color space flag
"-color_trc", "1", # Sets OETF flag
"-color_primaries", "1", # Sets color primaries flag
"-pix_fmt", "yuv420p", # 4:2:0
]
elif codec_choice == "2":
vcodec = "prores_ks"
codec_specific_params = [
"-profile:v", "0", # Prores Proxy
"-bits_per_mb", "300", # Sets bitrate
"-quant_mat", "0", # Sets the quantization matrix to the default
"-color_range", "1", # Sets color range / data level flag
"-colorspace", "1", # Sets color space flag
"-color_trc", "1", # Sets OETF flag
"-color_primaries", "1", # Sets color primaries flag
"-map", "0:a",
"-map", "0:v",
]
elif platform.system() == "Darwin":
if codec_choice == "1":
vcodec = "hevc_videotoolbox"
codec_specific_params = [
"-map", "0:v", # Maps every streams
"-map", "0:a", # Maps every streams
"-map_metadata", "0", # Maps metadata
"-timecode", self.timecode, # Adds original timecode
"-b:v", "5000k", # video bitrate
"-color_range", "1", # Sets color range / data level flag
"-colorspace", "1", # Sets color space flag
"-color_trc", "1", # Sets OETF flag
"-color_primaries", "1", # Sets color primaries flag
"-pix_fmt", "yuv420p", # 4:2:0
]
elif codec_choice == "2":
vcodec = "prores_videotoolbox"
codec_specific_params = [
"-profile:v", "0", # Prores Proxy
"-pix_fmt", "yuv422p10le",
"-color_range", "1", # Sets color range / data level flag
"-colorspace", "1", # Sets color space flag
"-color_trc", "1", # Sets OETF flag
"-color_primaries", "1", # Sets color primaries flag
"-map", "0:a",
"-map", "0:v",
]
# Ensures none of the critical variables are None
if self.timecode is None or self.burnin_timecode is None or self.frame_rate is None:
print(f"Skipping {self.FootageFile} due to missing critical metadata (timecode, burnin_timecode, or frame_rate).")
return
# Constructs the ffmpeg command
command = [
"ffmpeg",
"-y", # Overwrite output file without asking
"-i", self.FootageFile, # Input file
"-vf", (f"scale='min(1920,iw)':-1,"f"drawtext=fontfile='{font}':fontsize=40:fontcolor=white@0.9:box=1:boxcolor=black@0.55:boxborderw=10:x=30:y=30:text='{self.footage_name}',"f"drawtext=fontfile='{font}':fontsize=40:fontcolor=white@0.9:box=1:boxcolor=black@0.55:boxborderw=10:x=w-tw-30:y=30:timecode='{self.burnin_timecode}':rate={self.frame_rate}:tc24hmax=1"),
"-c:v", vcodec,
"-c:a", "libmp3lame", # Audio codec
"-b:a", "128k", # Audio bitrate
"-progress", "pip2", # Output progress
]
# Add codec-specific parameters
command.extend(codec_specific_params)
# Add the output file at the end
command.append(self.ProxyFile)
# Print the command for debugging purposes
#print(f"Running command: {' '.join(command)}")
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, encoding='utf-8')
# Defines transcoding progress bar
# Left side padding
max_length_left = 50
desc_text = "File: " + os.path.basename(self.FootageFile)
desc_text = desc_text.ljust(max_length_left) # This pads the string to the desired length
# Right side padding
FrameSpeedpd = 13 # frame/s padding
rate_fmt = "[" + "{rate_fmt:>" + str(FrameSpeedpd) + "}]"
FrameLpd = 8 # frame left padding
FrameRpd = 9 # frame right padding
frame_fmt = "{{n:>{FrameLpd}}}/{{total:<{FrameRpd}}}".format(FrameLpd=FrameLpd, FrameRpd=FrameRpd)
pbar = tqdm(total=self.total_frames, position=0, desc=desc_text + "Progress", unit="frame", dynamic_ncols=True, bar_format='{l_bar}{bar}| [{elapsed}<{remaining}] ' + frame_fmt + rate_fmt)
frame_re = re.compile(r'frame=\s*(\d+)')
while True:
line = process.stderr.readline()
if line == "" and process.poll() is not None:
break
match = frame_re.search(line)
if match:
try:
current_frame = int(match.group(1))
pbar.update(current_frame - pbar.n) # Update the progress bar with the number of new frames processed
except ValueError:
continue # Skip the problematic line
process.wait()
# Ensures the progress bar is updated to the total frames if the process completes successfully
if process.returncode == 0:
pbar.n = self.total_frames
pbar.last_print_n = self.total_frames
pbar.update(0)
else:
print(f"Error during transcoding: {self.FootageFile}")
pbar.close()
# Caculates the number of total Video files
TotalVideoFiles = 0
for root, dirs, files in os.walk(FootageFolderPath):
for file in files:
if file.lower().endswith((".mp4", ".mov", ".mxf")):
TotalVideoFiles += 1
if __name__ == "__main__":
Transcoder = VideoTranscoder(FootageFolderPath, ProxyFolderPath)
vbar = tqdm(total=TotalVideoFiles, position=1, desc="-------- Completed Video Files", bar_format='{l_bar} {n_fmt}/{total_fmt}')
for Transcoding in Transcoder.iteration():
Transcoding.ffmpeg_tqdm()
vbar.update(1) # Update the total progress bar after processing each file
vbar.close()