-
Notifications
You must be signed in to change notification settings - Fork 4
/
video_convert.py
executable file
·138 lines (114 loc) · 4.69 KB
/
video_convert.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
#! /usr/bin/env python3
import os, os.path, sys, json, re, subprocess
def create_video_filter(orig_file_name):
# use ffprobe to get the width of the original file
result = subprocess.run(['ffprobe', '-show_streams', '-select_streams', 'v', '-of', 'json', orig_file_name], stdout=subprocess.PIPE)
print('result.stdout => %s' % result.stdout)
file_data = json.loads(result.stdout)
print('file_data => %s' % file_data)
width = file_data['streams'][0]['width']
# make sure the width is an even number
if (width % 2):
width -= width
scale_width = width # the width of the original file rounded down to be even
scale_height = 'trunc(ow/a/2)*2' # keep the same aspect ratio but make sure the height is also and even number
video_filter = '-vf "scale=%s:%s"' % (scale_width, scale_height)
return video_filter
def create_new_file_name(orig_file_name):
# check to see if search/replace parameters have been passed
if ('file-search' in params.keys() and 'file-replace' in params.keys()):
s = params['file-search']
p = re.compile(s)
print('p => %s' % p.pattern)
replace = params['file-replace']
result = re.sub(p, replace, orig_file_name)
# replace spaces with hyphens
file_name = result.replace(' ', '-')
else:
file_name = orig_file_name
if '.mp4' not in result:
file_name = file_name.replace(result.split('.')[-1], 'mp4')
pass
return file_name
def convert_file(orig_file_name):
orig_file_ext = orig_file_name.split('.')[-1]
print("\n\n\n\n")
print("Starting to convert %s..." % orig_file_name)
new_file_name = create_new_file_name(orig_file_name)
print("new_file_name: %s" % new_file_name)
# create the video filter
video_filter = create_video_filter(orig_file_name)
# create the command to convert the file
command = 'ffmpeg -i "%s" %s -acodec aac -strict -2 -ab %sk -ar 44100 -vcodec h264 -vb %sK -y -movflags +faststart "%s" ' % (orig_file_name, video_filter, ffmpeg_args['ab'], ffmpeg_args['vb'], new_file_name)
command = "time %s" % command
print('convert command: %s' % command)
# convert the file
exit_code = os.system(command)
if (exit_code == 0):
# move the original file
orig_file_new_name = orig_file_name.replace('.%s' % orig_file_ext, '_%s' % orig_file_ext)
move_command = 'mv "%s" "%s"' % (orig_file_name, orig_file_new_name)
os.system(move_command)
print("\n")
print("%s created and original file moved to %s" % (new_file_name, orig_file_new_name))
else:
os.system('rm "%s"' % (new_file_name))
print("Converting %s failed, please try again." % orig_file_name)
pass
def output_help():
data = {
'script-name': sys.argv[0].split('/')[-1], # script name
'slash_one': '%s%s' % ('\\', '1'),
}
print("""
Video Converter
Usage:
Single file
%(script-name)s {options} file_name.avi
Batch processing of files
%(script-name)s {options} *.avi
Batch processing of files with regex name replacement - example
%(script-name)s *.avi --file-search='Castle\.2009\.(.*)\.HDTV.XviD-LOL.*' --file-replace='Castle-(2009).%(slash_one)s.mp4'
Options:
-ab=128 Set ffmpeg audio bitrate (128)kbps
-vb=800 Set ffmpeg video bitrate (800)kbps
--file-search Regular Epression Pattern for file name change - remember case counts
--file-replace replacement pattern for file name change - for groupings use %(slash_one)s instead of $1
--test-replace returns a list of the potential file names from the --file-replace argument
""" % data)
return
def simulate_file_name_replace(files_to_convert):
for file_to_convert in files_to_convert:
print("%s => %s" % (file_to_convert, create_new_file_name(file_to_convert)))
pass
if __name__ == "__main__":
ffmpeg_args = {
'ab': 128, # audio bitrate is 128kbps by default
'vb': 800, # video bitrate is 800K by default
}
params = {}
args = sys.argv[1:]
files_to_convert = []
for arg in args:
if ('--' in arg):
param_name = arg.split('--')[1].split('=')[0]
if (param_name == 'help'):
output_help()
exit()
if (len(arg.split('=')) > 1):
param = arg.split('=')[1]
else:
param = 1
params[param_name] = param
elif (arg[0] == '-'):
# this is an argument not a file to convert
ffmpeg_args[arg.split('-')[1].split('=')[0]] = arg.split('=')[1]
elif (os.path.isfile(arg)) :
# we have confirmed that the argument is a file
files_to_convert.append(arg)
print("ffmpeg_args: %s" % ffmpeg_args)
if ('test-replace' in params.keys()):
simulate_file_name_replace(files_to_convert)
exit()
for file_to_convert in files_to_convert:
convert_file(file_to_convert)