Search code examples
pythonyoutube-dl

youtube_dl get audio link with Python


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


Solution

  • 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