forked from nnen/waterfall
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileinfo
executable file
·79 lines (57 loc) · 2.15 KB
/
fileinfo
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""fileinfo module.
Author: Jan Milik <[email protected]>
"""
import os
import sys
import struct
def show_file_info(file_name):
stat = os.stat(file_name)
print "Filename: %s" % (file_name, )
print "File size: %d B" % (stat.st_size, )
if file_name.endswith(".wav"):
show_wav_info(file_name)
def show_wav_info(file_name):
with open(file_name, "rb") as f:
data = f.read()
# print repr(struct.unpack("<4sI4s4sI", data[:20]))
riff_chunk_id = struct.unpack("<4s", data[:4])[0]
if riff_chunk_id <> "RIFF":
print "ERROR: Invalid RIFF chunk id (%r)." % (riff_chunk_id, )
return
chunk_size = struct.unpack("<I", data[4:8])[0]
wave_id = struct.unpack("<4s", data[8:12])[0]
if wave_id <> "WAVE":
print "ERROR: Invalid WAVE ID."
return
offset = 12
while offset - 8 < chunk_size:
subchunk_id, subchunk_size = struct.unpack("<4sI", data[offset:offset + 8])
offset += 8
if subchunk_id <> "fmt ":
offset += subchunk_size
continue
fmt, num_channels, sample_rate, byte_rate, block_align, bits_per_sample = struct.unpack("<HHIIHH", data[offset:offset + 16])
fmt_name = {
0x0001: "PCM",
0x0003: "IEEE FLOAT",
0x0006: "ALAW",
0x0007: "MULAW",
0xfffe: "EXTENSIBLE",
}.get(fmt, None)
if fmt_name is None:
print "Format: %d" % (fmt, )
else:
print "Format: %s (%d)" % (fmt_name, fmt, )
print "Number of channels: %d" % (num_channels, )
print "Sample rate: %d" % (sample_rate, )
print "Byte rate: %d" % (byte_rate, )
print "Block align: %d" % (block_align, )
print "Bits per sample: %d" % (bits_per_sample, )
return
def main():
for arg in sys.argv[1:]:
show_file_info(arg)
if __name__ == "__main__":
main()