-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
204 lines (171 loc) · 6.43 KB
/
main.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
# -*- coding: utf-8
import subprocess
import re
import os, sys, getopt
import progressbar
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
this_file = os.path.abspath(this_file)
if getattr(sys,'frozen', False):
basepath = getattr(sys,'_MEIPASS', os.path.dirname(sys.executable))
else:
basepath = os.path.dirname(this_file)
"""
"""
presets = ['ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', 'slow', 'slower', 'veryslow', 'placebo']
preset = presets[5]
vprofiles = ['baseline','main', 'high', 'high10', 'high422', 'high444']
vprofile = vprofiles[1]
# profile baseline = level = 3.0
levels = ['1.3', '3.0', '3.1', '3.2', '4.0', '4.1', '4.2', '5.0', '5.1', '5.2'] # 1.3 seulement avec profil 'baseline'
level = [6]
debitVideo = '1600k'
codecsVideo = ['libx264']
debitAudio = '160k'
codecAudio = 'aac'
scale = '-2:-2'
crop = ''
container = 'mp4'
simulate = [] # ['-ss', '00:30:00', '-t', '00:30']
def time2secs(duration):
heures, minutes, secondes = duration.split(':')
total = 0
total += int(heures) * 3600
total += int(minutes) * 60
total += float(secondes)
return total
def getCrop(filename,position = 5):
if os.name in ("nt", "dos", "os2", "ce"):
destNull = 'NUL'
elif sys.platform == "darwin":
destNull = '/dev/null'
command = [FFMPEG_PATH, '-ss', '{}'.format(position), '-y', '-i', filename,
'-t', '3', '-vf', 'cropdetect=24:16:0', '-an', '-f', 'mp4',
destNull]
p = subprocess.Popen(command, stderr=subprocess.PIPE, universal_newlines=True)
while True:
chunk = p.stderr.readline()
if chunk == '':
break
m = re.search("crop=(?P<crop>\S+)", chunk)
if m is not None:
return 'crop={}'.format(m.group('crop'))
break
return ""
def getDuration(filename):
command = [FFPROBE_PATH, '-i', filename, '-show_format']
p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
while True:
#chunk = p.stderr.readline()
chunk = p.stdout.readline()
if chunk == '':
break
m = re.search("duration=(?P<duration>\S+)", chunk)
if m is not None:
try:
return float(m.group('duration'))
except Exception as e:
return 0.0
break
return 0.0
def encoding(infile, outfile, codecVideo=codecsVideo[0], preset = presets[5], vprofile=vprofiles[1], level=levels[6], simulate = []):
print('codec = {} vprofile = {} level = {} simulate = {}'.format(codecVideo, vprofile, level, simulate))
cmd = [FFMPEG_PATH]
if simulate:
cmd += simulate
vf = ['scale='+scale]
if crop != '':
vf += ['crop={}'.format(crop)]
vf += ['yadif=0:-1:0']
cmd += ['-y', '-i', infile]
cmd += ['-vf', ','.join(vf), '-c:v', codecVideo]
cmd += ['-map', '0:0']
cmd += ['-b:v', debitVideo]
cmd += ['-preset', preset, '-profile:v', vprofile, '-pix_fmt', 'yuv420p']
cmd += ['-map', '0:1', '-c:a', codecAudio, '-b:a', debitAudio, '-ac', '2']
cmd += ['-async', '1']
cmd += ['-f', container]
cmd += [outfile]
cli(cmd, infile)
return cmd
def cli(cmd, filename=''):
widget = ['Encodage',progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA()] #, ' ', filename]
bar = progressbar.ProgressBar(widgets=widget)
p = subprocess.Popen(cmd, stderr=subprocess.PIPE, universal_newlines=True)
while True:
chunk = p.stderr.readline()
if chunk == '':
break
m = re.search("Duration: (?P<time>\S+),", chunk)
if m is not None:
durationTotal = time2secs(m.group('time'))
bar.max_value = durationTotal
m = re.search("time=(?P<time>\S+)", chunk)
if m is not None:
d = time2secs(m.group('time'))
# print(d/durationTotal, m.group('time'))
try:
bar.update(min(d,durationTotal))
except Exception as e:
print(e)
if re.search("Conversion failed!", chunk) is not None:
print("Erreur de conversion pour le fichier {}".format(filename))
print(' '.join(cmd))
FFMPEG_PATH = 'ffmpeg'
FFPROBE_PATH = 'ffprobe'
if os.name in ("nt", "dos", "os2", "ce"):
FFMPEG_PATH = os.path.join(basepath, 'plugin\\ffmpeg.exe')
FFPROBE_PATH = os.path.join(basepath, 'plugin\\ffprobe.exe')
codecsVideo = ['libx264', 'h264_amf', 'h264_nvenc', 'h264_qsv', 'nvenc', 'nvenc_h264']
codecVideo = codecsVideo[0]
elif sys.platform == 'darwin':
FFMPEG_PATH = os.path.join(basepath, 'plugin/ffmpeg')
FFPROBE_PATH = os.path.join(basepath, 'plugin/ffprobe')
# ./ffmpeg -h encoder=h264_videotoolbox
# ./ffmpeg -encoders | grep 264
codecsVideo = ['libx264', 'h264_videotoolbox', 'libopenh264']
codecVideo = codecsVideo[0]
elif sys.platform == 'linux':
codecsVideo = ['libx264', 'h264_omx', 'h264_v4l2m2m', 'h264_vaapi']
codecVideo = codecsVideo[0]
infile = ''
outfile = ''
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:o:c:v:a:", ['help', 'inputfile=', 'outputfile=', 'getcrop=', 'codecvideo='])
except getopt.GetoptError as e:
print(e)
sys.exit(2)
for opt, arg in opts:
if opt in('--inputfile', '-i'):
infile = arg
elif opt in ('--outputfile', '-o'):
outfile = arg
elif opt in ('--getcrop', '-c'):
print(getCrop(arg, position=int(getDuration(arg)/2)))
sys.exit()
elif opt in ('--codecvideo', '-v'):
if arg in codecsVideo:
codecVideo = arg
else:
codecVideo = codecsVideo[0]
print("'{}' n'existe pas, le codec '{}' sera utilisé".format(arg, codecVideo))
elif opt in ('--help', '-h'):
print('pyencode')
print('========')
print('')
print('Usage: pyencode.py -i source -o destination')
sys.exit()
infile = '/Users/gilles/Movies/video.ts'
outfile = '/Users/gilles/Movies/video.mp4'
if os.path.exists(infile):
if outfile=='':
outfile = os.path.splitext(infile)[0]+'_{}_{}.{}'.format(codecVideo, codecAudio, container)
encoding(infile, outfile, codecVideo = codecVideo)
else:
if infile != '':
print("Le fichier '{}' n'existe pas".format(infile))
else:
print('pyencode')