-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo-convert
More file actions
executable file
·237 lines (205 loc) · 8.5 KB
/
Copy pathvideo-convert
File metadata and controls
executable file
·237 lines (205 loc) · 8.5 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
#!/usr/bin/python
import re
import sys
import time
import os.path
import optparse
import subprocess
def getCommandOutput( cmd, verbose ):
if verbose:
print "Going to execute command [%s]" % (cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
result = []
for line in p.stdout.readlines():
result.append(line.rstrip())
return result
if __name__ == "__main__":
parser = optparse.OptionParser("Usage: video-convert [OPTIONS] <filename> [OPTIONS]")
parser.add_option('-b',
'--bitrate',
type='string',
dest='bitrate',
help="Set the new bitrate (can be an pure number or a number followed by 'k', 'K', 'm', 'M') or a percentage")
parser.add_option('-s',
'--scale',
type='string',
dest='scale',
help="Specifies the scale ratio for the resolution; can be a floating number greater than 0 and less or equal to 1, or an integer between 1 and 100 followed by '%'")
parser.add_option('-a',
'--remove-audio',
action="store_true",
dest='removeaudio',
default=False,
help='Remove autio track')
parser.add_option('-v',
'--verbose',
action="store_true",
dest='verbose',
default=False,
help='Prints out useful debug information')
parser.add_option('-i',
'--info',
action="store_true",
dest='info',
default=False,
help='Prints out size and bitrate of the video')
parser.add_option('-O',
'--overwrite',
action="store_true",
dest='overwrite',
default=False,
help='Overwrite output file if already exists')
parser.add_option('-P',
'--ffmpeg-path',
type='string',
dest='ffmpeg',
default='/usr/local/bin/ffmpeg',
help='Alternative path of ffmpeg binary')
parser.add_option('-p',
'--ffprobe-path',
type='string',
dest='ffprobe',
default='/usr/local/bin/ffprobe',
help='Alternative path of ffprobe binary')
parser.add_option('-o',
'--output-file',
type='string',
dest='outfile',
help='Alternative path for output file')
parser.add_option('-T',
'--T2',
action="store_true",
dest='applet2',
default=False,
help='Use Apple\'s T2 encoder chip instead of software h264')
parser.add_option('-5',
'--h265',
action="store_true",
dest='h265',
default=False,
help='Use h265 encoder instead of h264')
(options, args) = parser.parse_args()
if len(args) == 0:
print "Must specify mandatory argument <filename>. Stop!\n"
sys.exit(1)
filename = args[0]
bitrate = str(options.bitrate)
percent = 0.5
realbitrate = 0;
scale = str(options.scale)
realscale = 1.0
verbose = options.verbose
infomode = options.info
overwrite = options.overwrite
removeaudio = options.removeaudio
ffmpeg = options.ffmpeg
ffprobe = options.ffprobe
if os.path.exists(ffmpeg) == False or os.access(ffprobe, os.X_OK) == False:
print "\nCannot find %s or it is not executable. Stop." % (ffmpeg)
sys.exit(1)
if os.path.exists(ffprobe) == False or os.access(ffprobe, os.X_OK) == False:
print "\nCannot find %s or it is not executable. Stop." % (ffprobe)
sys.exit(1)
if os.path.isfile(filename) == False or os.access(filename, os.R_OK) == False:
print "\nCannot find %s or it is not readable. Stop" % (filename)
sys.exit(1)
#infocmd = "%s -v quiet -select_streams v:0 -show_entries stream=width,height,bit_rate -of default=nw=1:nk=1 %s" % (ffprobe, filename)
infocmd = "%s -v quiet -select_streams v:0 -show_entries stream=width,height,bit_rate,codec_name,r_frame_rate -show_entries format=size -of default=nw=1:nk=1 \"%s\"" % (ffprobe, filename)
result = getCommandOutput(infocmd, verbose)
if len(result) == 0:
print "No video Info. Stop."
sys.exit(0)
C = str(result[0])
H = int(result[2])
W = int(result[1])
FR= str(result[3])
R = int(result[4])
S = int(result[5])
a,b = FR.split('/')
fr = int(a)/int(b)
if infomode:
print "Height: %d, Width: %d, bitrate: %d, codec: %s, size: %d bytes, framerate: %d fps" % (H,W,R,C,S,fr)
sys.exit(0)
if options.bitrate:
a = re.match(r'^[0-9]+[kKmM]$', bitrate)
b = re.match(r'^[0-9]+$',bitrate)
c = re.match(r'^[0-9]{1,2}%$',bitrate)
if (not a) and (not b) and (not c):
print "Bitrate must be in the form of integer (e.g. '123456') or integer followed by unit of measure for kibibytes or megabytes (e.g. '1000k', '1M') or a percentage (e.g. '50%'). Stop"
sys.exit(1)
if options.scale:
a = re.match(r'^[0-9]+%$', scale)
b = re.match(r'^[01](\.[0-9])?$', scale)
if (not a) and (not b):
print "Scale must be of the form <integer-number>%, or a <floating-number> between 0 and 1. Stop."
sys.exit(1)
if options.bitrate:
if re.match(r'^([0-9]+)%$', bitrate):
percent = bitrate
percent = float(percent.rstrip('%'))
percent /= 100
realbitrate = int(R*percent)
else:
a = re.match(r'^([0-9]+)[kK]$', bitrate)
b = re.match(r'^([0-9]+)[mM]$', bitrate)
c = re.match(r'^([0-9]+)$', bitrate)
if a:
realbitrate = int(a.group(1)) * 1024
if b:
realbitrate = int(b.group(1)) * 1048576
if c:
realbitrate = int(c.group(1))
if options.scale:
a = re.match(r'^([0-9]+)%$', scale)
if a:
realscale = float(a.group(1))/100
else:
realscale = float(scale)
if realscale > 1:
print "Warning: specified scale factor >1 (or >100%). Resetting to 100%."
realscale = 1
if realscale == 0:
print "Warning: specified scale factor equal to 0. Resetting to 100%."
realscale = 1
H = H * realscale
W = W * realscale
if realbitrate > R:
print "Specified up-sampling (bitrate is greater than the original one). Stop."
sys.exit(1)
if realbitrate == 0 and options.bitrate:
print "Specified zero bitrate. Resetting it to the original value %d." % (R)
realbitrate = R
ra = ''
if removeaudio:
ra ='-an'
outfile = "%s-converted.mp4" % (filename)
if options.outfile:
outfile = options.outfile
if os.path.exists(outfile) == True:
if overwrite == False:
print "Output file %s already exists. Please use --overwrite to overwrite it. Stop." % (outfile)
sys.exit(1)
rb = ''
if options.bitrate:
rb = "-b:v %d" % (realbitrate)
rs = ''
if options.scale:
rs = "-vf scale=%d:%d" % (W, H)
overwrite = ''
if options.overwrite:
overwrite = '-y'
if options.applet2 and options.h265:
print "Please only one of the two option --t2, --h265"
sys.exit(1)
encoder = "-c:v libx264"
if options.applet2:
encoder = "-c:v hevc_videotoolbox -vtag hvc1"
if options.h265:
encoder = "-c:v libx265"
ffmpegcmd = "%s %s %s -i %s %s %s %s %s >/dev/null 2>&1" % (ffmpeg, ra, overwrite, filename, rs, encoder, rb, outfile)
millisb = int(round(time.time() * 1000))
res = getCommandOutput( ffmpegcmd, verbose )
millisa = int(round(time.time() * 1000))
size = os.stat(filename).st_size
print "Finished conversion. Output file is %s" % (outfile)
print "Conversion time millis: %d, %.2f bytes/s" % ( (millisa-millisb), (1000*size)/(millisa-millisb))