-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscode-video.py
216 lines (196 loc) · 10.2 KB
/
transcode-video.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
import sys, getopt, shutil, configparser, os, subprocess
from libs import fileInfo as fi
from libs.videoInput import videoInput as vinput
from libs.subs import subsBackup
from os import path
############### CONFIG ##########################
config = configparser.ConfigParser()
config.read(os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),'config/config.ini')))
opts = {}
for section in config.sections():
for key, val in config.items(section):
try: # Try int
exec('opts["' + key + '"] = ' + val)
except: # So str
exec('opts["' + key + '"] = "' + val + '"')
# Try bolean
if val.lower() in ('true', 'false'): exec('opts["' + key + '"] = ' + val.title())
output_path = fi.checkDir(opts['output_path'])
log_path = fi.checkDir(opts['log_path'])
temp_path = fi.checkDir(opts['temp_path'])
############# END CONFIG ########################
videoInput = None
sample = False
def main():
global sample
try:
inpts, args = getopt.getopt(sys.argv[1:],"hi:s",["help","input=","sample"])
if len(inpts) == 0:
print('No Input provided')
__usage(2)
except getopt.GetoptError:
print ('Invalid input ', sys.argv[1:] )
__usage(2)
for opt, arg in inpts:
if opt in ("-h", "--help"):
__usage(0)
elif opt in ("-i", "--input"):
input = arg
elif opt in ("-s", "--sample"):
sample = True
__loadVideoInput(input)
__runTranscode()
__moveOutput()
if opts['backup_subs']:
subsBackup(videoInput)
def __loadVideoInput(inputfile):
global videoInput
videoInput = vinput(inputfile)
def __runTranscode():
# Ideally find a way to pass StingIO file direct to ffmpeg...? but for now
global metadata
metadata = path.join(temp_path, videoInput.title + '.met')
with open(metadata, 'w') as met:
print(videoInput.info.chapters.read(), file=met)
cmd = __createFFMPEGcmd()
with open(log_path + videoInput.ofilename + '.cmd.log', "w") as log_file:
print (cmd, file=log_file)
print ('Running transcode job')
with open(log_path + videoInput.ofilename + '.transcode.log', 'w') as log_file:
subprocess.call(cmd, stderr=subprocess.STDOUT, stdout=log_file, shell=True)
print('Process complete')
if opts['delete_temp']: shutil.rmtree(temp_path)
def __createFFMPEGcmd():
cmd = []
cmd.append(__baseFFMPEGcmd())
cmd.append(__hdFFMPEGcmd())
if videoInput.uhd: cmd.append(__uhdFFMPEGcmd())
return ' '.join(cmd)
def __baseFFMPEGcmd():
cmd = ['ffmpeg ' + opts['ffmpeg_options']]
cmd.append('-loglevel ' + opts['ffmpeg_loglevel'])
cmd.append('-probesize ' + opts['ffmpeg_prob_anal'])
cmd.append('-analyzeduration ' + opts['ffmpeg_prob_anal'])
# Seems to be an issue having metadata as second input, seems to only work on first?
cmd.append('-i "' + metadata + '"')
if sample: cmd.append(opts['sample_time'])
cmd.append('-forced_subs_only ' + str(opts['ffmpeg_forced_subs_only']))
cmd.append(opts['hw_accel'])
cmd.append('-i ' + videoInput.input)
return ' '.join(cmd)
def __hdFFMPEGcmd():
cmd = ['-map_metadata 0']
# TODO wont work, but gone with subs backup instead and adding as subs track instead of buring on
if opts['burn_subs']:
cmd.append('-filter_complex " [1:' + videoInput.info.vstream + ']pad=1920:1080[main];[main][1:s:' + str(opts['sub_stream']) + ']overlay[v]" -map [v]')
else:
cmd.append('-map 1:' + videoInput.info.vstream)
if videoInput.hdr.exists: cmd.append(videoInput.hdr.sdrmap)
cmd.append('-c:v ' + opts['hd_codec'])
if not opts['x264_level'] == '': cmd.append('-level:v ' + str(opts['x264_level']))
cmd.append('-crf ' + str(opts['hd_crf']))
if not opts['hd_maxrate'] == '':
cmd.append('-maxrate ' + str(opts['hd_maxrate']) + 'k')
cmd.append('-bufsize ' + str(opts['hd_maxrate']*3) + 'k')
cmd.append('-preset ' + opts['hd_preset'])
if not videoInput.x264_tune == '': cmd.append('-tune ' + videoInput.x264_tune)
cmd.append('-s ' + opts['hd_size'])
cmd.append('-pix_fmt ' + opts['hd_pix_fmt'])
aos = '0'
if opts['add_stereo']:
if opts['stereo_primary']:
cmd.append('-filter_complex "[1:' + videoInput.info.astream + ']volume=2.5:precision=fixed[a]" -map [a] -c:a:' + aos + ' aac -b:a:' + aos + ' 320k -ac:a:' + aos + ' 2 -metadata:s:a:' + aos + ' title="Stereo" -metadata:s:a:' + aos + ' handler="Stereo"')
aos = '1'
else:
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' ' + opts['hd_acodec'] + ' -b:a:' + aos + ' 640k -metadata:s:a:' + aos + ' title="Surround 5.1" -metadata:s:a:' + aos + ' handler="Surround 5.1"')
aos = '1'
cmd.append('-filter_complex "[1:' + videoInput.info.astream + ']volume=2.5:precision=fixed[a]" -map [a] -c:a:' + aos + ' aac -b:a:' + aos + ' 320k -ac:a:' + aos + ' 2 -metadata:s:a:' + aos + ' title="Stereo" -metadata:s:a:' + aos + ' handler="Stereo"')
else:
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' ' + opts['hd_acodec'] + ' -b:a:' + aos + ' 640k -metadata:s:a:' + aos + ' title="Surround 5.1" -metadata:s:a:' + aos + ' handler="Surround 5.1"')
cmd.append('-metadata:s:a language=eng')
cmd.append('-metadata title="' + videoInput.title + '" -metadata date="' + str(videoInput.year) + '"')
global hd_output
hd_output = output_path + videoInput.ofilename + '.'
if videoInput.hdr.exists: hd_output = hd_output + 'SDR.'
if sample: hd_output = hd_output + 'sample.'
hd_output = hd_output + opts['hd_ext']
cmd.append('"' + hd_output + '" -y')
return ' '.join(cmd)
def __uhdFFMPEGcmd():
cmd = ['-map_metadata 0 -map 1:' + videoInput.info.vstream + ' -c:v libx265']
cmd.append('-preset ' + opts['uhd_preset'])
if not opts['uhd_pix_fmt'] == '': videoInput.info.pix_fmt = opts['uhd_pix_fmt']
cmd.append('-pix_fmt ' + videoInput.info.pix_fmt + ' -x265-params')
params = 'crf=' + str(opts['uhd_crf'])
if not opts['uhd_maxrate'] == '' :
params = params + ':vbv-maxrate=' + str(opts['uhd_maxrate'])
params = params + ':vbv-bufsize=' + str(opts['uhd_maxrate']*3)
if videoInput.hdr.exists: params = params + ':' + videoInput.hdr.params
cmd.append(params)
# Add audio based on user codec preferences
aos = '0'
atmos_ext = ''
# If copy atmos is set by user and atmos exists copy it
if videoInput.atmos.exists and opts['uhd_copy_atmos']:
atmos_ext = 'Atmos.'
if opts['uhd_atmos_primary']:
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' copy -max_muxing_queue_size 4096')
cmd.append('-metadata:s:a:' + aos + ' title="Dolby Atmos" -metadata:s:a:' + aos + ' handler="Dolby Atmos"')
aos = '1'
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' ' + opts['uhd_acodec'] + ' -b:a:0 640k')
cmd.append('-metadata:s:a:0 title="Surround 5.1" -metadata:s:a:' + aos + ' handler="Surround 5.1"')
else:
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' ' + opts['uhd_acodec'] + ' -b:a:0 640k')
cmd.append('-metadata:s:a:0 title="Surround 5.1" -metadata:s:a:' + aos + ' handler="Surround 5.1"')
aos = '1'
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' copy -max_muxing_queue_size 4096')
cmd.append('-metadata:s:a:' + aos + ' title="Dolby Atmos" -metadata:s:a:' + aos + ' handler="Dolby Atmos"')
aos = '2'
else:
cmd.append('-map 1:' + videoInput.info.astream + ' -c:a:' + aos + ' copy -max_muxing_queue_size 4096')
cmd.append('-metadata:s:a:' + aos + ' title="Dolby Atmos" -metadata:s:a:' + aos + ' handler="Dolby Atmos"')
aos = '1'
if opts['uhd_add_stereo']:
cmd.append('-filter_complex "[1:' + videoInput.info.astream + ']volume=2.5:precision=fixed[a]" -map [a] -c:a:' + aos + ' aac -b:a:' + aos + ' 320k -ac:a:' + aos + ' 2 -metadata:s:a:' + aos + ' title="Stereo" -metadata:s:a:' + aos + ' handler="Stereo"')
cmd.append('-metadata:s:a language=eng')
cmd.append('-metadata title="' + videoInput.title + '" -metadata year="' + str(videoInput.year) + '"')
global uhd_output
uhd_output = output_path + videoInput.ofilename + '.UHD.'
if videoInput.hdr.exists: uhd_output = uhd_output + 'HDR.'
uhd_output = uhd_output + atmos_ext
if sample: uhd_output = uhd_output + 'sample.'
uhd_output = uhd_output + opts['uhd_ext']
cmd.append('"' + uhd_output + '" -y')
return ' '.join(cmd)
def __moveOutput():
if opts['hd_move_output']:
hd_folder = videoInput.mdb.__dict__[opts['hd_folder_var']] if opts['hd_folder_var'] else ''
hd_move_to = os.path.realpath(os.path.join(opts['hd_base_dir'],hd_folder))
print('Moving HD to:= ', hd_move_to)
try:
fi.checkDir(hd_move_to)
shutil.move(hd_output, hd_move_to)
with open(log_path + videoInput.ofilename + '.cmd.log', "a") as log_file:
print('HD version moved to:= ', hd_move_to, file=log_file)
except:
print('something went wrong trying to move it')
if opts['uhd_move_output'] and videoInput.uhd:
uhd_folder = videoInput.mdb.__dict__[opts['uhd_folder_var']] if opts['uhd_folder_var'] else ''
uhd_move_to = os.path.realpath(os.path.join(opts['uhd_base_dir'],uhd_folder))
print('Moving UHD to:= ', uhd_move_to)
try:
fi.checkDir(uhd_move_to)
shutil.move(uhd_output, uhd_move_to)
with open(log_path + videoInput.ofilename + '.cmd.log', "a") as log_file:
print('UHD version moved to:= ', uhd_move_to, file=log_file)
except:
print('something went wrong trying to move it')
def __usage(exit_code):
script = os.path.basename(__file__)
print('Correct Usage:')
print (script, '-h[--help] Show this help')
print (script, '-i[--input] <inputfile> -s[--sample] (optional)')
print ('NB sample time is a setting in config.ini')
exit(exit_code)
if __name__ == "__main__":
main()