-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranscode.py
380 lines (345 loc) · 14.8 KB
/
transcode.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python
# Mark's Media Framework
# Copyright (C) 2011 Mark Pariente
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import optparse
import os
import shlex
import subprocess
import sys
import tempfile
from mmf import *
def _calc_scaled_bitrate(target_config, video_max_bitrate,
scaled_width, scaled_height):
return (int(video_max_bitrate *
float(scaled_width * scaled_height) /
float(target_config.video_max_width *
target_config.video_max_height)))
def main(argv = sys.argv):
optparser = optparse.OptionParser()
optparser.add_option(
"-o", "--output", action="store", type="string",
dest="output_file", help="Output file name")
optparser.add_option(
"-t", "--target", action="store", type="string",
dest="target_string", help="Target device name")
optparser.add_option(
"-l", "--length", type="int", dest="duration",
help="Number of seconds to encode (defaults to whole file)")
optparser.add_option(
"-s", "--offset", type="int", dest="start_offset",
help="Offset in seconds from the beginning of the input file to skip")
optparser.add_option(
"-2", "--double-pass", action = "store_true", dest="double_pass",
help="Use double-pass encoding for better compression efficiency")
optparser.add_option(
"-p", "--preset", action="store", type="string", dest="ffmpeg_preset",
help="FFMpeg preset to use for encoding")
optparser.add_option(
"-n", "--use-neroaac", action = "store_true", dest="use_neroaac",
help="Use neroAacEnc instead of ffmpeg for audio encoding")
optparser.add_option(
"-v", "--video-bitrate", action = "store", type="string", dest="video_bitrate",
help="Override video bitrate information from input file")
(options, extra_args) = optparser.parse_args()
if options.output_file is None:
print "No output file specified, exiting."
sys.exit(1)
else:
output_path = os.path.abspath(options.output_file)
if options.target_string is None:
print "No target specified, exiting."
sys.exit(1)
if len(extra_args) == 0:
print "No input file specified, exiting."
sys.exit(1)
elif len(extra_args) == 1:
single_file = True
input_file = os.path.abspath(extra_args[0])
input_file_str = " -i \"" + input_file + "\""
else:
if options.duration or options.start_offset:
print "Multiple file mode is not compatible with --length or \
--offset."
sys.exit(1)
single_file = False
try:
input_files = multifile.MultiFileInput(extra_args)
except errors.MMFError as e:
print e.msg
sys.exit(1)
input_file_str = " -i -"
try:
if single_file:
vid_info = vidparse.VidParser(extra_args[0])
else:
vid_info = input_files.parser
except errors.MMFError as e:
print e.msg
sys.exit(1)
try:
target_config = targetconfig.TargetConfig(options.target_string)
except errors.MMFError as e:
print e.msg
sys.exit(1)
temp_dir = tempfile.mkdtemp()
print "Working directory: " + temp_dir
os.chdir(temp_dir)
# Prepare duration strings
if not options.duration:
length_str = ""
else:
length_str = " -t " + str(options.duration)
if not options.start_offset:
offset_str = ""
else:
offset_str = " -ss " + str(options.start_offset)
# Audio parameter calculation
if vid_info.audio_bitrate is None:
print (("WARNING: No audio bitrate information for %s using %d Kbps") %
(vid_info.input_file_name, target_config.audio_max_bitrate))
audio_bitrate = target_config.audio_max_bitrate
else:
audio_bitrate = min(vid_info.audio_bitrate,
target_config.audio_max_bitrate)
if audio_bitrate < target_config.audio_max_bitrate:
# Round up bitrate to a standard step unless it is > 320 in which
# case just keep it
audio_bitrate_steps = [320, 256, 192, 160, 128, 112, 96, 64, 0]
prev_rate = audio_bitrate
for rate in audio_bitrate_steps:
if audio_bitrate > rate:
audio_bitrate = prev_rate
break
prev_rate = rate
audio_bitrate = audio_bitrate * 1000
if options.use_neroaac:
# Audio encode with neroAacEnc using ffmpeg to convert input to pcm
ffmpeg_cmdline = ("ffmpeg -v 0 -y" + offset_str + length_str +
input_file_str + " -vn -acodec pcm_s16le -ac " +
str(target_config.audio_channel_count) + " -ar " +
str(target_config.audio_sample_rate) +
" -f wav pipe:1")
print ffmpeg_cmdline
ffmpeg_args = shlex.split(ffmpeg_cmdline)
null_device = open(os.devnull, 'w')
if single_file:
ffmpeg = subprocess.Popen(ffmpeg_args, stdout = subprocess.PIPE,
stderr = null_device)
else:
ffmpeg = subprocess.Popen(ffmpeg_args, stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = null_device)
try:
neroaac_dir = os.environ['NEROAAC_DIR']
except KeyError:
neroaac_dir = ""
neroaac_path = os.path.join(neroaac_dir, "neroAacEnc")
neroaac_cmdline = (neroaac_path + " -cbr " + str(audio_bitrate) +
" -lc -ignorelength -if - -of output-audio.aac")
print neroaac_cmdline
neroaac_args = shlex.split(neroaac_cmdline)
neroaac = subprocess.Popen(neroaac_args, stdin = ffmpeg.stdout)
if single_file:
while ffmpeg.returncode is not None:
buf = ffmpeg.communicate()
neroaac.communicate(buf)
neroaac.wait()
else:
input_files.set_output(ffmpeg.stdin)
try:
input_files.write_all()
except IOError as e:
print "FFMpeg was killed or input files not \
compatible with concatenation"
print e
sys.exit(1)
# Flush ffmpeg's stdout and kill it, couldn't find any other way
# of making neroaac drain ffmpeg's stdout without hanging
ffmpeg.stdout.flush()
ffmpeg.kill()
neroaac.wait()
input_files.rewind()
null_device.close()
# Video parameter calculations
if target_config.codec_h264_same:
# use same profile/level as input
if vid_info.vid_format_profile is None:
print vid_info
print "Same H.264 profile/level as input requested, but no info\
found in input file."
sys.exit(1)
partition = vid_info.vid_format_profile.partition('@')
h264_profile_str = partition[0].lower()
h264_level_str = partition[2].replace('.','').replace('L','')
else:
# use target preset for profile/level
h264_level_str = target_config.codec_h264_level.replace('.', '')
h264_profile_str = target_config.codec_h264_profile.lower()
if options.video_bitrate is not None:
video_max_bitrate = min(target_config.video_max_bitrate,
int(options.video_bitrate))
elif vid_info.vid_bitrate is not None:
video_max_bitrate = min(target_config.video_max_bitrate,
vid_info.vid_bitrate)
else:
print ("No video bitrate information for '%s' use -v [bitrate]" %
vid_info.input_file_name)
sys.exit(1)
if vid_info.vid_width is None or vid_info.vid_height is None:
print ("No video width/height information for '%s'" %
vid_info.input_file_name)
sys.exit(1)
if vid_info.vid_width > target_config.video_max_width:
# Scale down width and see if height is within maximum allowed
scale_factor = (float(target_config.video_max_width) /
vid_info.vid_width)
scaled_height = int(float(vid_info.vid_height) * scale_factor)
if scaled_height % 2 == 1: # make sure scaled height divisible by 2
scaled_height += 1
if scaled_height < target_config.video_max_height:
vid_size_str = (" -s " + str(target_config.video_max_width) +
"x" + str(scaled_height))
bit_rate = _calc_scaled_bitrate(target_config,
video_max_bitrate,
target_config.video_max_width,
scaled_height)
else:
# Failed, have to break aspect ratio
vid_size_str = (" -s " + str(target_config.video_max_width) +
"x" + str(target_config.video_max_height))
bit_rate = video_max_bitrate
elif vid_info.vid_height > target_config.video_max_height:
# Scale down height and see if width is within maximum allowed
scale_factor = (float(target_config.video_max_height) /
vid_info.vid_height)
scaled_width = int(float(vid_info.vid_width) * scale_factor)
if scaled_width % 2 == 1: # make sure scaled width divisible by 2
scaled_width += 1
if scaled_width < target_config.video_max_width:
vid_size_str = (" -s " + str(scaled_width) + "x" +
str(target_config.video_max_height))
bit_rate = _calc_scaled_bitrate(target_config,
video_max_bitrate,
scaled_width,
target_config.video_max_height)
else:
# Failed, have to break aspect ratio
vid_size_str = (" -s " + str(target_config.video_max_width) + "x" +
str(target_config.video_max_height))
bit_rate = video_max_bitrate
else:
# Video is smaller than target's max width/height, use max bitrate
vid_size_str = ""
bit_rate = video_max_bitrate
vid_bitrate_str = " -b:v " + str(bit_rate * 1000)
if options.ffmpeg_preset:
preset_str = " -preset " + options.ffmpeg_preset
else:
preset_str = ""
# Interlace handling
if vid_info.vid_interlaced:
if target_config.video_interlaced:
int_str = " -flags +ildct"
else:
int_str = " -vf yadif=1"
if vid_info.vid_fps is None:
fps_str = ""
elif vid_info.vid_fps == 23.976:
fps_str = " -r 24000/1001"
elif vid_info.vid_fps == 29.970:
fps_str = " -r 30000/1001"
else:
fps_str = " -r " + str(vid_info.vid_fps)
else:
if target_config.video_interlaced:
print "WARNING: Interlaced output for progressive input \
not supported!"
int_str = ""
fps_str = ""
if options.double_pass:
# Video first pass
ffmpeg_cmdline = ("ffmpeg -y" + offset_str + length_str +
input_file_str + vid_size_str +
" -pass 1 -vcodec libx264" + " -threads 0 -level " +
h264_level_str + preset_str + " -vprofile " +
h264_profile_str + vid_bitrate_str + int_str +
fps_str + " -acodec copy -f rawvideo /dev/null")
print ffmpeg_cmdline
ffmpeg_args = shlex.split(ffmpeg_cmdline)
if single_file:
ffmpeg = subprocess.Popen(ffmpeg_args)
else:
ffmpeg = subprocess.Popen(ffmpeg_args, stdin = subprocess.PIPE)
input_files.set_output(ffmpeg.stdin)
try:
input_files.write_all()
except IOError as e:
print "FFMpeg was killed or input files not \
compatible with concatenation"
print e
sys.exit(1)
ffmpeg.wait()
if not single_file:
input_files.rewind()
pass_str = " -pass 2"
else:
pass_str = ""
# Video second (or first) pass + muxer step (+ audio encode if not using
# neroAac)
if options.use_neroaac:
# Stream mapping calculation
if (vid_info.audio_stream_id is None or
vid_info.vid_stream_id is None):
stream_map_str = ""
if vid_info.audio_stream_id > vid_info.vid_stream_id:
stream_map_str = " -map 0:0 -map 1:0"
else:
stream_map_str = " -map 0:1 -map 1:0"
audio_input_str = " -i output-audio.aac"
audio_codec_str = " -acodec copy"
else:
stream_map_str = ""
audio_input_str = ""
audio_codec_str = (" -acodec libvo_aacenc -ac " +
str(target_config.audio_channel_count) + " -ar " +
str(target_config.audio_sample_rate) + " -ab " +
str(audio_bitrate))
ffmpeg_cmdline = ("ffmpeg -y" + offset_str + length_str + input_file_str +
audio_input_str + stream_map_str + vid_size_str +
pass_str + " -vcodec libx264 -threads 0 -level " +
h264_level_str + preset_str +" -vprofile " +
h264_profile_str + vid_bitrate_str + int_str +
fps_str + audio_codec_str + " \"" + output_path + "\"")
print ffmpeg_cmdline
ffmpeg_args = shlex.split(ffmpeg_cmdline)
if single_file:
ffmpeg = subprocess.Popen(ffmpeg_args)
else:
ffmpeg = subprocess.Popen(ffmpeg_args, stdin = subprocess.PIPE)
input_files.set_output(ffmpeg.stdin)
try:
input_files.write_all()
except IOError as e:
print "FFMpeg was killed or input files not \
compatible with concatenation"
print e
sys.exit(1)
ffmpeg.wait()
# Clean up intermediate files
if options.use_neroaac:
os.remove(os.path.join(temp_dir, "output-audio.aac"))
if options.double_pass:
os.remove(os.path.join(temp_dir, "ffmpeg2pass-0.log"))
os.remove(os.path.join(temp_dir, "x264_2pass.log.mbtree"))
os.remove(os.path.join(temp_dir, "x264_2pass.log"))
os.rmdir(temp_dir)