lollercoaster
lollercoaster

Reputation: 16493

Download only audio from youtube video using youtube-dl in python script

There's a few posts on downloading audio from YouTube using youtube-dl, but none of them are concrete or too helpful. I'm wondering what the best way to do it from a Python script is.

For example, here's the README example for downloading videos:

import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

Obviously if you just care about the audio, you'd rather not download the whole video...

Therefore, the youtube-dl source isn't very helpful

Any suggestions on how to script this?

Upvotes: 77

Views: 154304

Answers (5)

JaTh98
JaTh98

Reputation: 19

Although this post has not been modified for a long time, I've been working on it lately and built a GUI in python. There you can enter a yt link or a name and the corresponding audio will be downloaded.

https://github.com/JacobTh98/yt2audio.git

Upvotes: 1

Matt
Matt

Reputation: 265

I did not intend to answer this question, in fact, I came to find an answer for myself. In my search, none of these answers worked in a satisfactory manner. I did however find an excellent alternative that worked perfectly for me that I felt I should share: the module pytube.

from pytube import YouTube
import os

yt = YouTube('YOUR LINK HERE')

video = yt.streams.filter(only_audio=True).first()

out_file = video.download(output_path=".")

base, ext = os.path.splitext(out_file)
new_file = base + '.mp3'
os.rename(out_file, new_file)

Upvotes: 16

shekhar chander
shekhar chander

Reputation: 618

As youtube_dl is discontinued, you can use tube_dl

Usage :

pip install tube_dl
from tube_dl import Youtube

In your case, here's how to get only audio.

from tube_dl import Youtube
youtube('Your URL').formats.filter(only_audio=True)[0]**.download(convert='mp3')

** Instead of 0, you can use your favourite quality. Just type:

print(youtube('Your URL').formats.filter(only_audio=True))

This will print all the audios supported. Then specify index of your choice and download

EDIT:

filter is deprecated, use filter_by instead

Upvotes: 2

Pheon
Pheon

Reputation: 171

Use postprocessors argument. The list of all the available postprocessors can be found here.

If you want to pass additional ffmpeg or avconv options, which are not included in youtube-dl library (like audio bitrate - -ar <BR> in ffmpeg), add postprocessor_args as a list.

You can also prefer ffmpeg over avconv setting prefer_ffmpeg to True.

And to keep both original and converted audio file set 'keepvideo' to True.

For example:

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'wav',
        'preferredquality': '192'
    }],
    'postprocessor_args': [
        '-ar', '16000'
    ],
    'prefer_ffmpeg': True,
    'keepvideo': True
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

The list of all the available options is in the documentation. You can read ffmpeg posprocessor's code here.

And a less complex example is in their GitHub README.

Upvotes: 17

anon
anon

Reputation:

Read on in the developer instructions for an amended example:

from __future__ import unicode_literals
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(['http://www.youtube.com/watch?v=BaW_jenozKc'])

This will download an audio file if possible/supported. If the file is not mp3 already, the downloaded file be converted to mp3 using ffmpeg or avconv. For more information, refer to the format and postprocessors documentation entries in a current version of youtube-dl.

Upvotes: 114

Related Questions