Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

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...

The youtube-dl source is only so helpful (ie, not very).

Any suggestions how to script this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...