-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_change.py
104 lines (90 loc) · 3.37 KB
/
codec_change.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
import os
def compute(select, in_file, out_file, codec):
# os.system('ffmpeg -i ' + in_file + ' -c:v libvpx-vp9 -b:v 2M -pass 1 -an -f null /dev/null && '
# 'ffmpeg -i ' + in_file + ' -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus ' + out_file)
# Change the video/audio codec indicated from the user.
if select:
os.system('ffmpeg -i ' + in_file + ' -c:v ' + codec + ' ' + out_file)
else:
os.system('ffmpeg -i ' + in_file + ' -c:a ' + codec + ' ' + out_file)
def change():
# Ask the codec type to be changed.
while True:
try:
sel = int(input('Which type of codec you want to change? 1: Audio, 2: Video \n'))
except ValueError: # just catch the exceptions you know!
print('That\'s not a number!')
else:
if 1 <= sel <= 2: # this is faster
break
else:
print('Out of range. Try again')
print("\nSelect the video you want to resize:")
# Create aux variables.
direc = '.'
allfiles = os.listdir(direc)
aux = 1
names = []
# Plot all the videos in the current directory.
for f_name in allfiles:
if f_name.endswith('.mp4'):
names.append(f_name)
print(aux, f_name)
aux = aux + 1
# Ask the user which video use.
while True:
try:
x = int(input('Pick a video >>> '))
except ValueError: # just catch the exceptions you know!
print('That\'s not a number!')
else:
if 1 <= x < len(names): # this is faster
break
else:
print('Out of range. Try again')
in_name = names[int(x)-1]
# Show the user the possible codecs (depending on audio or video).
vid_codecs = ['vp3', 'vp8', 'vp9', 'av1']
aud_codecs = ['mp3', 'vorbis', 'libopus']
print('Introduce the codec you want to use: ')
aux = 1
# Ask the user which codec use.
if sel == 1:
selector = True
for f_name in aud_codecs:
print(aux, f_name)
aux = aux + 1
while True:
try:
x = int(input('Select codec >>> '))
except ValueError: # just catch the exceptions you know!
print('That\'s not a number!')
else:
if 1 <= x <= len(aud_codecs): # this is faster
break
else:
print('Out of range. Try again')
codec = aud_codecs[int(x) - 1]
else:
selector = True
for f_name in vid_codecs:
print(aux, f_name)
aux = aux + 1
while True:
try:
x = int(input('Select codec >>> '))
except ValueError: # just catch the exceptions you know!
print('That\'s not a number!')
else:
if 1 <= x <= len(vid_codecs): # this is faster
break
else:
print('Out of range. Try again')
codec = vid_codecs[int(x) - 1]
# Ask the user the output file name (with the extension!).
print('Introduce the new video name (remember to add the extension, e.g. if you want to change the audio codec to '
'mp3, output name: MP3_codec_change.mp4) >>> ')
out_name = input()
compute(selector, in_name, out_name, codec)
if __name__ == '__main__':
change()