-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_commands.py
289 lines (258 loc) · 10.3 KB
/
ffmpeg_commands.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
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
import re
import os
import Utils
class FFmpegCommands:
def __init__(self, smelt_instance):
self.smelt = smelt_instance
def construct_dpx_commands(self):
"""
Construct FFmpeg commands for DPX file processing.
This method constructs a set of FFmpeg commands for processing DPX image sequences
into various video formats including lossless MOV, H.264 MP4, and ProRes MOV.
Commands:
- ffmpeg_base: Base FFmpeg command with verbosity and progress options.
- ffmpeg_dpx: Command to input DPX image sequence and optional audio file.
- '-f image2': Input format as image sequence.
- '-vsync 0': Disable frame duplication or dropping.
- '-framerate': Frame rate for the input sequence.
- '-start_number 0': Start frame number.
- '-i': Input file pattern for DPX files.
Conditional Commands:
- If audio is included:
- '-i': Audio file input.
- '-c:a copy': Copy audio codec without re-encoding.
Output Commands:
- Lossless MOV:
- '-qp 0': Lossless quality.
- H.264 MP4:
- '-c:v libx264': Use H.264 codec for video.
- '-pix_fmt yuv420p': Pixel format.
- '-vf scale=-2:1080': Scale video to 1080p while preserving aspect ratio.
- '-preset slow': Encoding speed/quality tradeoff.
- '-crf 23': Constant rate factor for quality control.
- '-c:a aac': Use AAC codec for audio.
- '-b:a 224k': Audio bitrate.
- '-map 0:v:0': Map first video stream.
- '-map 1:a:0': Map first audio stream.
- ProRes MOV:
- '-c:v prores': Use ProRes codec for video.
- '-profile:v 3': ProRes 422 HQ profile.
- '-c:a pcm_s16le': Use PCM audio codec with 16-bit little-endian samples.
The constructed commands are stored in instance variables:
- self.ffmpeg_lossless_cmd
- self.ffmpeg_h264_cmd_direct
- self.ffmpeg_prores_cmd
- self.ffmpeg_h264_cmd
"""
base_filename = os.path.basename(self.images_path)
prefix = re.match(r'^\D*', base_filename).group()
start_number = Utils.extract_number(self.images_path)
ffmpeg_input_pattern = os.path.join(self.folder_path, '{}%06d.dpx'.format(prefix))
self.ffmpeg_base = [
self.ffmpeg_path, '-v',
'info', '-stats',
'-progress', '-',
'-report'
]
ffmpeg_dpx = [
'-f', 'image2',
'-vsync', '0',
'-framerate', self.fps,
'-start_number', str(start_number),
'-i', ffmpeg_input_pattern,
]
if self.inkluderLydCheckBox.isChecked() and os.path.exists(self.audio_file):
ffmpeg_dpx.extend(['-i', self.audio_file])
audio_cmd = ['-c:a', 'copy']
else:
audio_cmd = []
self.ffmpeg_lossless_cmd = (self.ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_dpx +
self.ffmpeg_encoder + audio_cmd + [
'-preset', 'slow',
'-qp', '0',
self.lossless_mov,
self.proceed_lossless,
])
if self.inkluderLydCheckBox.isChecked():
self.ffmpeg_h264_cmd_direct = self.ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_dpx + audio_cmd + [
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-vf', 'scale=w=min(1920\,iw):h=min(1080\,ih):force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-preset', 'slow',
'-crf', '23',
'-c:a', 'aac',
'-b:a', '224k',
'-ac', '2', # Force steroe (2 channels)
'-map', '0:v:0',
'-map', '1:a:0',
self.h264_mp4,
self.proceed_h264
]
else:
self.ffmpeg_h264_cmd_direct = self.ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_dpx + [
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-vf', 'scale=w=min(1920\,iw):h=min(1080\,ih):force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-preset', 'slow',
'-crf', '23',
'-map', '0:v:0',
self.h264_mp4,
self.proceed_h264
]
if self.mezzaninfilCheckBox.isChecked():
self.ffmpeg_prores_cmd = self.ffmpeg_base + [
'-hwaccel', 'auto',
'-i', self.lossless_mov,
'-vf', 'scale=-2:1080,format=yuv422p10le',
'-c:v', 'prores',
'-profile:v', '3',
'-vf', 'scale=-2:1080',
'-c:a', 'pcm_s16le',
self.prores_mov,
self.proceed_prores
]
else:
self.ffmpeg_prores_cmd = self.ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_dpx + [
'-c:v', 'prores',
'-profile:v', '3',
'-vf', 'scale=-2:1080,format=yuv422p10le',
'-c:a', 'pcm_s16le',
self.prores_mov,
self.proceed_prores
]
self.ffmpeg_h264_cmd = self.ffmpeg_base + self.ffmpeg_hardware_accel + [
'-i', self.lossless_mov,
] + self.ffmpeg_encoder + [
'-vf', 'scale=w=min(1920\,iw):h=min(1080\,ih):force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-pix_fmt', 'yuv420p',
'-preset', 'slow',
'-crf', '23',
'-c:a', 'aac',
'-b:a', '224k',
'-ac', '2', # Force stereo (2 channels)
self.h264_mp4,
self.proceed_h264
]
def construct_mxf_mov_commands(self):
"""
Construct FFmpeg commands for MXF and MOV file processing.
This method constructs FFmpeg commands to process MXF and MOV files into lossless MOV,
H.264 MP4, and ProRes MOV formats.
Commands:
- ffmpeg_base: Base FFmpeg command.
- ffmpeg_video_audio: Input video and optionally audio file.
- ffmpeg_audio_param: Parameters for audio encoding.
Output Commands:
- Lossless MOV:
- '-qp 0': Lossless quality.
- '-c:a copy': Copy audio codec without re-encoding.
- ProRes MOV:
- '-c:v prores': Use ProRes codec for video.
- '-profile:v 3': ProRes 422 HQ profile.
- '-pix_fmt yuv422p10le': 10-bit YUV 4:2:2 pixel format.
- '-vf scale=-2:1080': Scale video to 1080p while preserving aspect ratio.
- '-c:a pcm_s16le': Use PCM audio codec with 16-bit little-endian samples.
- H.264 MP4:
- '-c:v libx264': Use H.264 codec for video.
- '-pix_fmt yuv420p': Pixel format.
- '-preset slow': Encoding speed/quality tradeoff.
- '-crf 21': Constant rate factor for quality control.
- '-ac 2': Set number of audio channels to 2.
- '-b:a 224k': Audio bitrate.
The constructed commands are stored in instance variables:
- self.ffmpeg_dcp_cmd
- self.ffmpeg_dcp_prores
- self.ffmpeg_dcp_h264_cmd
- self.ffmpeg_h264_from_prores_cmd
"""
ffmpeg_base = [self.ffmpeg_path, '-report', ]
if self.inkluderLydCheckBox.isChecked and self.audio_file:
ffmpeg_video_audio = ['-i', self.video, '-i', self.audio_file, ]
ffmpeg_audio_param = [
'-c:a', 'aac',
'-b:a', '224k',
]
else:
ffmpeg_video_audio = ['-i', self.video, ]
ffmpeg_audio_param = []
self.ffmpeg_dcp_cmd = ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_video_audio + self.ffmpeg_encoder + [
'-preset', 'slow',
'-qp', '0',
'-c:a', 'copy',
'-v', 'info',
self.lossless_mov,
self.proceed_lossless,
]
self.ffmpeg_dcp_prores = ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_video_audio + [
'-c:v', 'prores',
'-profile:v', '3',
'-pix_fmt', 'yuv422p10le',
'-vf', 'scale=-2:1080',
'-c:a', 'pcm_s16le',
self.prores_mov,
self.proceed_prores
]
self.ffmpeg_dcp_h264_cmd = ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_video_audio + [
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'slow',
'-crf', '21',
'-vf', 'scale=w=min(1920\,iw):h=min(1080\,ih):force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-ac', '2',
] + ffmpeg_audio_param + [
'-v', 'info',
self.h264_mp4,
self.proceed_h264
]
self.ffmpeg_h264_from_prores_cmd = ffmpeg_base + self.ffmpeg_hardware_accel + ffmpeg_video_audio + [
'-c:v', 'libx264',
'-vf', 'scale=w=min(1920\,iw):h=min(1080\,ih):force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2',
'-pix_fmt', 'yuv420p',
'-preset', 'slow',
'-crf', '23',
'-c:a', 'aac', # Add audio codec
'-ac', '2', # Force stereo
'-b:a', '224k', # Set audio bitrate
'-v', 'info',
self.h264_mp4,
self.proceed_h264
]
def construct_audio_commands(self):
"""
Construct FFmpeg commands for audio file processing.
This method constructs FFmpeg commands to process audio files into AAC and PCM formats.
Commands:
- ffmpeg_audio_cmd: Command to convert audio to AAC format.
- '-i': Input audio file.
- '-c:a aac': Use AAC codec for audio.
- '-b:a 192k': Audio bitrate.
- '-vn': Disable video.
- ffmpeg_lossless_audio_cmd: Command to convert audio to PCM format.
- '-i': Input audio file.
- '-c:a pcm_s16le': Use PCM audio codec with 16-bit little-endian samples.
- '-vn': Disable video.
The constructed commands are stored in instance variables:
- self.ffmpeg_audio_cmd
- self.ffmpeg_lossless_audio_cmd
"""
self.ffmpeg_audio_cmd = [
self.ffmpeg_path,
'-report',
'-i', self.audio_file,
'-c:a', 'aac',
'-b:a', '192k',
'-vn',
'-v', 'info',
self.h264_mp4,
self.proceed_h264
]
self.ffmpeg_lossless_audio_cmd = [
self.ffmpeg_path,
'-report',
'-i', self.audio_file,
'-c:a', 'pcm_s16le',
'-vn',
'-v', 'info',
self.lossless_mov,
self.proceed_lossless
]