set last page flag on opus in case something wants that

This commit is contained in:
Artemis Tosini 2024-04-04 06:20:23 +00:00
parent 6e50a386ec
commit f0960f6115
Signed by: artemist
GPG key ID: EE5227935FE3FF18

View file

@ -14,9 +14,11 @@ def crc32ogg(seq):
return crc return crc
def paginate(sequence: int, content: bytes): def paginate(sequence: int, is_last: bool, content: bytes):
# Version, flags, position, serial number, sequence number, checksum, segments, segment table # Version, flags, position, serial number, sequence number, checksum, segments, segment table
flags = 2 if sequence == 0 else 0 flags = 2 if sequence == 0 else 0
if is_last:
flags |= 4
page = bytearray( page = bytearray(
b"OggS" b"OggS"
+ struct.pack( + struct.pack(
@ -27,8 +29,6 @@ def paginate(sequence: int, content: bytes):
+ content + content
) )
struct.pack_into("<I", page, 22, crc32ogg(page)) struct.pack_into("<I", page, 22, crc32ogg(page))
if sequence == 0:
print(page)
return page return page
@ -64,17 +64,20 @@ def write_opus(ktss: bytes, filename: pathlib.Path):
+ channel_mapping + channel_mapping
) )
# Channel mapping is apparently incorrect for 6 channels but I don't care # Channel mapping is apparently incorrect for 6 channels but I don't care
out.write(paginate(0, opus_header)) out.write(paginate(0, False, opus_header))
comment_header = b"OpusTags\4\0\0\0ktss\0\0\0\0\0\0\0\0" comment_header = b"OpusTags\4\0\0\0ktss\0\0\0\0\0\0\0\0"
out.write(paginate(1, comment_header)) out.write(paginate(1, False, comment_header))
# Weird length encoding here, not opus standard # Weird length encoding here, not opus standard
sequence = 2 sequence = 2
offset = start_offset offset = start_offset
while offset < len(ktss): while offset < len(ktss):
(packet_len,) = struct.unpack(">I", ktss[offset : offset + 4]) (packet_len,) = struct.unpack(">I", ktss[offset : offset + 4])
out.write(paginate(sequence, ktss[offset + 8 : offset + 8 + packet_len])) is_last = offset + 8 + packet_len >= len(ktss)
out.write(
paginate(sequence, is_last, ktss[offset + 8 : offset + 8 + packet_len])
)
offset += packet_len + 8 offset += packet_len + 8
sequence += 1 sequence += 1