Skip to main content

test_yt-dlp_alexwlchan.py

1"""
2Tests for yt-dlp_alexwlchan.
3"""
5import importlib
6import os
8from pymediainfo import MediaInfo
9import pytest
11yt_dlp_alexwlchan = importlib.import_module("yt-dlp_alexwlchan")
13download_video = yt_dlp_alexwlchan.download_video
14normalise_url = yt_dlp_alexwlchan.normalise_url
17def test_youtube_video() -> None:
18 """
19 Download a YouTube video and check we get the expected output.
20 """
21 video_info = download_video("https://www.youtube.com/watch?v=TUQaGhPdlxs")
23 assert (
24 video_info["title"]
25 == '"new york city, manhattan, people" - Free Public Domain Video'
26 )
27 assert os.path.exists(video_info["video_path"])
28 assert os.path.exists(video_info["thumbnail_path"])
29 assert video_info["subtitle_path"] is None
30 assert os.path.exists(video_info["uploader"]["avatar_path"])
32 assert video_info["id"] == "TUQaGhPdlxs"
33 assert video_info["date_uploaded"] == "2022-03-25T01:10:38Z"
35 assert video_info["video_path"].name.endswith(" [TUQaGhPdlxs].mp4")
38def test_youtube_path_is_cleaned_up() -> None:
39 """
40 Paths of YouTube videos get cleaned up during the download.
41 """
42 video = download_video("https://www.youtube.com/shorts/eso8JB7q0a0")
43 assert (
44 video["title"] == "3D Printing Everyday for 365 Days 176/365 "
45 "#stem #3dprinting #3dprint #ideas #useful"
46 )
47 assert (
48 os.path.basename(video["video_path"]) == "3D Printing Everyday for 365 Days "
49 "176-365 stem 3dprinting 3dprint ideas useful [eso8JB7q0a0].mp4"
50 )
53def test_youtube_video_does_not_http429() -> None:
54 """
55 Download a YouTube video without any subtitles or spoken audio.
56 """
57 video = download_video("https://www.youtube.com/watch?v=0N1_0SUGlDQ")
58 assert video["subtitle_path"] is None
61def test_youtube_video_with_automatic_subtitles() -> None:
62 """
63 Download a YouTube video with automatic subtitles.
64 """
65 video = download_video("https://www.youtube.com/shorts/hyGluE562oA")
66 assert video["subtitle_path"] is not None
69def test_youtube_ignores_ai_upscaling() -> None:
70 """
71 Downloaded a YouTube video ignores AI upscaled versions.
72 """
73 # This is a video uploaded in 2009, and the highest resolution is
74 # 640x480, but as of October 2025, YouTube have started offering
75 # AI upscaled versions.
76 video = download_video("https://www.youtube.com/watch?v=0N1_0SUGlDQ")
78 media_info = MediaInfo.parse(video["video_path"])
79 video_track = next(tr for tr in media_info.tracks if tr.track_type == "Video")
80 assert (video_track.width, video_track.height) == (640, 480)
83def test_instagram_video() -> None:
84 """
85 Download an Instagram video and check we get the expected output.
86 """
87 video_info = download_video("https://www.instagram.com/reel/DMWY8KkOS0n/")
89 assert os.path.exists(video_info["video_path"])
90 assert os.path.exists(video_info["thumbnail_path"])
91 assert video_info["subtitle_path"] is None
93 assert video_info["uploader"]["id"] == "52716733233"
94 assert video_info["uploader"]["name"] == "Public Domain Gems"
95 assert (
96 video_info["uploader"]["url"] == "https://www.instagram.com/publicdomaingems/"
97 )
98 assert os.path.exists(video_info["uploader"]["avatar_path"])
100 assert video_info["id"] == "DMWY8KkOS0n"
101 assert video_info["date_uploaded"] == "2025-07-21T00:34:41Z"
103 assert video_info["video_path"].name.endswith(" [DMWY8KkOS0n].mp4")
106@pytest.mark.parametrize(
107 "url, expected",
108 [
109 (
110 "https://www.instagram.com/reel/DMWY8KkOS0n/",
111 "https://www.instagram.com/reel/DMWY8KkOS0n/",
112 ),
113 (
114 "https://www.youtube.com/shorts/hyGluE562oA",
115 "https://www.youtube.com/shorts/hyGluE562oA",
116 ),
117 (
118 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
119 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
120 ),
121 (
122 "https://www.youtube.com/watch?v=0N1_0SUGlDQ&app=desktop&list=LL&index=43",
123 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
124 ),
125 ],
127def test_normalise_url(url: str, expected: str) -> None:
128 """
129 Tests for `normalise_url`.
130 """
131 assert normalise_url(url) == expected