129

I'm making a little graphic interface with Python 3 which should download a youtube video with its URL. I used the youtube_dl module for that. This is my code :

import youtube_dl # Youtube_dl is used for download the video

ydl_opt = {"outtmpl" : "/videos/%(title)s.%(ext)s", "format": "bestaudio/best"} # Here we give some advanced settings. outtmpl is used to define the path of the video that we are going to download

def operation(link):
    """
    Start the download operation
    """
    try:
        with youtube_dl.YoutubeDL(ydl_opt) as yd: # The method YoutubeDL() take one argument which is a dictionary for changing default settings
            video = yd.download([link]) # Start the download
        result.set("Your video has been downloaded !")
    except Exception:
        result.set("Sorry, we got an error.")

operation("https://youtube.com/watch?v=...")

When I execute my code, I get this error:

ERROR: YouTube said: Unable to extract video data

I saw here that it was because it doesn't find any video info, how can I resolve this problem?

MendelG
  • 14,885
  • 4
  • 25
  • 52
Bastien
  • 1,447
  • 3
  • 9
  • 19
  • 1
    You will get more traction with this question if you are able to boil it down a bit to the specific section that is throwing this error. I’m often worried about not including enough data but people are generally more likely to engage if its a simple question, versus something which at first glance looks like ‘do this for me’. I’m not casting any aspersions about you, but people will click, glance, and hit back on these quite a lot. – Solebay Sharp Sep 09 '20 at 17:53
  • Is the error raised for any video or a specific one? (I think I read somewhere on Reddit that this can happen because of the age-gate) – Minion Jim Sep 09 '20 at 18:13
  • @SolebaySharp Sorry it's just because I was not sure if something external to youtube_db could block my program – Bastien Sep 09 '20 at 19:13
  • @MinionJim I tested my program with multiple videos and always got the same error – Bastien Sep 09 '20 at 19:14
  • 1
    I just tested your code and it works except for the fact that the url is wrong. Firstly, you misspelt `https` (you have `htps`) and second it should be `/watch` rather than `?watch` so your operation call line would become `operation("https://youtube.com/watch?v=...")`. I assume this is just a typo with the question, but I hope this resolves it (I could not reproduce your error) – Minion Jim Sep 10 '20 at 09:10
  • Sorry it was only typing errors. It doesn't resolve the problem. I think the problem come from the windows interpreter because when I try to run my program directly in the windows command, it tells me "No module named youtube_dl" while I already installed it ... – Bastien Sep 10 '20 at 09:20
  • But I still don't know how to resolve this problem ... – Bastien Sep 10 '20 at 09:21
  • Ok, I'm running out of ideas :-( Do you have multiple Python interpreters installed (this is more for the ImportError than a solution to the question)? Is it because your path starts with a forward-slash (again, I presume you substitute `%(title)` with something as Windows won't like that as a path)? EDIT: in the future please @ me so it shows in my SO inbox thing – Minion Jim Sep 10 '20 at 10:26
  • @MinionJim No I'm only using python 3.8.3 – Bastien Sep 10 '20 at 10:28
  • And the path starting with a slash (does removing it fix the issue)? – Minion Jim Sep 11 '20 at 08:33
  • @MinionJim No same error ... – Bastien Sep 11 '20 at 10:12
  • As long as you don't get errors when you type `import youtube_dl` in your python shell there should be no problems with import. Can you download the video using `youtube-dl` on the command line? And why do you pass the link as a list `[]`? – Tom-Oliver Heidel Sep 13 '20 at 03:58
  • use **sudo youtube-dl link** to download video . Use the **sudo** before **youtube-dl** command. – Udesh Dec 26 '21 at 09:19

10 Answers10

207

Updating youtube-dl helped me. Depending on the way you installed it, here are the commands:

  • youtube-dl --update (self-update)
  • pip install -U youtube-dl (via python)
  • brew upgrade youtube-dl (macOS + homebrew)
  • choco upgrade youtube-dl (Windows + Chocolatey)
Benoît P
  • 3,179
  • 13
  • 31
