Use CRC32 instead of MD5 for Track IDs in DASH/HLS

This commit is contained in:
rlaphoenix 2024-02-15 10:56:51 +00:00
parent bd185126b6
commit 709901176e
2 changed files with 6 additions and 6 deletions

View File

@ -11,11 +11,11 @@ from concurrent import futures
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from copy import copy from copy import copy
from functools import partial from functools import partial
from hashlib import md5
from pathlib import Path from pathlib import Path
from typing import Any, Callable, MutableMapping, Optional, Union from typing import Any, Callable, MutableMapping, Optional, Union
from urllib.parse import urljoin, urlparse from urllib.parse import urljoin, urlparse
from uuid import UUID from uuid import UUID
from zlib import crc32
import requests import requests
from langcodes import Language, tag_is_valid from langcodes import Language, tag_is_valid
@ -195,14 +195,14 @@ class DASH:
# a good and actually unique track ID, sometimes because of the lang # a good and actually unique track ID, sometimes because of the lang
# dialect not being represented in the id, or the bitrate, or such. # dialect not being represented in the id, or the bitrate, or such.
# this combines all of them as one and hashes it to keep it small(ish). # this combines all of them as one and hashes it to keep it small(ish).
track_id = md5("{codec}-{lang}-{bitrate}-{base_url}-{ids}-{track_args}".format( track_id = hex(crc32("{codec}-{lang}-{bitrate}-{base_url}-{ids}-{track_args}".format(
codec=codecs, codec=codecs,
lang=track_lang, lang=track_lang,
bitrate=get("bitrate"), bitrate=get("bitrate"),
base_url=(rep.findtext("BaseURL") or "").split("?")[0], base_url=(rep.findtext("BaseURL") or "").split("?")[0],
ids=[get("audioTrackId"), get("id"), period.get("id")], ids=[get("audioTrackId"), get("id"), period.get("id")],
track_args=track_args track_args=track_args
).encode()).hexdigest() ).encode()))[2:]
tracks.add(track_type( tracks.add(track_type(
id_=track_id, id_=track_id,

View File

@ -10,12 +10,12 @@ import time
from concurrent import futures from concurrent import futures
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from functools import partial from functools import partial
from hashlib import md5
from pathlib import Path from pathlib import Path
from queue import Queue from queue import Queue
from threading import Lock from threading import Lock
from typing import Any, Callable, Optional, Union from typing import Any, Callable, Optional, Union
from urllib.parse import urljoin from urllib.parse import urljoin
from zlib import crc32
import m3u8 import m3u8
import requests import requests
@ -115,7 +115,7 @@ class HLS:
primary_track_type = Video primary_track_type = Video
tracks.add(primary_track_type( tracks.add(primary_track_type(
id_=md5(str(playlist).encode()).hexdigest()[0:7], # 7 chars only for filename length id_=hex(crc32(str(playlist).encode()))[2:],
url=urljoin(playlist.base_uri, playlist.uri), url=urljoin(playlist.base_uri, playlist.uri),
codec=primary_track_type.Codec.from_codecs(playlist.stream_info.codecs), codec=primary_track_type.Codec.from_codecs(playlist.stream_info.codecs),
language=language, # HLS manifests do not seem to have language info language=language, # HLS manifests do not seem to have language info
@ -166,7 +166,7 @@ class HLS:
raise ValueError(msg) raise ValueError(msg)
tracks.add(track_type( tracks.add(track_type(
id_=md5(str(media).encode()).hexdigest()[0:6], # 6 chars only for filename length id_=hex(crc32(str(media).encode()))[2:],
url=urljoin(media.base_uri, media.uri), url=urljoin(media.base_uri, media.uri),
codec=codec, codec=codec,
language=track_lang, # HLS media may not have language info, fallback if needed language=track_lang, # HLS media may not have language info, fallback if needed