-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpeg_analyse.py
executable file
·133 lines (108 loc) · 4.2 KB
/
mpeg_analyse.py
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
#!/usr/bin/env python2
#
# Analyse an mp2 file for debugging
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Matthias P. Braendli
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from pprint import pprint
import sys
import struct
# Header:
# AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
# see http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
audio_version_ids = ["MPEG version 2.5", "reserved", "MPEG Version 2 (ISO/IEC 13818-3)", "MPEG Version 1 (ISO/IEC 11172-3)"]
layer_descriptions = ["reserved", "Layer III", "Layer II", "Layer I"]
# bitrate index:
# bits V1,L1 V1,L2 V1,L3 V2,L1 V2, L2 & L3
# 0000 free free free free free
# 0001 32 32 32 32 8
# 0010 64 48 40 48 16
# 0011 96 56 48 56 24
# 0100 128 64 56 64 32
# 0101 160 80 64 80 40
# 0110 192 96 80 96 48
# 0111 224 112 96 112 56
# 1000 256 128 112 128 64
# 1001 288 160 128 144 80
# 1010 320 192 160 160 96
# 1011 352 224 192 176 112
# 1100 384 256 224 192 128
# 1101 416 320 256 224 144
# 1110 448 384 320 256 160
# 1111 bad bad bad bad bad
ver1_layer2_bitrates = [0,
32, 48, 56, 64, 80, 96, 112, 128,
160, 192, 224, 256, 320, 384, -1]
fd = open(sys.argv[1], "rb")
while fd:
headerdata = struct.unpack("!BBBB", fd.read(4))
sync = (headerdata[0] << 3) | (headerdata[1] >> 5)
print("Sync: {:x}".format(sync))
audio_version_id = (headerdata[1] >> 3) & 0x3
print("audio_version_id {}".format(audio_version_ids[audio_version_id]))
layer_description = (headerdata[1] >> 1) & 0x3
print("layer_description {}".format(layer_descriptions[layer_description]))
protection_bit = headerdata[1] & 0x1
if protection_bit:
print("Without protection")
else:
print("With protection")
bitrate_index = headerdata[2] >> 4
print("bitrate_index {}".format(bitrate_index))
sampling_rate = (headerdata[2] >> 2) & 0x3
print("sampling_rate {}".format(sampling_rate))
padding_bit = (headerdata[2] >> 1) & 0x1
if padding_bit:
print("With padding")
else:
print("Without padding")
private_bit = headerdata[2] & 0x1
if private_bit:
print("Private bit set")
else:
print("Private bit unset")
channel_mode = headerdata[3] >> 6
print("channel_mode {}".format(channel_mode))
mode_extension = (headerdata[3] >> 4) & 0x3
print("mode_extension {}".format(mode_extension))
copyright = (headerdata[3] >> 3) & 0x1
if copyright:
print("With copyright")
else:
print("Without copyright")
original = (headerdata[3] >> 2) & 0x1
if original:
print("With original")
else:
print("Without original")
emphasis = headerdata[3] & 0x3
print("emphasis {}".format(emphasis))
if "MPEG Version 1" in audio_version_ids[audio_version_id] and \
layer_descriptions[layer_description] == "Layer II" and \
sampling_rate == 1:
BitRate = ver1_layer2_bitrates[bitrate_index] * 1000
print("bitrate {}".format(BitRate))
SampleRate = 48000
FrameLengthInBytes = 144 * BitRate / SampleRate + padding_bit
print("Frame size {}".format(FrameLengthInBytes))
databytes = fd.read(FrameLengthInBytes - 4)
pprint(["{:02x}".format(ord(d)) for d in databytes[-10:]])