|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
from math import inf
|
|
|
|
from math import inf
|
|
|
|
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
|
|
|
from .common import PostProcessor
|
|
|
@ -61,61 +63,49 @@ class MP4FixupTimestampPP(PostProcessor):
|
|
|
|
|
|
|
|
|
|
|
|
return smallest_bmdt, sdur_cutoff
|
|
|
|
return smallest_bmdt, sdur_cutoff
|
|
|
|
|
|
|
|
|
|
|
|
def modify_mp4(self, src, dst, bmdt_offset, sdur_cutoff):
|
|
|
|
@staticmethod
|
|
|
|
with open(src, 'rb') as r, open(dst, 'wb') as w:
|
|
|
|
def transform(r, bmdt_offset, sdur_cutoff):
|
|
|
|
def converter():
|
|
|
|
for btype, content in r:
|
|
|
|
moov_over, in_secondary_moov = False, False
|
|
|
|
if btype == 'tfdt':
|
|
|
|
for btype, content in parse_mp4_boxes(r):
|
|
|
|
version, _ = unpack_ver_flags(content[0:4])
|
|
|
|
# skip duplicate MOOV boxes
|
|
|
|
if version == 0:
|
|
|
|
if btype == 'moov':
|
|
|
|
bmdt = unpack_be32(content[4:8])
|
|
|
|
if moov_over:
|
|
|
|
else:
|
|
|
|
in_secondary_moov = True
|
|
|
|
bmdt = unpack_be64(content[4:12])
|
|
|
|
continue
|
|
|
|
if bmdt == 0:
|
|
|
|
elif btype is None and content == 'moov':
|
|
|
|
yield (btype, content)
|
|
|
|
in_secondary_moov = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
# calculate new baseMediaDecodeTime
|
|
|
|
if moov_over:
|
|
|
|
bmdt = max(0, bmdt - bmdt_offset)
|
|
|
|
continue
|
|
|
|
# pack everything again and insert as a new box
|
|
|
|
moov_over = True
|
|
|
|
if version == 0:
|
|
|
|
elif in_secondary_moov:
|
|
|
|
bmdt_b = pack_be32(bmdt)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if btype == 'tfdt':
|
|
|
|
bmdt_b = pack_be64(bmdt)
|
|
|
|
version, _ = unpack_ver_flags(content[0:4])
|
|
|
|
yield ('tfdt', content[0:4] + bmdt_b + content[8 + version * 4:])
|
|
|
|
if version == 0:
|
|
|
|
continue
|
|
|
|
bmdt = unpack_be32(content[4:8])
|
|
|
|
elif btype == 'tfhd':
|
|
|
|
else:
|
|
|
|
version, flags = unpack_ver_flags(content[0:4])
|
|
|
|
bmdt = unpack_be64(content[4:12])
|
|
|
|
if not flags & 0x08:
|
|
|
|
if bmdt == 0:
|
|
|
|
|
|
|
|
yield (btype, content)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
# calculate new baseMediaDecodeTime
|
|
|
|
|
|
|
|
bmdt = max(0, bmdt - bmdt_offset)
|
|
|
|
|
|
|
|
# pack everything again and insert as a new box
|
|
|
|
|
|
|
|
if version == 0:
|
|
|
|
|
|
|
|
bmdt_b = pack_be32(bmdt)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
bmdt_b = pack_be64(bmdt)
|
|
|
|
|
|
|
|
yield ('tfdt', content[0:4] + bmdt_b + content[8 + version * 4:])
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif btype == 'tfhd':
|
|
|
|
|
|
|
|
version, flags = unpack_ver_flags(content[0:4])
|
|
|
|
|
|
|
|
if not flags & 0x08:
|
|
|
|
|
|
|
|
yield (btype, content)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
sdur_start = 8
|
|
|
|
|
|
|
|
if flags & 0x01:
|
|
|
|
|
|
|
|
sdur_start += 8
|
|
|
|
|
|
|
|
if flags & 0x02:
|
|
|
|
|
|
|
|
sdur_start += 4
|
|
|
|
|
|
|
|
sample_dur = unpack_be32(content[sdur_start:sdur_start + 4])
|
|
|
|
|
|
|
|
if sample_dur > sdur_cutoff:
|
|
|
|
|
|
|
|
sample_dur = 0
|
|
|
|
|
|
|
|
sd_b = pack_be32(sample_dur)
|
|
|
|
|
|
|
|
yield ('tfhd', content[:sdur_start] + sd_b + content[sdur_start + 4:])
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
yield (btype, content)
|
|
|
|
yield (btype, content)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
sdur_start = 8
|
|
|
|
|
|
|
|
if flags & 0x01:
|
|
|
|
|
|
|
|
sdur_start += 8
|
|
|
|
|
|
|
|
if flags & 0x02:
|
|
|
|
|
|
|
|
sdur_start += 4
|
|
|
|
|
|
|
|
sample_dur = unpack_be32(content[sdur_start:sdur_start + 4])
|
|
|
|
|
|
|
|
if sample_dur > sdur_cutoff:
|
|
|
|
|
|
|
|
sample_dur = 0
|
|
|
|
|
|
|
|
sd_b = pack_be32(sample_dur)
|
|
|
|
|
|
|
|
yield ('tfhd', content[:sdur_start] + sd_b + content[sdur_start + 4:])
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
yield (btype, content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
write_mp4_boxes(w, converter())
|
|
|
|
def modify_mp4(self, src, dst, bmdt_offset, sdur_cutoff):
|
|
|
|
|
|
|
|
with open(src, 'rb') as r, open(dst, 'wb') as w:
|
|
|
|
|
|
|
|
write_mp4_boxes(w, self.transform(parse_mp4_boxes(r)))
|
|
|
|
|
|
|
|
|
|
|
|
def run(self, information):
|
|
|
|
def run(self, information):
|
|
|
|
filename = information['filepath']
|
|
|
|
filename = information['filepath']
|
|
|
@ -137,6 +127,6 @@ class MP4FixupTimestampPP(PostProcessor):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.report_warning(f'Failed to fix duration of the file. (baseMediaDecodeTime offset = {bmdt_offset}, sample duration cutoff = {sdur_cutoff})')
|
|
|
|
self.report_warning(f'Failed to fix duration of the file. (baseMediaDecodeTime offset = {bmdt_offset}, sample duration cutoff = {sdur_cutoff})')
|
|
|
|
|
|
|
|
|
|
|
|
self._downloader.replace(temp_filename, filename)
|
|
|
|
os.replace(temp_filename, filename)
|
|
|
|
|
|
|
|
|
|
|
|
return [], information
|
|
|
|
return [], information
|
|
|
|