使用Python嵌入youtube-dl 2020.3.24下载YouTube视频时遭遇“Unable to extract video data”错误求助
Hey there, let's break down what's going wrong here:
The Root Cause
Your youtube-dl version (2020.3.24) is way out of date—and here's why that matters:
youtube-dlstopped active development back in 2021. Since then, YouTube has repeatedly updated its page structure, anti-scraping mechanisms, and video data APIs.- Old versions like 2020.3.24 simply don't know how to parse YouTube's current webpage layout, hence the "Unable to extract video data" error.
The Fix: Switch to yt-dlp
The best solution is to use yt-dlp—an actively maintained fork of youtube-dl that keeps up with YouTube's constant changes. Here's how to make the switch:
Uninstall the outdated youtube-dl
Run this command in your terminal:pip3 uninstall youtube-dlInstall yt-dlp
Use pip to install the latest version:pip3 install yt-dlpUpdate your code
The API is almost identical to youtube-dl, so you only need to change the import line. Here's your modified script:# ydl1.py from __future__ import unicode_literals import yt_dlp ydl_opts = {} with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])Run the script again
Execute your updatedydl1.py—it should now download the video without issues.
Bonus: Customize Your Downloads (Optional)
yt-dlp supports all the old youtube-dl options plus tons more. For example, to download the highest quality combined audio+video, you could update ydl_opts like this:
ydl_opts = { 'format': 'bestvideo+bestaudio/best', }
内容的提问来源于stack exchange,提问作者Tiel

