我正在尝试从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
答案 0 :(得分:3)
代码中的格式和输出模板错误。让我们从格式开始:您的规范bestvideo[width>=1920]/bestvideo+bestaudio/best
说:
只需删除第一个字词并传入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'])