本文整理汇总了Python中mutagen.ogg.OggPage.to_packets方法的典型用法代码示例。如果您正苦于以下问题:Python OggPage.to_packets方法的具体用法?Python OggPage.to_packets怎么用?Python OggPage.to_packets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mutagen.ogg.OggPage
的用法示例。
在下文中一共展示了OggPage.to_packets方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_packets
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_to_packets(self):
self.failUnlessEqual(
[b"foo", b"bar", b"baz"], OggPage.to_packets(self.pages))
self.pages[0].complete = False
self.pages[1].continued = True
self.failUnlessEqual(
[b"foobar", b"baz"], OggPage.to_packets(self.pages))
示例2: test_replace_continued
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_replace_continued(self):
# take a partial packet and replace it with a new page
# replace() should make it spanning again
fileobj = BytesIO()
pages = [OggPage(), OggPage()]
pages[0].serial = 1
pages[0].sequence = 0
pages[0].complete = False
pages[0].packets = [b"foo"]
pages[1].serial = 1
pages[1].sequence = 1
pages[1].continued = True
pages[1].packets = [b"bar"]
fileobj = BytesIO()
for page in pages:
fileobj.write(page.write())
fileobj.seek(0, 0)
pages_from_file = [OggPage(fileobj), OggPage(fileobj)]
self.assertEqual(OggPage.to_packets(pages_from_file), [b"foobar"])
packets_part = OggPage.to_packets([pages_from_file[0]])
self.assertEqual(packets_part, [b"foo"])
new_pages = OggPage.from_packets([b"quuux"])
OggPage.replace(fileobj, [pages_from_file[0]], new_pages)
fileobj.seek(0, 0)
written = OggPage.to_packets([OggPage(fileobj), OggPage(fileobj)])
self.assertEquals(written, [b"quuuxbar"])
示例3: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def _inject(self, fileobj):
"""Write tag data into the Speex comment packet/page."""
fileobj.seek(0)
# Find the first header page, with the stream info.
# Use it to get the serial number.
page = OggPage(fileobj)
while not page.packets[0].startswith("Speex "):
page = OggPage(fileobj)
# Look for the next page with that serial number, it'll start
# the comment packet.
serial = page.serial
page = OggPage(fileobj)
while page.serial != serial:
page = OggPage(fileobj)
# Then find all the pages with the comment packet.
old_pages = [page]
while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
page = OggPage(fileobj)
if page.serial == old_pages[0].serial:
old_pages.append(page)
packets = OggPage.to_packets(old_pages, strict=False)
# Set the new comment packet.
packets[0] = self.write(framing=False)
new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
OggPage.replace(fileobj, old_pages, new_pages)
示例4: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def _inject(self, fileobj, padding_func):
"""Write tag data into the Theora comment packet/page."""
fileobj.seek(0)
page = OggPage(fileobj)
while not page.packets[0].startswith(b"\x81theora"):
page = OggPage(fileobj)
old_pages = [page]
while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
page = OggPage(fileobj)
if page.serial == old_pages[0].serial:
old_pages.append(page)
packets = OggPage.to_packets(old_pages, strict=False)
content_size = get_size(fileobj) - len(packets[0]) # approx
vcomment_data = b"\x81theora" + self.write(framing=False)
padding_left = len(packets[0]) - len(vcomment_data)
info = PaddingInfo(padding_left, content_size)
new_padding = info._get_padding(padding_func)
packets[0] = vcomment_data + b"\x00" * new_padding
new_pages = OggPage._from_packets_try_preserve(packets, old_pages)
OggPage.replace(fileobj, old_pages, new_pages)
示例5: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def _inject(self, fileobj, padding_func):
"""Write tag data into the Vorbis comment packet/page."""
# Find the old pages in the file; we'll need to remove them,
# plus grab any stray setup packet data out of them.
fileobj.seek(0)
page = OggPage(fileobj)
while not page.packets[0].startswith(b"\x03vorbis"):
page = OggPage(fileobj)
old_pages = [page]
while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
page = OggPage(fileobj)
if page.serial == old_pages[0].serial:
old_pages.append(page)
packets = OggPage.to_packets(old_pages, strict=False)
content_size = get_size(fileobj) - len(packets[0]) # approx
vcomment_data = b"\x03vorbis" + self.write()
padding_left = len(packets[0]) - len(vcomment_data)
info = PaddingInfo(padding_left, content_size)
new_padding = info._get_padding(padding_func)
# Set the new comment packet.
packets[0] = vcomment_data + b"\x00" * new_padding
new_pages = OggPage._from_packets_try_preserve(packets, old_pages)
OggPage.replace(fileobj, old_pages, new_pages)
示例6: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def _inject(self, fileobj):
"""Write tag data into the FLAC Vorbis comment packet/page."""
# Ogg FLAC has no convenient data marker like Vorbis, but the
# second packet - and second page - must be the comment data.
fileobj.seek(0)
page = OggPage(fileobj)
while not page.packets[0].startswith(b"\x7FFLAC"):
page = OggPage(fileobj)
first_page = page
while not (page.sequence == 1 and page.serial == first_page.serial):
page = OggPage(fileobj)
old_pages = [page]
while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
page = OggPage(fileobj)
if page.serial == first_page.serial:
old_pages.append(page)
packets = OggPage.to_packets(old_pages, strict=False)
# Set the new comment block.
data = self.write()
data = bytes((packets[0][0],)) + struct.pack(">I", len(data))[-3:] + data
packets[0] = data
new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
OggPage.replace(fileobj, old_pages, new_pages)
示例7: __init__
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def __init__(self, fileobj, info):
self.pages = []
self.pages_data = []
self.sections = []
complete = False
while not complete:
page = OggPage(fileobj)
if page.serial == info.serial:
self.pages.append(page)
# print page.packets
packet_start = page.offset + 27 + page.segments
packet_end = packet_start + page.lacings[0] - 1
# print page
page_data = OggVCommentData(page.offset, packet_start, packet_end)
# print page_data
self.pages_data.append(page_data)
complete = page.complete or (len(page.packets) > 1)
data = OggPage.to_packets(self.pages)[0][7:] # Strip off "\x03vorbis".
# print data
super(OggVCommentDict, self).__init__(data)
picture_found = False
for (tag, tagvalue), (offset, size) in zip(self, self.tag_data):
# the offset is relative, calculate the absolute offset - we need to add the absolute page offset for the first page and the stripped off "\x03vorbis"
abs_offset = offset + self.pages_data[0].packet_start + 7
if tag == 'METADATA_BLOCK_PICTURE' and not picture_found:
picture_found = True
# print "tag: %s, offset: %s, abs offset: %s, size: %s" % (tag, offset, abs_offset, size)
# find which page picture starts in
length = count = 0
for pd in self.pages_data:
data_length = pd.packet_end - pd.packet_start + 1
length += data_length
if offset <= length:
# picture starts on this page
tag_len = len(tag) + 1
data_start = abs_offset + tag_len
self.sections.append(data_start)
packet_left = pd.packet_end - data_start + 1
if packet_left >= size:
data_end = size
self.sections.append(data_end)
else:
self.sections.append(packet_left)
remaining = size - packet_left - tag_len
for i in range(count+1, len(self.pages_data)):
data_start = self.pages_data[i].packet_start
self.sections.append(self.pages_data[i].packet_start)
packet_length = self.pages_data[i].packet_end - self.pages_data[i].packet_start + 1
if packet_length >= remaining:
self.sections.append(remaining)
break
else:
self.sections.append(packet_length)
remaining -= packet_length
# print self.sections
break
count += 1
示例8: test_replace_fast_path
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_replace_fast_path(self):
# create interleaved pages
fileobj = BytesIO()
pages = [OggPage(), OggPage(), OggPage()]
pages[0].serial = 42
pages[0].sequence = 0
pages[0].packets = [b"foo"]
pages[1].serial = 24
pages[1].sequence = 0
pages[1].packets = [b"bar"]
pages[2].serial = 42
pages[2].sequence = 1
pages[2].packets = [b"baz"]
for page in pages:
fileobj.write(page.write())
fileobj.seek(0, 0)
pages_from_file = [OggPage(fileobj), OggPage(fileobj),
OggPage(fileobj)]
old_pages = [pages_from_file[0], pages_from_file[2]]
packets = OggPage.to_packets(old_pages, strict=True)
self.assertEqual(packets, [b"foo", b"baz"])
new_packets = [b"111", b"222"]
new_pages = OggPage._from_packets_try_preserve(new_packets, old_pages)
self.assertEqual(len(new_pages), 2)
# remove insert_bytes, so we can be sure the fast path was taken
old_insert_bytes = _util.insert_bytes
_util.insert_bytes = None
try:
OggPage.replace(fileobj, old_pages, new_pages)
finally:
_util.insert_bytes = old_insert_bytes
# validate that the new data was written and the other pages
# are untouched
fileobj.seek(0, 0)
pages_from_file = [OggPage(fileobj), OggPage(fileobj),
OggPage(fileobj)]
packets = OggPage.to_packets(
[pages_from_file[0], pages_from_file[2]], strict=True)
self.assertEqual(packets, [b"111", b"222"])
packets = OggPage.to_packets([pages_from_file[1]], strict=True)
self.assertEqual(packets, [b"bar"])
示例9: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def _inject(self, fileobj):
fileobj.seek(0)
info = OggOpusInfo(fileobj)
old_pages = self.__get_comment_pages(fileobj, info)
packets = OggPage.to_packets(old_pages)
packets[0] = "OpusTags" + self.write(framing=False)
new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
OggPage.replace(fileobj, old_pages, new_pages)
示例10: test_page_max_size
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_page_max_size(self):
page = OggPage()
page.packets = ["1" * 255 * 255]
page.complete = False
page2 = OggPage()
page2.packets = [""]
page2.sequence = 1
page2.continued = True
self.failUnlessEqual(["1" * 255 * 255], OggPage.to_packets([page, page2]))
示例11: test_packet_exactly_255
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_packet_exactly_255(self):
page = OggPage()
page.packets = [b"1" * 255]
page.complete = False
page2 = OggPage()
page2.packets = [b""]
page2.sequence = 1
page2.continued = True
self.failUnlessEqual([b"1" * 255], OggPage.to_packets([page, page2]))
示例12: __init__
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def __init__(self, fileobj, info):
pages = []
complete = False
while not complete:
page = OggPage(fileobj)
if page.serial == info.serial:
pages.append(page)
complete = page.complete or (len(page.packets) > 1)
data = OggPage.to_packets(pages)[0][7:]
super(OggTheoraCommentDict, self).__init__(data + "\x01")
示例13: __init__
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def __init__(self, fileobj, info):
pages = []
complete = False
while not complete:
page = OggPage(fileobj)
if page.serial == info.serial:
pages.append(page)
complete = page.complete or (len(page.packets) > 1)
data = OggPage.to_packets(pages)[0] + "\x01"
super(OggSpeexVComment, self).__init__(data, framing=False)
示例14: __init__
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def __init__(self, fileobj, info):
pages = []
complete = False
while not complete:
page = OggPage(fileobj)
if page.serial == info.serial:
pages.append(page)
complete = page.complete or (len(page.packets) > 1)
data = OggPage.to_packets(pages)[0][7:] # Strip off "\x03vorbis".
super(OggVCommentDict, self).__init__(data)
self._padding = len(data) - self._size
示例15: test_random_data_roundtrip
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import to_packets [as 别名]
def test_random_data_roundtrip(self):
try:
random_file = open("/dev/urandom", "rb")
except (IOError, OSError):
print "WARNING: Random data round trip test disabled."
return
for i in range(10):
num_packets = random.randrange(2, 100)
lengths = [random.randrange(10, 10000) for i in range(num_packets)]
packets = map(random_file.read, lengths)
self.failUnlessEqual(packets, OggPage.to_packets(OggPage.from_packets(packets)))