2Tests for yt-dlp_alexwlchan.
8from pymediainfo import MediaInfo
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:
19 Download a YouTube video and check we get the expected output.
21 video_info = download_video("https://www.youtube.com/watch?v=TUQaGhPdlxs")
25 == '"new york city, manhattan, people" - Free Public Domain Video'
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:
40 Paths of YouTube videos get cleaned up during the download.
42 video = download_video("https://www.youtube.com/shorts/eso8JB7q0a0")
44 video["title"] == "3D Printing Everyday for 365 Days 176/365 "
45 "#stem #3dprinting #3dprint #ideas #useful"
48 os.path.basename(video["video_path"]) == "3D Printing Everyday for 365 Days "
49 "176-365 stem 3dprinting 3dprint ideas useful [eso8JB7q0a0].mp4"
53def test_youtube_video_does_not_http429() -> None:
55 Download a YouTube video without any subtitles or spoken audio.
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:
63 Download a YouTube video with automatic subtitles.
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:
71 Downloaded a YouTube video ignores AI upscaled versions.
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:
85 Download an Instagram video and check we get the expected output.
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"
96 video_info["uploader"]["url"] == "https://www.instagram.com/publicdomaingems/"
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(
110 "https://www.instagram.com/reel/DMWY8KkOS0n/",
111 "https://www.instagram.com/reel/DMWY8KkOS0n/",
114 "https://www.youtube.com/shorts/hyGluE562oA",
115 "https://www.youtube.com/shorts/hyGluE562oA",
118 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
119 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
122 "https://www.youtube.com/watch?v=0N1_0SUGlDQ&app=desktop&list=LL&index=43",
123 "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
127def test_normalise_url(url: str, expected: str) -> None:
129 Tests for `normalise_url`.
131 assert normalise_url(url) == expected