Manoj D Bhat
  • 2,300
  • 1
  • 6
  • 11
  • 36
    `pip install -U youtube-dl` – yW0K5o Dec 12 '20 at 22:35
  • 11
    `brew upgrade youtube-dl` in my case. – vbwx Dec 19 '20 at 17:15
  • 2
    `choco upgrade youtube-dl` in my case – P. Pedrycz Jan 17 '21 at 14:07
  • 3
    If you're coming from Ubuntu/debian, you may as well not rely on the apt repository and instead install by another means, as documented on the youtube-dl github: https://github.com/ytdl-org/youtube-dl#installation – Norman Breau Feb 08 '21 at 20:35
  • 4
    Don't forget to run "youtube-dl --update" with writing "sudo" first, otherwise, you can get an error like: "ERROR: no write permissions on /usr/local/Cellar/youtube-dl/2020.03.24/bin/youtube-dl". It's caused by permission status, not by youtube-dl – Oguzhan Bolukbas Feb 17 '21 at 12:30
  • pip3 install iU youtube-dl for python3 – Kebab Programmer Apr 01 '21 at 20:03
  • 1
    In my case I use home-manager. I declared: `unstable = import (fetchGit { name = "nixpkgsDarwinSandbox"; url = "https://github.com/NixOS/nixpkgs"; ref = "refs/heads/master"; rev = "31014c4a2888cd4f782b0cba246f3b8036d63b30"; }) {}; ... home.packages = with pkgs; [ unstable.youtube-dl ... ] ` – yogsototh Apr 24 '21 at 08:23
  • Can also download an updated version here: [https://ytdl-org.github.io/youtube-dl/index.html](https://ytdl-org.github.io/youtube-dl/index.html) – Digger May 08 '21 at 20:34
  • @NormanBreau I normally find it is best to do the initial install with apt as that way all the dependancies install correctly, then you can use --update to allow it to work. – Benjamin Goodacre Aug 12 '21 at 06:36
  • ```youtube-dl: error: youtube-dl's self-update mechanism is disabled on Debian. ``` while updating – Lawhatre Nov 11 '21 at 01:58
  • There is an up2date snap package that works for me on KDE Neon, so probably any Ubuntu based distribution. – arkascha Oct 07 '22 at 12:33
  • i'm getting the error even after updating youtube-dl to the latest version! – shripal mehta Mar 11 '23 at 12:24
  • @shripalmehta Try the new replacement fork [yt-dlp](https://github.com/yt-dlp/yt-dlp). – Frank Breitling Jul 19 '23 at 17:21
92

For ubuntu users:

sudo apt purge youtube-dl 
sudo pip3 install youtube-dl
hash youtube-dl
Mikołaj Głodziak
  • 4,775
  • 7
  • 28
hurelhuyag
  • 1,691
  • 1
  • 15
  • 20
15

I had the same error on Ubuntu 20.04. I solved it by updating youtube-dl by downloading a .deb from: https://packages.debian.org/sid/all/youtube-dl/download

Though you can also get the update on youtube-dl's official site.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Andrés Heras
  • 159
  • 1
  • 3
12

The only thing that worked for me on Ubuntu was to install using the Debian package / .deb file:

wget http://ftp.de.debian.org/debian/pool/main/y/youtube-dl/youtube-dl_2021.02.04.1-1_all.deb
sudo apt install ./youtube-dl_2021.02.04.1-1_all.deb
HotDogCannon
  • 2,113
  • 11
  • 34
  • 53
  • 1
    @CharithJayasanka the `.deb` file gets updated every once and a while... just download & install the most recent one, such as (as of today) `http://ftp.de.debian.org/debian/pool/main/y/youtube-dl/youtube-dl_2021.02.10-1_all.deb` – HotDogCannon Mar 22 '21 at 12:08
9

Ubuntu Users:

The simplest & quickest way to solve this issue without running around and trying a thousand different solutions is to completely remove Youtube-dl and reinstall it using the .deb file & apt. First, purge it from your system.

sudo apt purge youtube-dl 

OR

sudo pip3 uninstall youtube-dl

Next, go HERE (http://ftp.us.debian.org/debian/pool/main/y/youtube-dl/youtube-dl_2021.12.17-1_all.deb) to download the .deb file. Once the file is downloaded, install using apt with the command below. This will solve your issue. Obviously you will make sure your version number & file name are correct.

sudo apt install ./youtube-dl_2021.12.17-1_all.deb

If this solution works for you PLEASE vote it up so that others can easily find it.

Marvo
  • 17,845
  • 8
  • 50
  • 74
Gregory Smitherman
  • 417
  • 3
  • 8
  • 16
8

Install yt-dlp as an alternative to youtube-dl

Save yourself time and install yt-dlp instead using pip with python 3.7+:

python -m pip install -U yt-dlp

then

yt-dlp video_url -o /path/to/output.mp4

for example:

yt-dlp https://www.youtube.com/watch?v=gKCvphbCpPE -o ~/Videos/my_video.mp4

Why use yt-dlp?

Quoted from the package repo:

yt-dlp is a youtube-dl fork based on the now inactive youtube-dlc. The main focus of this project is adding new features and patches while also keeping up to date with the original project

Worked for me after nearly 1 painful hour of searching.

William Le
  • 825
  • 1
  • 9
  • 16
  • Fails with the same error. So no. – ivanlan Feb 22 '23 at 23:57
  • @ivanlan have you uninstalled the previously installed youtube-dl? If you're on Ubuntu, try ```which youtube-dl``` to see if you still have youtube-dl installed. Use ```yt-dlp video_url```? – William Le Feb 23 '23 at 14:22
  • Tracked down the problem: the machine I was running it on is still using Python 3.6, and the latest, working versions of both yt-dlp & youtube-dl have dropped support for that version. I switched to a different machine with Python 3.8 & yt-dlp works. Haven't tried with youtube-dl, but since yt-dlp is behaving correctly I might not bother. – ivanlan Feb 24 '23 at 15:34
  • 1
    This worked. Many other solutions mentioned here failed for me. – AndyJost Apr 03 '23 at 23:28
7

If you are using youtube-dl command line on MacOsx update using this command :

sudo youtube-dl --update

Abderrazzak Nejeoui
  • 1,496
  • 12
  • 9
  • 1
    Please add a comment instead of writing a new answer. Because it answered already by Manoj D Bhat, Dec 10 '20 at 8:30, 11 days before yours. – Oguzhan Bolukbas Feb 17 '21 at 12:33
6

If you have pip installed you can use it to update youtube-dl that helped me.

sudo pip install --upgrade youtube_dl

Hamza
  • 171
  • 1
  • 10
4

You could try adding a cookie file as some videos are age restricted. Use this plugin Chrome plugin Cookie.txt to download your cookies in a txt file then use these --cookies /path/to/cookies/file.txt flags not forgetting to put the right path to the file of your cookies.txt.

Sample:

youtube-dl -n --cookies ~/Downloads/cookies.txt https://www.youtube.com/watch\?v\=h7Ii7KKapig

Surce

HugoBraga
  • 41
  • 2
1

The youtube-dl package is using python code and it's looking for the correct python version to run. If you have python3 then enter:

sudo sed -i '1s/python/python3/' /usr/local/bin/youtube-dl

outerSpace
  • 137
  • 3