【问题标题】:youtube-dl is not merging audio and video when embedded in Python Script嵌入 Python 脚本时,youtube-dl 不合并音频和视频
【发布时间】:2018-09-26 12:28:07
【问题描述】:

我正在尝试从 Python 脚本中下载特定 YouTube 视频的最高质量音频和视频。

我的代码非常简单:

import youtube_dl
ydl_opts = {
    'format': 'bestvideo[width>=1920]/bestvideo+bestaudio/best',
    'outtmpl': 'test.mp4',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])

格式行取自this question

我遇到的问题是下载的结果没有音频组件。视频组件按预期工作。控制台输出似乎也没有表明音频已下载。

C:\Dev>py youtube_test.py
[youtube] BaW_jenozKc: Downloading webpage
[youtube] BaW_jenozKc: Downloading video info webpage
[youtube] BaW_jenozKc: Extracting video information
[download] Destination: test.mp4
[download] 100% of 2.11MiB in 00:00

为什么我没有通过测试视频获得音频组件,我该如何解决?

我在 Windows 10 上运行。我已安装 ffmpeg 并在我的路径中。

C:\Dev>ffmpeg -version
ffmpeg version N-90721-g783df2eb59 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7.3.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
libavutil      56. 13.100 / 56. 13.100
libavcodec     58. 17.100 / 58. 17.100
libavformat    58. 11.101 / 58. 11.101
libavdevice    58.  2.100 / 58.  2.100
libavfilter     7. 15.100 /  7. 15.100
libswscale      5.  0.102 /  5.  0.102
libswresample   3.  0.101 /  3.  0.101
libpostproc    55.  0.100 / 55.  0.100

【问题讨论】:

    标签: python youtube-dl


    【解决方案1】:

    您的代码中的格式和输出模板有误。让我们从格式开始:您的规范bestvideo[width>=1920]/bestvideo+bestaudio/best 说:

    1. 如果存在且宽度 >= 1920,请选择最佳视频格式。
    2. 否则,请选择最佳视频格式和最佳音频格式,然后将它们组合起来。
    3. 否则,请选择包含音频和视频的最佳文件。

    只需删除第一项并传入bestvideo+bestaudio/best

    请注意,生成的文件可能不是 mp4,因此您还应该在输出模板中使用 %(ext)s。总之,使用这个:

    import youtube_dl
    ydl_opts = {
        'format': 'bestvideo+bestaudio/best',
        'outtmpl': 'test.%(ext)s',
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
    

    【讨论】:

    • 不是合并文件。它下载两个单独的文件。
    猜你喜欢
    • 2015-02-12
    • 2021-05-22
    • 2012-02-01
    • 2021-08-05
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多