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

前端 未结 2 836
温柔的废话
温柔的废话 2020-11-28 21:55

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

2条回答
  •  鱼传尺愫
    2020-11-28 22:21

    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.

提交回复
热议问题