1

I'm trying to retrieve the link for the audio only file on youtube using youtube_dl. I wonder if it's possible to do so. I'm using youtube_dl as in python code not with terminal.

Many thanks

Shiro Guo
  • 25
  • 1
  • 3
  • There usually isn't a separate audio file. `youtube-dl` extracts the audio from the video with `ffmpeg`. – Blender Mar 13 '18 at 00:51
  • Youtube-dl will offer audio-only if it is available. The -F flag will indicate what formats are available. – Alan Mar 13 '18 at 00:56
  • @Alan OP doesn't want to use the terminal, so -F flag isn't useful. He talking about using the python library `youtube_dl` – francium Mar 13 '18 at 00:57
  • Yes, aware of that; the comment was correcting Blender's incorrect information for future reference. – Alan Mar 13 '18 at 00:58
  • @Alan: I guess I didn't word the second part well. I didn't mean to imply that it always extracts it from the video file, I just rarely encounter YouTube links where the audio isn't just extracted from the video stream. – Blender Mar 13 '18 at 01:03
  • *Why* do you want the URL? If you actually want to download it, there are some pretty important caveats, [which I outlined in my answer](https://stackoverflow.com/a/49249893/35070). –  Mar 13 '18 at 06:53

3 Answers3

6

Only a very small minority of the websites supported by youtube-dl actually serve audio-only files. But some do, including most videos on YouTube. For these, you can request the filetype bestaudio and extract the information instead of downloading:

from __future__ import unicode_literals 
import youtube_dl

ydl_opts = {
    'format': 'bestaudio',
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info = ydl.extract_info(
        'https://www.youtube.com/watch?v=BaW_jenozKc', download=False)
    print(info['formats'][0]['url'])

Note that this will get you the correct URL. However, there is no guarantee that this URL will work, if you change anything of:

  • The IP address using the URL (in other words, if you transfer this URL to another machine and try to download from there, it won't work).
  • The HTTP headers (User-Agent and such). Can be found in the http_headers key of the format dictionary.
  • The cookies (can be found in ydl.cookiejar).

Which of these constrains have to be met depends on the video, and is subject to sudden change. For instance, it looks like at the moment the URL is enough for many YouTube videos, but YouTube has definitely blocked other IPv4 addresses and even all different IP addresses for some videos, or only music and other highly monetized videos, over time.

Also note that the file you'll get may be in a strange format. For instance, YouTube used to send invalid m4a files which could not be read by most players. You'll often get opus, which may not be supported everywhere. If you just want the audio file, it's better to let youtube-dl download and convert it, as described in the documentation and other answers.

5

If you want to download the audio only, you can do this,

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    'outtmpl': '%(title)s.%(etx)s',
    'quiet': False
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])  # Download into the current working directory

This is a snippet of code I've taken out of a project I worked on. You can see the full source here:https://github.com/francium/audiosave/blob/master/audiosave.py

You can see the API and docs for youtube_dl here: https://github.com/rg3/youtube-dl/blob/master/README.md#embedding-youtube-dl

francium
  • 2,384
  • 3
  • 20
  • 29
  • 1
    Suggest you add a link to the official documentation for any follow-up: https://github.com/rg3/youtube-dl/blob/master/README.md#embedding-youtube-dl – Alan Mar 13 '18 at 00:59
  • Hum is there a way to retrieve the url of the audio only link without actually downloading it? – Shiro Guo Mar 13 '18 at 01:11
  • @ShiroGuo not off the top of my head. You'll have to reference the API docs. Based on what I understand, the youtube HTML actually contains the links and I would imagine youtube_dl just scrapes the links in that HTML. You can try this out by going to a youtube page, viewing the source and searching for 'googlevideo'. The HTML stores all the data in JSON. – francium Mar 13 '18 at 01:21
  • 1
    @francium As the long-time lead developer of youtube-dl, I can only say I wish it were quite that easy ;). Also bear in mind that youtube-dl [supports a lot more websites than just YouTube](https://rg3.github.io/youtube-dl/supportedsites.html). And even YouTube has more than a dozen of different formats (searches, playlists, channels, monetized videos, live streams, restricted videos, ...). –  Mar 13 '18 at 06:51
  • Is it possible to force VBR over CBR with that bitrate? – lamino Mar 19 '19 at 03:04
  • @lamino I'm not sure. You may wanna pose this as a separate question unless someone else in this thread provides you with an answer – francium Mar 19 '19 at 15:11
  • 1
    I figured it out by looking at the source code. For anyone interested, any value below 10 implies VBR encoding – lamino Mar 19 '19 at 20:55
0

This is a duplicate of an existing question here: download only audio from youtube video using youtube-dl in python script

However, I will be helpful and share the answer here. Use this code:

import youtube_dl


ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['insert youtube link here'])

It should convert the audio to an mp3 using ffmpeg. Refer here for further examples and instructions.

chaNcharge
  • 239
  • 1
  • 2
  • 12
  • 1
    Hum is there a way to retrieve the url of the audio only link without actually downloading it? – Shiro Guo Mar 13 '18 at 01:11
  • 1
    Yes, also shown in the examples in the README, there's a link that directs you to youtube_dl/YoutubeDL.py and it'll get you down to the list of available options. I believe `simulate` or `skip_download` may be what you're looking for. – chaNcharge Mar 13 '18 at 01:18