本文整理汇总了Python中mutagen._util.BitReader类的典型用法代码示例。如果您正苦于以下问题:Python BitReader类的具体用法?Python BitReader怎么用?Python BitReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BitReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bits
def test_bits(self):
data = b"\x12\x34\x56\x78\x89\xAB\xCD\xEF"
ref = cdata.uint64_be(data)
for i in xrange(64):
fo = cBytesIO(data)
r = BitReader(fo)
v = r.bits(i) << (64 - i) | r.bits(64 - i)
self.assertEqual(v, ref)
示例2: __init__
def __init__(self, fileobj, length):
"""Raises DescriptorError"""
r = BitReader(fileobj)
try:
self.objectTypeIndication = r.bits(8)
self.streamType = r.bits(6)
self.upStream = r.bits(1)
self.reserved = r.bits(1)
self.bufferSizeDB = r.bits(24)
self.maxBitrate = r.bits(32)
self.avgBitrate = r.bits(32)
if (self.objectTypeIndication, self.streamType) != (0x40, 0x5):
return
# all from here is optional
if length * 8 == r.get_position():
return
tag = r.bits(8)
except BitReaderError as e:
raise DescriptorError(e)
if tag == DecoderSpecificInfo.TAG:
assert r.is_aligned()
self.decSpecificInfo = DecoderSpecificInfo.parse(fileobj)
示例3: find_stream
def find_stream(cls, fileobj, max_bytes):
"""Returns a possibly valid _ADTSStream or None.
Args:
max_bytes (int): maximum bytes to read
"""
r = BitReader(fileobj)
stream = cls(r)
if stream.sync(max_bytes):
stream.offset = (r.get_position() - 12) // 8
return stream
示例4: _parse_dac3
def _parse_dac3(self, atom, fileobj):
# ETSI TS 102 366
assert atom.name == b"dac3"
ok, data = atom.read(fileobj)
if not ok:
raise ASEntryError("truncated %s atom" % atom.name)
fileobj = cBytesIO(data)
r = BitReader(fileobj)
# sample_rate in AudioSampleEntry covers values in
# fscod2 and not just fscod, so ignore fscod here.
try:
r.skip(2 + 5 + 3) # fscod, bsid, bsmod
acmod = r.bits(3)
lfeon = r.bits(1)
bit_rate_code = r.bits(5)
r.skip(5) # reserved
except BitReaderError as e:
raise ASEntryError(e)
self.channels = [2, 1, 2, 3, 3, 4, 4, 5][acmod] + lfeon
try:
self.bitrate = [
32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192,
224, 256, 320, 384, 448, 512, 576, 640][bit_rate_code] * 1000
except IndexError:
pass
示例5: _parse_esds
def _parse_esds(self, esds, fileobj):
assert esds.name == b"esds"
ok, data = esds.read(fileobj)
if not ok:
raise ASEntryError("truncated %s atom" % esds.name)
try:
version, flags, data = parse_full_atom(data)
except ValueError as e:
raise ASEntryError(e)
if version != 0:
raise ASEntryError("Unsupported version %d" % version)
fileobj = cBytesIO(data)
r = BitReader(fileobj)
try:
tag = r.bits(8)
if tag != ES_Descriptor.TAG:
raise ASEntryError("unexpected descriptor: %d" % tag)
assert r.is_aligned()
except BitReaderError as e:
raise ASEntryError(e)
try:
decSpecificInfo = ES_Descriptor.parse(fileobj)
except DescriptorError as e:
raise ASEntryError(e)
dec_conf_desc = decSpecificInfo.decConfigDescr
self.bitrate = dec_conf_desc.avgBitrate
self.codec += dec_conf_desc.codec_param
self.codec_description = dec_conf_desc.codec_desc
decSpecificInfo = dec_conf_desc.decSpecificInfo
if decSpecificInfo is not None:
if decSpecificInfo.channels != 0:
self.channels = decSpecificInfo.channels
if decSpecificInfo.sample_rate != 0:
self.sample_rate = decSpecificInfo.sample_rate
示例6: test_skip_too_much
def test_skip_too_much(self):
r = BitReader(cBytesIO(b"\xAB\xCD"))
# aligned skips don't fail, but the following read will
r.skip(32 + 8)
self.assertRaises(BitReaderError, r.bits, 1)
self.assertRaises(BitReaderError, r.skip, 1)
示例7: test_skip_more
def test_skip_more(self):
r = BitReader(cBytesIO(b"\xAB\xCD"))
self.assertEqual(r.bits(4), 0xa)
r.skip(8)
self.assertEqual(r.bits(4), 0xd)
self.assertRaises(BitReaderError, r.bits, 1)
示例8: test_skip
def test_skip(self):
r = BitReader(cBytesIO(b"\xEF"))
r.skip(4)
self.assertEqual(r.bits(4), 0xf)
示例9: test_read_too_much
def test_read_too_much(self):
r = BitReader(cBytesIO(b""))
self.assertEqual(r.bits(0), 0)
self.assertRaises(BitReaderError, r.bits, 1)
示例10: test_is_aligned
def test_is_aligned(self):
r = BitReader(cBytesIO(b"\xAB\xCD\xEF"))
self.assertTrue(r.is_aligned())
r.skip(1)
self.assertFalse(r.is_aligned())
r.skip(7)
self.assertTrue(r.is_aligned())
r.bits(7)
self.assertFalse(r.is_aligned())
r.bits(1)
self.assertTrue(r.is_aligned())
示例11: test_get_position
def test_get_position(self):
r = BitReader(cBytesIO(b"\xAB\xCD"))
self.assertEqual(r.get_position(), 0)
r.bits(3)
self.assertEqual(r.get_position(), 3)
r.skip(9)
self.assertEqual(r.get_position(), 3 + 9)
r.align()
self.assertEqual(r.get_position(), 16)
示例12: _parse_alac
def _parse_alac(self, atom, fileobj):
# https://alac.macosforge.org/trac/browser/trunk/
# ALACMagicCookieDescription.txt
assert atom.name == b"alac"
ok, data = atom.read(fileobj)
if not ok:
raise ASEntryError("truncated %s atom" % atom.name)
try:
version, flags, data = parse_full_atom(data)
except ValueError as e:
raise ASEntryError(e)
if version != 0:
raise ASEntryError("Unsupported version %d" % version)
fileobj = cBytesIO(data)
r = BitReader(fileobj)
try:
# for some files the AudioSampleEntry values default to 44100/2chan
# and the real info is in the alac cookie, so prefer it
r.skip(32) # frameLength
compatibleVersion = r.bits(8)
if compatibleVersion != 0:
return
self.sample_size = r.bits(8)
r.skip(8 + 8 + 8)
self.channels = r.bits(8)
r.skip(16 + 32)
self.bitrate = r.bits(32)
self.sample_rate = r.bits(32)
except BitReaderError as e:
raise ASEntryError(e)
示例13: _parse_adif
def _parse_adif(self, fileobj):
r = BitReader(fileobj)
try:
copyright_id_present = r.bits(1)
if copyright_id_present:
r.skip(72) # copyright_id
r.skip(1 + 1) # original_copy, home
bitstream_type = r.bits(1)
self.bitrate = r.bits(23)
npce = r.bits(4)
if bitstream_type == 0:
r.skip(20) # adif_buffer_fullness
pce = ProgramConfigElement(r)
try:
self.sample_rate = _FREQS[pce.sampling_frequency_index]
except IndexError:
pass
self.channels = pce.channels
# other pces..
for i in xrange(npce):
ProgramConfigElement(r)
r.align()
except BitReaderError as e:
raise AACError(e)
# use bitrate + data size to guess length
start = fileobj.tell()
fileobj.seek(0, 2)
length = fileobj.tell() - start
if self.bitrate != 0:
self.length = (8.0 * length) / self.bitrate
示例14: test_bytes
def test_bytes(self):
r = BitReader(cBytesIO(b"\xAB\xCD\xEF"))
self.assertEqual(r.bytes(2), b"\xAB\xCD")
self.assertEqual(r.bytes(0), b"")
示例15: test_bytes_unaligned
def test_bytes_unaligned(self):
r = BitReader(cBytesIO(b"\xAB\xCD\xEF"))
r.skip(4)
self.assertEqual(r.bytes(2), b"\xBC\xDE")