-
Notifications
You must be signed in to change notification settings - Fork 4
/
ffmpeg-h264-mp4-faststart
executable file
·150 lines (131 loc) · 5.63 KB
/
ffmpeg-h264-mp4-faststart
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
#!/usr/bin/env python
import sys
import os
import posix
import optparse
import subprocess
main_function_map = {}
def main_function(func):
global main_function_map
main_function_map[func.__name__.replace('_','-')] = func
return func
# Using an Intel GPU through VAAPI, transcode video into H264 in MP4
# $ ffmpeg-intel-gpu-h264-mp4 f0.avi f1.wmv
# NOTE: causes desktop hangs
@main_function
def ffmpeg_intel_gpu_h264_mp4(args):
# https://ffmpeg.org/pipermail/ffmpeg-user/2016-May/032153.html
op = optparse.OptionParser(usage='usage: %prog FILE...', option_list=[
optparse.Option('--audio-bitrate', default='161k', help='AAC audio bitrate'),
optparse.Option('--vaapi-device', default=None, help='X11 display or DRM render node'),
])
(options, args) = op.parse_args(args)
if not args:
op.print_usage()
sys.exit(2)
if options.vaapi_device:
vaapi_device = options.vaapi_device
elif os.getenv('DISPLAY'):
vaapi_device = os.getenv('DISPLAY')
else:
l = glob.glob('/dev/dri/render*')
if not l:
sys.stderr.write("Can't determine vaapi device. Explicitly pass an option like '--vaapi-device /dev/dri/renderD12'.\n")
sys.exit(2)
vaapi_device = l[0]
new_ext = '.mp4'
for filename in args:
output_filename = os.path.splitext(os.path.basename(filename))[0] + new_ext
if os.path.realpath(output_filename) == os.path.realpath(filename):
output_filename = os.path.splitext(os.path.basename(filename))[0] + '.1' + new_ext
subprocess.check_call(['ffmpeg',
'-vaapi_device', vaapi_device,
'-i', filename,
'-vf', 'format=nv12,hwupload',
'-c:v', 'h264_vaapi',
'-c:a', 'aac',
'-b:a', str(options.audio_bitrate),
'-movflags', '+faststart',
'-y', output_filename])
# Transcode video into H264 in MP4 using ffmpeg
# $ ffmpeg-h264-mp4-faststart f0.avi f1.wmv
@main_function
def ffmpeg_h264_mp4_faststart(args):
op = optparse.OptionParser(usage='usage: %prog FILE...', option_list=[
optparse.Option('--audio-bitrate', default='161k', help='AAC audio bitrate'),
])
(options, args) = op.parse_args(args)
if not args:
op.print_usage()
sys.exit(2)
new_ext = '.mp4'
for filename in args:
output_filename = os.path.splitext(os.path.basename(filename))[0] + new_ext
if os.path.realpath(output_filename) == os.path.realpath(filename):
output_filename = os.path.splitext(os.path.basename(filename))[0] + '.1' + new_ext
subprocess.check_call(['ffmpeg', '-i', filename,
'-c:v', 'libx264',
'-preset', 'medium',
'-c:a', 'aac',
'-b:a', str(options.audio_bitrate),
'-movflags', '+faststart',
'-y', output_filename])
# Transcode video into H264 in MKV using ffmpeg
# $ ffmpeg-h264-mkv test0.avi test1.wmv
@main_function
def ffmpeg_h264_mkv(args):
op = optparse.OptionParser(usage='usage: %prog FILE...', option_list=[
# optparse.Option('--audio-bitrate', default='161k', help='AAC audio bitrate'),
])
(options, args) = op.parse_args(args)
if not args:
op.print_usage()
sys.exit(2)
ncpu = posix.sysconf('SC_NPROCESSORS_ONLN')
new_ext = '.mkv'
for filename in args:
output_filename = os.path.splitext(os.path.basename(filename))[0] + new_ext
if os.path.realpath(output_filename) == os.path.realpath(filename):
output_filename = os.path.splitext(os.path.basename(filename))[0] + '.1' + new_ext
subprocess.check_call(['ffmpeg', '-i', filename,
'-c:v', 'libx264',
'-preset', 'slow',
#'-crf', '23',
#'-strict', 'experimental', # for AAC encoding
#'-c:a', 'aac',
#'-b:a', str(options.audio_bitrate),
'-c:a', 'copy',
'-threads', str(ncpu),
'-y', output_filename])
# Transcode video into WebM 360p format using ffmpeg
# $ ffmpeg-webm-360p f0.mov
@main_function
def ffmpeg_webm_360p(args):
op = optparse.OptionParser(usage='usage: %prog FILE...', option_list=[
])
(options, args) = op.parse_args(args)
if not args:
op.print_usage()
sys.exit(2)
ncpu = posix.sysconf('SC_NPROCESSORS_ONLN')
new_ext = '.webm'
for filename in args:
output_filename = os.path.splitext(os.path.basename(filename))[0] + new_ext
if os.path.realpath(output_filename) == os.path.realpath(filename):
output_filename = os.path.splitext(os.path.basename(filename))[0] + '.1' + new_ext
subprocess.check_call(['ffmpeg', '-i', filename,
'-vpre', 'libvpx-360p',
'-b', '380k',
'-threads', str(ncpu),
'-y', output_filename])
def main_function_dispatch(name, args):
try:
f = main_function_map[name]
except KeyError:
sys.stderr.write('%s is not a valid command name\n' % (name,))
sys.exit(2)
f(args)
def program_name():
return os.path.basename(sys.argv[0])
if __name__ == '__main__':
main_function_dispatch(program_name(), sys.argv[1:])