fix: remux failed due to segment data incomplete

fix #217
This commit is contained in:
acgnhik 2023-12-24 10:52:43 +08:00
parent 5b49b1dd33
commit 293e7db2d0

View File

@ -26,7 +26,7 @@ from blrec.utils import operators as utils_ops
from blrec.utils.hash import cksum from blrec.utils.hash import cksum
from blrec.exception.helpers import format_exception from blrec.exception.helpers import format_exception
from ..exceptions import FetchSegmentError from ..exceptions import FetchSegmentError, SegmentDataCorrupted
__all__ = ('SegmentFetcher', 'InitSectionData', 'SegmentData') __all__ = ('SegmentFetcher', 'InitSectionData', 'SegmentData')
@ -119,20 +119,30 @@ class SegmentFetcher:
last_segment = seg last_segment = seg
url = seg.absolute_uri url = seg.absolute_uri
crc32 = seg.title.split('|')[-1] hex_size, crc32, *_ = seg.title.split('|')
size = int(hex_size, 16)
for _ in range(3): for _ in range(3):
data = self._fetch_segment(url) data = self._fetch_segment(url)
if len(data) != size:
logger.debug(
'Segment data incomplete: '
f'size expected: {size}, '
f'size fetched: {len(data)}, '
f'segment url: {url}'
)
continue
crc32_of_data = cksum(data) crc32_of_data = cksum(data)
if crc32_of_data == crc32: if crc32_of_data != crc32:
break logger.debug(
logger.debug( 'Segment data corrupted: '
'Segment data corrupted: ' f'correct crc32: {crc32}, '
f'correct crc32: {crc32}, ' f'crc32 of segment data: {crc32_of_data}, '
f'crc32 of segment data: {crc32_of_data}, ' f'segment url: {url}'
f'segment url: {url}' )
) continue
break
else: else:
logger.warning(f'Segment data corrupted: {url}') raise SegmentDataCorrupted(url)
except Exception as exc: except Exception as exc:
logger.warning( logger.warning(
'Failed to fetch segment: {}\n{}', url, format_exception(exc) 'Failed to fetch segment: {}\n{}', url, format_exception(exc)