Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use codecs pretty name for comparations

NTFSvolume5b5d411ef923
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index 41fe954a7..284135fc6 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -24,7 +24,7 @@ from cyberdrop_dl.crawlers._hls import HLSMixin
from cyberdrop_dl.downloader.http import Downloader
from cyberdrop_dl.exceptions import MaxChildrenError, NoExtensionError, ScrapeError
from cyberdrop_dl.filepath import check_dangerous_filename, check_path_traversal, compose_filename, get_filename_and_ext
-from cyberdrop_dl.mediaprops import ISO639Subtitle, Resolution
+from cyberdrop_dl.mediaprops import Codecs, ISO639Subtitle, Resolution
from cyberdrop_dl.models.validators import strings
from cyberdrop_dl.url_objects import AbsoluteHttpURL, MediaItem, MuxVideo, ScrapeItem, is_absolute_http_url
from cyberdrop_dl.utils import css, dates, enter_context, fast_cache, is_blob_or_svg, m3u8, parse_url, unique
@@ -1006,9 +1006,9 @@ class Crawler(HTTPMixin, HLSMixin, ABC):
if _include.file_id and file_id:
yield file_id
if _include.video_codec and video_codec:
- yield video_codec
+ yield Codecs.pretty_name(video_codec)
if _include.audio_codec and audio_codec:
- yield audio_codec
+ yield Codecs.pretty_name(audio_codec)
if _include.resolution and resolution and resolution not in {Resolution.highest(), Resolution.unknown()}:
res = resolution if type(resolution) is Resolution else Resolution.parse(resolution)
diff --git a/cyberdrop_dl/mediaprops.py b/cyberdrop_dl/mediaprops.py
index fc2d83c9e..8728fb01e 100644
--- a/cyberdrop_dl/mediaprops.py
+++ b/cyberdrop_dl/mediaprops.py
@@ -27,9 +27,11 @@ VIDEO_CODECS = (
"dvhe",
"h263",
"h264",
+ "hevc",
+ "hevc1",
+ "hevc2",
"hev1",
"hev2",
- "hevc",
"hvc1",
"hvc2",
"mp4v",
@@ -83,6 +85,18 @@ class Codecs:
if codec:
yield re.sub(r"^([^.]+)", lambda c: c.group(1).lower(), codec)
+ @staticmethod
+ def normalize(codec: str) -> str:
+ return re.sub(r"0+(?=\d)", "", codec)
+
+ @staticmethod
+ def pretty_name(codec: str) -> str:
+ name = Codecs.normalize(codec).partition(".")[0]
+ for pf in ("hevc1", "hev1", "hvc1", "hevc", "hev", "hvc"):
+ if name.startswith(pf):
+ return name.replace(pf, "hevc")
+ return name
+
@staticmethod
def parse(codecs: str) -> Codecs:
codecs = codecs.strip()
@@ -92,7 +106,7 @@ class Codecs:
v_codec = a_codec = s_codec = hdr = None
codecs_tuple = tuple(Codecs.split(codecs))
for codec in codecs_tuple:
- parts = re.sub(r"0+(?=\d)", "", codec).split(".")
+ parts = Codecs.normalize(codec).split(".")
name = parts[0]
if name in AUDIO_CODECS:
a_codec = a_codec or codec
@@ -105,9 +119,6 @@ class Codecs:
else:
logger.warning("Unknown RFC6381 codec: %s", codec)
- if v_codec and v_codec.startswith(("hev", "hvc")):
- v_codec = "hevc"
-
if v_codec or a_codec or s_codec:
return Codecs(v_codec, a_codec, s_codec, hdr)
diff --git a/cyberdrop_dl/utils/m3u8.py b/cyberdrop_dl/utils/m3u8.py
index 8ef38f5e7..f69216817 100644
--- a/cyberdrop_dl/utils/m3u8.py
+++ b/cyberdrop_dl/utils/m3u8.py
@@ -278,8 +278,10 @@ class VariantM3U8Parser:
if isinstance(only, str):
only = (only,)
+ only = tuple(map(Codecs.pretty_name, only))
+ exclude = tuple(map(Codecs.pretty_name, exclude))
for group in self.groups:
- if codec := group.codecs.video:
+ if codec := Codecs.pretty_name(group.codecs.video or ""):
if only and codec not in only:
continue
if codec in exclude:
diff --git a/tests/test_m3u8.py b/tests/test_m3u8.py
index 31b0e8956..dc1aaafb6 100644
--- a/tests/test_m3u8.py
+++ b/tests/test_m3u8.py
@@ -3,7 +3,7 @@ from typing import LiteralString
import pytest
-from cyberdrop_dl.mediaprops import Resolution
+from cyberdrop_dl.mediaprops import Codecs, Resolution
from cyberdrop_dl.url_objects import AbsoluteHttpURL
from cyberdrop_dl.utils import m3u8
@@ -109,25 +109,6 @@ def test_get_resolution_from_url_invalid_url(url: str, exception: type[Exception
m3u8.get_resolution_from_url(url)
- ("codecs", "result"),
- [
- ("avc1.4d401f,mp4a.40.2", ("avc1", "mp4a")),
- ("hvc1.1.6.L93.B0,mp4a.40.2", ("hevc", "mp4a")),
- ("hevc1.1.6.L93.B0,mp4a.40.2", ("hevc", "mp4a")),
- ("vp09.00.10.08,opus", ("vp9", "opus")),
- ("vp10.00.10.08,opus", ("vp10", "opus")),
- ("vp9.00.10.08,opus", ("vp9", "opus")),
- ("av01.0.04M.08,opus", ("av1", "opus")),
- ("avc1.4d401f,ac-3", ("avc1", "ac-3")),
- ("avc01.4d401f,ec-3", ("avc1", "ec-3")),
- ("avc1.4d401f", ("avc1", None)),
- ],
-)
-def test_codecs_parse(codecs: str, result: m3u8.Codecs) -> None:
- assert m3u8.Codecs.parse(codecs) == result
-
-
def test_m3u8(m3u8_content: str) -> None:
m3u8_obj = m3u8.M3U8(m3u8_content)
assert m3u8_obj.total_duration == dt.timedelta(seconds=28.5)
@@ -144,7 +125,7 @@ def test_m3u8_master(m3u8_master_content: str) -> None:
assert best.urls.audio is None
assert best.urls.subtitle is None
assert best.resolution == (1920, 1080)
- assert best.codecs == ("avc1", "mp4a")
+ assert best.codecs == Codecs("avc1.4d402j", "mp4a.40.2")
def test_m3u8_master_exclude_codec(m3u8_master_content: str) -> None:
@@ -162,20 +143,21 @@ def test_m3u8_master2(m3u8_master_content2: str) -> None:
variant = _variant_parser(m3u8_master_content2)
assert len(variant.groups) == 10
best = variant.get_best_group()
- assert best.urls.video == AbsoluteHttpURL("https://example.com/4b4ef277/vp9_1080p/video.m3u8")
+ assert best.urls.video == AbsoluteHttpURL("https://example.com/4b4ef277/1080p/video.m3u8")
assert best.urls.audio == AbsoluteHttpURL("https://example.com/4b4ef277/audio/audio.m3u8")
assert best.urls.subtitle is None
assert best.resolution == (1080, 1920)
- assert best.codecs == ("vp9", "mp4a")
+ assert best.codecs == Codecs("avc1.64002a", "mp4a.40.2")
def test_m3u8_master2_exclude_codec(m3u8_master_content2: str) -> None:
variant = _variant_parser(m3u8_master_content2)
best = variant.get_best_group(exclude="vp9")
- assert best.codecs.video == "avc1"
+ assert best.codecs.video == "avc1.64002a"
assert best.urls.video == AbsoluteHttpURL("https://example.com/4b4ef277/1080p/video.m3u8")
best = variant.get_best_group(only="vp9")
- assert best.codecs.video == "vp9"
+ assert best.codecs.video
+ assert Codecs.pretty_name(best.codecs.video) == "vp9"
with pytest.raises(StopIteration):
best = variant.get_best_group(exclude=["vp9", "avc1"])
with pytest.raises(StopIteration):
@@ -210,7 +192,7 @@ def test_m3u8_master_w_no_codecs_should_not_raise_an_error() -> None:
groups = _variant_parser(content).groups
assert len(groups) == 2
for group in groups:
- assert group.codecs == (None, None)
+ assert group.codecs is Codecs.UNKNOWN
def test_m3u8_selection_of_streams_w_no_resolution() -> None:
diff --git a/tests/test_mediaprops.py b/tests/test_mediaprops.py
index 922a61fa6..2a70d7ec5 100644
--- a/tests/test_mediaprops.py
+++ b/tests/test_mediaprops.py
@@ -21,6 +21,16 @@ from cyberdrop_dl.mediaprops import Codecs
("fLaC", Codecs(None, "flac")),
("theora, vorbis", Codecs("theora", "vorbis")),
("random_vcodec, random_acodec", Codecs("random_vcodec", "random_acodec")),
+ ("avc1.4d401f,mp4a.40.2", Codecs("avc1.4d401f", "mp4a.40.2")),
+ ("hvc1.1.6.L93.B0,mp4a.40.2", Codecs("hvc1.1.6.L93.B0", "mp4a.40.2")),
+ ("hevc1.1.6.L93.B0,mp4a.40.2", Codecs("hevc1.1.6.L93.B0", "mp4a.40.2")),
+ ("vp09.00.10.08,opus", Codecs("vp09.00.10.08", "opus")),
+ ("vp10.00.10.08,opus", Codecs("vp10.00.10.08", "opus")),
+ ("vp9.00.10.08,opus", Codecs("vp9.00.10.08", "opus")),
+ ("av01.0.04M.08,opus", Codecs("av01.0.04M.08", "opus")),
+ ("avc1.4d401f,ac-3", Codecs("avc1.4d401f", "ac-3")),
+ ("avc01.4d401f,ec-3", Codecs("avc01.4d401f", "ec-3")),
+ ("avc1.4d401f", Codecs("avc1.4d401f", None)),
],
)
def test_parse_codecs(codecs: str, expected: Codecs) -> None: