--extract-audio等效于YoutubeDL类?

时间:2015-06-14 02:16:05

标签: python youtube youtube-dl

我想从youtube链接中简单地提取MP3格式的音频,但如果不使用youtube-dl选项从命令行调用--extract-audio,我无法弄清楚如何执行此操作。有没有办法在YoutubeDL类中执行此操作,类似于给定here的示例?

这是我到目前为止所拥有的,但它仍会创建一个mp4文件。

import youtube_dl

# download using optimal audio settings
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    'quiet': True,
    'restrictfilenames': True}

ydl = youtube_dl.YoutubeDL(ydl_opts)
ydl.download(['https://www.youtube.com/watch?v=Pnt2cy5MWyw'])

澄清:以上代码会生成一个MP3文件,但它也会生成一个MP4视频文件。我希望它生成MP3。

更新:以下是安静设置为False时的输出示例:

[youtube] et6sSlEn8LE: Downloading webpage
[youtube] et6sSlEn8LE: Extracting video information
[youtube] et6sSlEn8LE: Downloading DASH manifest
[download] Destination: downloaded_tracks/et6sSlEn8LE.m4a

[download]   0.0% of 7.63MiB at 47.07KiB/s ETA 02:46
[download]   0.0% of 7.63MiB at 138.74KiB/s ETA 00:56
[download]   0.1% of 7.63MiB at 319.81KiB/s ETA 00:24
[download]   0.2% of 7.63MiB at 679.31KiB/s ETA 00:11
[download]   0.4% of 7.63MiB at  1.12MiB/s ETA 00:06
[download]   0.8% of 7.63MiB at  1.19MiB/s ETA 00:06
[download]   1.6% of 7.63MiB at  1.65MiB/s ETA 00:04
[download]   3.3% of 7.63MiB at  2.43MiB/s ETA 00:03
[download]   6.5% of 7.63MiB at  3.38MiB/s ETA 00:02
[download]  13.1% of 7.63MiB at  4.32MiB/s ETA 00:01
[download]  26.2% of 7.63MiB at  4.96MiB/s ETA 00:01
[download]  52.4% of 7.63MiB at  5.15MiB/s ETA 00:00
[download] 100.0% of 7.63MiB at  6.87MiB/s ETA 00:00
[download] 100% of 7.63MiB in 00:01
[ffmpeg] Correcting container in "downloaded_tracks/et6sSlEn8LE.m4a"
[avconv] Destination: downloaded_tracks/et6sSlEn8LE.mp3
Deleting original file downloaded_tracks/et6sSlEn8LE.m4a (pass -k to keep)

1 个答案:

答案 0 :(得分:0)

此代码应该可以正常工作。如果将quiet设置为False,输出结果是什么?确保您使用的是最新版本的youtube-dl并安装了ffmpeg / avconv。 ffmpeg / avconv应该在您的PATH中,或者您可以通过将'ffmpeg_location': '/path/to/directory/containing/ffmpeg/or/avconv'添加到ydl_opts来明确指定位置。