本文整理汇总了Python中mutagen.ogg.OggPage.from_packets方法的典型用法代码示例。如果您正苦于以下问题:Python OggPage.from_packets方法的具体用法?Python OggPage.from_packets怎么用?Python OggPage.from_packets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mutagen.ogg.OggPage
的用法示例。
在下文中一共展示了OggPage.from_packets方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_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)
示例2: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_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)
示例3: test_replace_continued
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_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"])
示例4: test_from_packets_position
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_from_packets_position(self):
packets = [b"1" * 100000]
pages = OggPage.from_packets(packets)
self.failUnless(len(pages) > 1)
for page in pages[:-1]:
self.failUnlessEqual(-1, page.position)
self.failUnlessEqual(0, pages[-1].position)
示例5: test_too_many_packets
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_too_many_packets(self):
packets = [b"1"] * 3000
pages = OggPage.from_packets(packets)
for p in pages:
OggPage.write(p)
self.failUnless(len(pages) > 3000//255)
示例6: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_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)
示例7: test__from_packets_try_preserve
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test__from_packets_try_preserve(self):
# if the packet layout matches, just create pages with
# the same layout and copy things over
packets = [b"1" * 100000, b"2" * 100000, b"3" * 100000]
pages = OggPage.from_packets(packets, sequence=42, default_size=977)
new_pages = OggPage._from_packets_try_preserve(packets, pages)
self.assertEqual(pages, new_pages)
# zero case
new_pages = OggPage._from_packets_try_preserve([], pages)
self.assertEqual(new_pages, [])
# if the layout doesn't match we should fall back to creating new
# pages starting with the sequence of the first given page
other_packets = list(packets)
other_packets[1] += b"\xff"
other_pages = OggPage.from_packets(other_packets, 42)
new_pages = OggPage._from_packets_try_preserve(other_packets, pages)
self.assertEqual(new_pages, other_pages)
示例8: test_random_data_roundtrip
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_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)))
示例9: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def _inject(self, fileobj):
"""Write tag data into the Theora comment packet/page."""
fileobj.seek(0)
page = OggPage(fileobj)
while not page.packets[0].startswith("\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)
packets[0] = "\x81theora" + self.write(framing=False)
new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
OggPage.replace(fileobj, old_pages, new_pages)
示例10: test_preserve_non_padding
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_preserve_non_padding(self):
self.audio["FOO"] = ["BAR"]
self.audio.save()
extra_data = b"\xde\xad\xbe\xef"
with open(self.filename, "r+b") as fobj:
OggPage(fobj) # header
page = OggPage(fobj)
data = OggPage.to_packets([page])[0]
data = data.rstrip(b"\x00") + b"\x01" + extra_data
new_pages = OggPage.from_packets([data], page.sequence)
OggPage.replace(fobj, [page], new_pages)
OggOpus(self.filename).save()
with open(self.filename, "rb") as fobj:
OggPage(fobj) # header
page = OggPage(fobj)
data = OggPage.to_packets([page])[0]
self.assertTrue(data.endswith(b"\x01" + extra_data))
示例11: _inject
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def _inject(self, fileobj):
"""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)
# Set the new comment packet.
packets[0] = b"\x03vorbis" + self.write()
new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
OggPage.replace(fileobj, old_pages, new_pages)
示例12: test_replace
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_replace(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"1111", b"2222"]
new_pages = OggPage.from_packets(new_packets,
sequence=old_pages[0].sequence)
self.assertEqual(len(new_pages), 1)
OggPage.replace(fileobj, old_pages, new_pages)
fileobj.seek(0, 0)
first = OggPage(fileobj)
self.assertEqual(first.serial, 42)
self.assertEqual(OggPage.to_packets([first], strict=True),
[b"1111", b"2222"])
second = OggPage(fileobj)
self.assertEqual(second.serial, 24)
self.assertEqual(OggPage.to_packets([second], strict=True), [b"bar"])
示例13: test_one_packet_per_wiggle
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_one_packet_per_wiggle(self):
packets = [b"1" * 511, b"2" * 511, b"3" * 511]
pages = OggPage.from_packets(
packets, default_size=1000, wiggle_room=1000000)
self.failUnlessEqual(len(pages), 2)
self.failUnlessEqual(OggPage.to_packets(pages), packets)
示例14: test_crappy_fragmentation
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_crappy_fragmentation(self):
packets = [b"1" * 511, b"2" * 511, b"3" * 511]
pages = OggPage.from_packets(packets, default_size=510, wiggle_room=0)
self.failUnless(len(pages) > 3)
self.failUnlessEqual(OggPage.to_packets(pages), packets)
示例15: test_too_many_packets
# 需要导入模块: from mutagen.ogg import OggPage [as 别名]
# 或者: from mutagen.ogg.OggPage import from_packets [as 别名]
def test_too_many_packets(self):
packets = [b"1"] * 3000
pages = OggPage.from_packets(packets)
list(map(OggPage.write, pages))
self.failUnless(len(pages) > 3000/255)