forked from tuffy/python-audio-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackinfo
executable file
·210 lines (183 loc) · 8.96 KB
/
trackinfo
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
#!/usr/bin/python
#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2014 Brian Langenberger
#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 2 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, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os.path
import audiotools
import audiotools.text as _
if (__name__ == '__main__'):
import argparse
parser = argparse.ArgumentParser(description=_.DESCRIPTION_TRACKINFO)
parser.add_argument("--version",
action="version",
version="Python Audio Tools %s" % (audiotools.VERSION))
parser.add_argument("-n", "--no-metadata",
action="store_true",
dest="no_metadata",
default=False)
parser.add_argument("-L", "--low-level",
action="store_true",
dest="low_level",
default=False)
parser.add_argument("-b",
"--bitrate",
action="store_true",
dest="show_bitrate")
parser.add_argument("-%",
"--percentage",
action="store_true",
dest="show_percentage")
parser.add_argument("-C", "--channel-assignment",
action="store_true",
dest="channel_assignment")
parser.add_argument("filenames",
metavar="FILENAME",
nargs="+",
help=_.OPT_INPUT_FILENAME)
options = parser.parse_args()
msg = audiotools.Messenger("trackinfo", options)
for file in audiotools.open_files(options.filenames, messenger=msg):
length = int(file.seconds_length())
if (options.show_bitrate):
try:
msg.output(
_.LAB_TRACKINFO_BITRATE %
{'bitrate': ((os.path.getsize(file.filename) * 8) /
2 ** 10) / length,
'filename': audiotools.Filename(file.filename)})
except ZeroDivisionError:
msg.output(
_.LAB_TRACKINFO_BITRATE %
{'bitrate': 0,
'filename': audiotools.Filename(file.filename)})
elif (options.show_percentage):
try:
percentage = (float(os.path.getsize(file.filename) * 100) /
(file.total_frames() * file.channels() *
(file.bits_per_sample() / 8)))
msg.output(
_.LAB_TRACKINFO_PERCENTAGE %
{'percentage': int(round(percentage)),
'filename': audiotools.Filename(file.filename)})
except ZeroDivisionError:
msg.output(_.LAB_TRACKINFO_PERCENTAGE %
{'percentage': "0",
'filename': audiotools.Filename(file.filename)})
else:
msg.output(
_.LAB_TRACKINFO_ATTRIBS %
{"minutes": length / 60,
"seconds": length % 60,
"channels": file.channels(),
"rate": audiotools.khz(file.sample_rate()),
"bits": file.bits_per_sample(),
"filename": audiotools.Filename(file.filename)})
if (not options.no_metadata):
metadata = file.get_metadata()
if (not options.low_level):
if (metadata is not None):
msg.output(unicode(metadata))
msg.output(u"")
replay_gain = file.replay_gain()
if (replay_gain is not None):
table = audiotools.output_table()
msg.output(_.LAB_TRACKINFO_REPLAYGAIN)
row = table.row()
row.add_column(_.LAB_TRACKINFO_TRACK_GAIN, "right")
row.add_column(u" : ")
row.add_column(unicode(replay_gain.track_gain) + u" dB")
row = table.row()
row.add_column(_.LAB_TRACKINFO_TRACK_PEAK, "right")
row.add_column(u" : ")
row.add_column(u"%f" % (replay_gain.track_peak))
row = table.row()
row.add_column(_.LAB_TRACKINFO_ALBUM_GAIN, "right")
row.add_column(u" : ")
row.add_column(unicode(replay_gain.album_gain) + u" dB")
row = table.row()
row.add_column(_.LAB_TRACKINFO_ALBUM_PEAK, "right")
row.add_column(u" : ")
row.add_column(u"%f" % (replay_gain.album_peak))
for row in table.format(msg.output_isatty()):
msg.output(row)
msg.output(u"")
cuesheet = file.get_cuesheet()
if (cuesheet is not None):
msg.output(_.LAB_TRACKINFO_CUESHEET)
indexes = {} # track_number -> {index_number -> offset}
isrcs = {} # track_number -> ISRC unicode
index_numbers = set([])
for track in cuesheet.tracks():
track_indexes = {}
for index in track.indexes():
track_indexes[index.number()] = index.offset()
index_numbers.add(index.number())
indexes[track.number()] = track_indexes
track_isrc = track.ISRC()
if (track_isrc is not None):
isrcs[track.number()] = \
track_isrc.strip(chr(0)).decode('ascii',
'replace')
else:
isrcs[track.number()] = u""
index_numbers = list(index_numbers)
index_numbers.sort()
table = audiotools.output_table()
row = table.row()
row.add_column(u" ")
row.add_column(_.LAB_TRACKINFO_CUESHEET_TRACK, "right")
for i in index_numbers:
row.add_column(u" ")
row.add_column(
_.LAB_TRACKINFO_CUESHEET_INDEX % (i), "right")
row.add_column(u" ")
row.add_column(_.LAB_TRACKINFO_CUESHEET_ISRC, "right")
for t in sorted(indexes.keys()):
row = table.row()
row.add_column(u"")
row.add_column(unicode(t), "right")
track_indexes = indexes[t]
for i in index_numbers:
row.add_column(u"")
if (i in track_indexes):
index = track_indexes[i]
row.add_column(
audiotools.build_timestamp(
int(index * 75)).decode('ascii'))
else:
row.add_column(u"")
row.add_column(u"")
row.add_column(isrcs[i])
for row in table.format(msg.output_isatty()):
msg.output(row)
msg.output(u"")
else:
if (metadata is not None):
msg.output(metadata.raw_info())
msg.output(u"")
if (options.channel_assignment):
msg.output(_.LAB_TRACKINFO_CHANNELS)
channel_names = dict(
[(attr, audiotools.ChannelMask.MASK_TO_NAME[mask])
for (attr, mask) in
audiotools.ChannelMask.SPEAKER_TO_MASK.items()])
if (file.channel_mask().defined()):
for (i, channel) in enumerate(file.channel_mask().channels()):
msg.output(_.LAB_TRACKINFO_CHANNEL %
{"channel_number": i + 1,
"channel_name": channel_names[channel]})
else:
for i in xrange(file.channels()):
msg.output(_.LAB_TRACKINFO_CHANNEL %
{"channel_number": i + 1,
"channel_name": _.LAB_TRACKINFO_UNDEFINED})