Skip to main content

youtube: replace /embed/ URLs with /watch?v= URLs

ID
edd1044
date
2026-08-01 22:19:15+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
5af6529
message
youtube: replace /embed/ URLs with /watch?v= URLs
changed files
2 files, 15 additions

Changed files

test_yt-dlp_alexwlchan.py (4085 → 4222)

diff --git a/test_yt-dlp_alexwlchan.py b/test_yt-dlp_alexwlchan.py
index eb6b527..643151f 100644
--- a/test_yt-dlp_alexwlchan.py
+++ b/test_yt-dlp_alexwlchan.py
@@ -120,6 +120,10 @@ def test_instagram_video() -> None:
             "https://www.youtube.com/watch?v=0N1_0SUGlDQ&app=desktop&list=LL&index=43",
             "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
         ),
+        (
+            "https://www.youtube.com/embed/0N1_0SUGlDQ",
+            "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
+        ),
     ],
 )
 def test_normalise_url(url: str, expected: str) -> None:

yt-dlp_alexwlchan.py (9064 → 9463)

diff --git a/yt-dlp_alexwlchan.py b/yt-dlp_alexwlchan.py
index d352239..31d74a4 100755
--- a/yt-dlp_alexwlchan.py
+++ b/yt-dlp_alexwlchan.py
@@ -59,6 +59,17 @@ def normalise_url(url: str) -> str:
     """
     u = urllib.parse.urlsplit(url)
 
+    # If it's a YouTube embed URL, change it to the watch?v= form
+    if (
+        u.netloc == "www.youtube.com"
+        and u.path.startswith("/embed/")
+        and len(u.path.split("/")) == 3
+    ):
+        video_id = u.path.split("/")[-1]
+        qs = [("v", video_id)]
+        query = urllib.parse.urlencode(qs)
+        return urllib.parse.urlunsplit((u.scheme, u.netloc, "/watch", query, ""))
+
     # If it's a YouTube URL, remove all query parameters except video ID (v)
     if u.netloc == "www.youtube.com":
         qs = urllib.parse.parse_qsl(u.query)