-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathaudiotranscoder
executable file
·118 lines (89 loc) · 2.93 KB
/
audiotranscoder
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
#!/usr/bin/python3
"""
audiotranscode
Copyright (c) 2013 Tom Wallroth
Sources on github:
http://github.com/devsnd/audiotranscode/
licensed under GNU GPL version 3 (or later)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
"""
import audiotranscode
import sys
import os
EXE = os.path.basename(__file__)
USAGE_TEXT = """audiotranscode version %s
usage:
%s [-b BITRATE] <input> <output>
%s -h | --help
%s --codecs
""" % (audiotranscode.__version__, EXE, EXE, EXE)
HELP_TEXT = """
%s [-b BITRATE] <input> <output>
bitrate is specified in kbits/s
<input> is the path to the file that needs to be transcoded
<output> is the output path. The file extension of the output determines
the new file format
%s -h | --help
shows this help text
%s --codecs
shows all installed codecs
""" % (EXE, EXE, EXE)
def usage():
"""print usage string"""
print(USAGE_TEXT)
sys.exit(1)
def list_codecs():
"""list all available encoders and decoders on the command line"""
transcode = audiotranscode.AudioTranscode()
row_format = "{:>10}" * 3
print('Encoders:')
print(row_format.format('ENCODER', 'INSTALLED', 'FILETYPE'))
for enc in transcode.Encoders:
avail = 'yes' if enc.available() else 'no'
print(row_format.format(enc.command[0], avail, enc.filetype))
print('Decoders:')
print(row_format.format('ENCODER', 'INSTALLED', 'FILETYPE'))
for dec in transcode.Decoders:
avail = 'yes' if dec.available() else 'no'
print(row_format.format(dec.command[0], avail, dec.filetype))
if len(sys.argv) == 1:
usage()
if '-h' in sys.argv or '--help' in sys.argv:
print(HELP_TEXT)
sys.exit(0)
if '--codecs' in sys.argv:
list_codecs()
sys.exit(0)
BITRATE = None
if '-b' in sys.argv:
if not len(sys.argv) == 5:
print('wrong number of arguments')
usage()
try:
BITRATE = int(sys.argv[sys.argv.index['-b']+1])
except ValueError:
print('bitrate must be a number')
usage()
else:
if not len(sys.argv) == 3:
print('wrong number of arguments')
usage()
IN_FILE = sys.argv[-2]
OUT_FILE = sys.argv[-1]
TRANSCODER = audiotranscode.AudioTranscode()
try:
TRANSCODER.transcode(IN_FILE, OUT_FILE, BITRATE)
except audiotranscode.TranscodeError as exc:
print(exc)
print('try the --codecs switch to see all installed codecs')
except IOError as exc:
print(exc)