forked from tuffy/python-audio-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracklength
executable file
·124 lines (103 loc) · 4.56 KB
/
tracklength
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
#!/usr/bin/python
#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2012 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
from decimal import Decimal
import audiotools
import audiotools.text as _
def audio_files(msg, args):
for audio_file in audiotools.open_files(filter(os.path.isfile, args),
messenger=msg,
warn_duplicates=True):
yield audio_file
for parent_dir in filter(os.path.isdir, args):
for audio_file in audiotools.open_directory(parent_dir,
sorted=False,
messenger=msg):
yield audio_file
class FormatSummary:
def __init__(self):
self.total_length = Decimal(0)
self.file_count = 0
self.total_size = 0
def add(self, audiofile):
self.total_length += audiofile.seconds_length()
self.file_count += 1
self.total_size += os.path.getsize(audiofile.filename)
def to_row(self, name, msg):
msg.new_row()
msg.output_column(name, True)
msg.output_column(u" ")
msg.output_column(unicode(self.file_count), True)
msg.output_column(u" ")
format_length = int(self.total_length)
msg.output_column(_.LAB_TRACKLENGTH %
{"hours": format_length / (60 * 60),
"minutes": (format_length / 60) % 60,
"seconds": format_length % 60},
True)
msg.output_column(u" ")
if (self.total_size > (2 ** 40)):
#terabytes
total_size = u"%sT" % \
((self.total_size /
Decimal(2 ** 40)).quantize(Decimal("1.0")))
elif (self.total_size > (2 ** 30)):
#gigabytes
total_size = u"%sG" % \
((self.total_size /
Decimal(2 ** 30)).quantize(Decimal("1.0")))
elif (self.total_size > (2 ** 20)):
#megabytes
total_size = u"%sM" % \
((self.total_size /
Decimal(2 ** 20)).quantize(Decimal("1.0")))
elif (self.total_size > (2 ** 10)):
#kilobytes
total_size = u"%sK" % \
((self.total_size /
Decimal(2 ** 10)).quantize(Decimal("1.0")))
else:
#bytes
total_size = unicode(self.total_size)
msg.output_column(total_size, True)
if (__name__ == '__main__'):
parser = audiotools.OptionParser(
usage=_.USAGE_TRACKLENGTH,
version="Python Audio Tools %s" % (audiotools.VERSION))
(options, args) = parser.parse_args()
msg = audiotools.Messenger("tracklength", options)
format_summaries = {}
total_summary = FormatSummary()
for audio_file in audio_files(msg, args):
if (audio_file.NAME not in format_summaries.keys()):
format_summaries[audio_file.NAME] = FormatSummary()
format_summaries[audio_file.NAME].add(audio_file)
total_summary.add(audio_file)
if (total_summary.file_count > 0):
msg.new_row()
msg.output_column(_.LAB_TRACKLENGTH_FILE_FORMAT, True)
msg.output_column(u" ")
msg.output_column(_.LAB_TRACKLENGTH_FILE_COUNT, True)
msg.output_column(u" ")
msg.output_column(_.LAB_TRACKLENGTH_FILE_LENGTH, True)
msg.output_column(u" ")
msg.output_column(_.LAB_TRACKLENGTH_FILE_SIZE, True)
msg.divider_row([u"-", u" ", u"-", u" ", u"-", u" ", u"-"])
for name in sorted(format_summaries.keys()):
format_summaries[name].to_row(name.decode('ascii'), msg)
if (len(format_summaries.keys()) > 1):
msg.divider_row([u"-", u" ", u"-", u" ", u"-", u" ", u"-"])
total_summary.to_row(_.LAB_TRACKLENGTH_FILE_TOTAL, msg)
msg.output_rows()