Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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"])
Пример #5
0
    def _inject(self, fileobj, padding_func):
        fileobj.seek(0)
        info = OggOpusInfo(fileobj)
        old_pages = self.__get_comment_pages(fileobj, info)

        packets = OggPage.to_packets(old_pages)
        vcomment_data = b"OpusTags" + self.write(framing=False)

        if self._pad_data:
            # if we have padding data to preserver we can't add more padding
            # as long as we don't know the structure of what follows
            packets[0] = vcomment_data + self._pad_data
        else:
            content_size = get_size(fileobj) - len(packets[0])  # approx
            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)
Пример #6
0
    def _inject(self, fileobj, padding_func):
        fileobj.seek(0)
        info = OggOpusInfo(fileobj)
        old_pages = self.__get_comment_pages(fileobj, info)

        packets = OggPage.to_packets(old_pages)
        vcomment_data = b"OpusTags" + self.write(framing=False)

        if self._pad_data:
            # if we have padding data to preserver we can't add more padding
            # as long as we don't know the structure of what follows
            packets[0] = vcomment_data + self._pad_data
        else:
            content_size = get_size(fileobj) - len(packets[0])  # approx
            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)
Пример #7
0
    def _inject(self, fileobj, padding_func):
        """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(b"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)

        content_size = get_size(fileobj) - len(packets[0])  # approx
        vcomment_data = 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)

        # 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)
Пример #8
0
    def _inject(self, fileobj, padding_func):
        """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(b"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)

        content_size = get_size(fileobj) - len(packets[0])  # approx
        vcomment_data = 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)

        # 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)