-
Notifications
You must be signed in to change notification settings - Fork 0
/
procdigs
executable file
·113 lines (94 loc) · 2.9 KB
/
procdigs
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
#!/usr/bin/env python
from os import path
import sys
# import argparse
import glob
import re
import wave
import time
import csv
import sox
import subprocess
import hashlib
def main():
paths = ['./']
if len(sys.argv) > 1:
paths = sys.argv[1:]
fieldnames = [
'path',
'id',
'side',
'dig',
'take',
'notes',
'channels',
'sample_rate',
'bitrate',
'num_samples',
'encoding',
'precision',
'duration',
'size',
'md5',
'updated at (UTC)',
]
csv_writer = csv.DictWriter(sys.stdout, fieldnames = fieldnames, quoting=csv.QUOTE_MINIMAL)
csv_writer.writeheader()
file_pattern = re.compile('^(?:.+/)(.+?)-([ab])-(d\d+)-(t\d+)(?:-(.+))?.wav$')
for this_path in paths:
file_paths = glob.glob(this_path + '/*')
for file_path in file_paths:
m = file_pattern.match(file_path)
if m:
file_entry = get_file_information(file_path, m)
csv_writer.writerow(file_entry)
def get_soxi_line(reg, input):
return re.sub(reg, '', input)
def get_file_intrinsic_information(file_path):
info_line = re.compile('(.*?):(.+)')
output = subprocess.check_output(['soxi', file_path]).split("\n")
replace_header = re.compile('.*?: ')
result = {
'channels': get_soxi_line(replace_header, output[2]), #channels
'sample_rate': get_soxi_line(replace_header, output[3]), #sample rate
'precision': get_soxi_line(replace_header, output[4]), #precision
'duration': re.sub('\s=.+$', '', get_soxi_line(replace_header, output[5])), #duration
'bitrate': get_soxi_line(replace_header, output[7]), #bit rate
'encoding': get_soxi_line(replace_header, output[8]) #encoding
}
return result
def get_file_extrinsic_information(file_path, m):
file_entry = {
'path': path.abspath(file_path),
'id': m.group(1),
'side': m.group(2),
'dig': m.group(3),
'take': m.group(4),
'notes': m.group(5),
'updated at (UTC)': time.strftime("%d %b %Y %H:%M:%S", time.gmtime(path.getmtime(file_path))),
'size': get_size(path.getsize(file_path)),
'md5': md5(file_path)
}
return file_entry
# from https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def get_file_information(file_path, m):
# test = sox.file_info.info(file_path)
file_file_extrinsic_information = get_file_extrinsic_information(file_path, m)
file_intrinsic_information = get_file_intrinsic_information(file_path)
result = dict(file_intrinsic_information, **file_file_extrinsic_information)
return result
# from https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
def get_size(num, suffix='B'):
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
return "%3.2f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
if __name__ == "__main__":
main()