当前位置: 首页>>技术教程>>正文


如何使用youtube-dl下载youtube视频作为最佳音质mp3

, , , , ,

问题描述

我想以最佳质量下载YouTube视频作为mp3文件。以下代码有助于下载youtube视频为m4a但不是我们mp3

youtube-dl -f bestaudio --audio-quality 0 --audio-format mp3 https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_

你能告诉我怎么用bestaudio参数下载YouTube视频为mp3

最佳解决思路

来自man youtube-dl

-x, --extract-audio                  Convert video files to audio-only files (requires ffmpeg or avconv and ffprobe or avprobe)
    --audio-format FORMAT            Specify audio format: "best", "aac", "vorbis", "mp3", "m4a", "opus", or "wav"; "best" by default
    --audio-quality QUALITY          Specify ffmpeg/avconv audio quality, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default

所以你的命令可能是:

youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 <Video-URL>

--audio-quality 0使用最高的编码质量,但可能会不必要地增加文件大小。默认值为5,可能是更好的选择,具体取决于源质量。

因此,如果质量和文件大小对您很重要,您应该避免使用re-encoding并使用Youtube的原生音乐格式:

youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <Video-URL>

导致m4a文件或

youtube-dl -f bestaudio --extract-audio --embed-thumbnail --add-metadata <Video-URL>

可能导致ogg文件。

您可以列出可用的格式

youtube-dl -F <Video-URL>

次佳解决思路

我建议使用Pafy(Python),非常容易获得音频链接,如果你愿意,你可以直接下载:

#python3
import pafy
video = pafy.new('video id or video url')
bestaudio = video.getbestaudio()
bestaudio.bitrate #get bit rate
bestaudio.extension #extension of audio fileurl
...
bestaudio.url #get url
...
#download if you want
bestaudio.download()

第三种解决思路

如果你只想要mp3文件:

youtube-dl -f bestaudio  "https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_" --exec "ffmpeg -i {}  -codec:a libmp3lame -qscale:a 0 {}.mp3 && rm {} "

如果您不想删除原始文件:

youtube-dl -f bestaudio  "https://www.youtube.com/playlist?list=PLYRruMbyFRcBVdVN8v4FNkIKkXvL-bZn_" --exec "ffmpeg -i {}  -codec:a libmp3lame -qscale:a 0 {}.mp3 "

第四种思路

还值得注意的是man youtube-dl中的其他部分(在线拷贝可以找到here):

(对于那些思考长话短说,只读第4段,你可以使用youtube-dl -x URL,因为它应该下载最好的质量:)

youtube-dl -f bestaudio URL也是我的经验!但是,大多数情况下,这两种选项都需要使用ffmpeg或类似的方式转换为MP3 – 因为您已经在编辑文件,所以您也可以使用tag it(使用旧的ID3版本以获得大多数设备兼容性!)。

FORMAT SELECTION

By default youtube-dl tries to download the best quality, but sometimes you may want to download other format. The simplest case is requesting a specific format, for example -f 22. You can get the list of available formats using --list-formats, you can also use a file extension (currently it supports aac, m4a, mp3, mp4, ogg, wav, webm) or the special names best, bestvideo, bestaudio and worst.

If you want to download multiple videos and they don’t have the same formats available, you can specify the order of preference using slashes, as in-f 22/17/18. You can also filter the video results by putting a condition in brackets, as in -f "best[height=720]" (or -f "[filesize>10M]"). This works for filesize, height, width, tbr, abr, vbr, asr, and fps and the comparisons <, <=, >, >=, =, != and for ext, codec, vcodec, container, and protocol and the comparisons =, != . Formats for which the value is not known are excluded unless you put a question mark (?) after the operator. You can combine format filters, so -f "[height <=? 720][tbr>500]" selects up to 720p videos (or videos where the height is not known) with a bitrate of at least 500 KBit/s.

Use commas to download multiple formats, such as -f 136/137/mp4/bestvideo,140/m4a/bestaudio. You can merge the video and audio of two formats into a single file using -f <video-format>+<audio-format> (requires ffmpeg or avconv), for example -f bestvideo+bestaudio.

Since the end of April 2015 and version 2015.04.26 youtube-dl uses -f bestvideo+bestaudio/best as default format selection (see #5447, #5456). If ffmpeg or avconv are installed this results in downloading bestvideo and bestaudio separately and muxing them together into a single file giving the best overall quality available. Otherwise it falls back to best and results in downloading best available quality served as a single file. best is also needed for videos that don’t come from YouTube because they don’t provide the audio and video in two different files. If you want to only download some dash formats (for example if you are not interested in getting videos with a resolution higher than 1080p), you can add -f bestvideo[height<=?1080]+bestaudio/best to your configuration file. Note that if you use youtube-dl to stream to stdout (and most likely to pipe it to your media player then), i.e. you explicitly specify output template as -o -, youtube-dl still uses -f best format selection in order to start content delivery immediately to your player and not to wait until bestvideo and bestaudio are downloaded and muxed.

If you want to preserve the old format selection behavior (prior to youtube-dl 2015.04.26), i.e. you want to download best available quality media served as a single file, you should explicitly specify your choice with -f best. You may want to add it to the configuration file (#configuration) in order not to type it every time you run youtube-dl.

Can you please put the -b option back?

Most people asking this question are not aware that youtube-dl now defaults to downloading the highest available quality as reported by YouTube, which will be 1080p or 720p in some cases, so you no longer need the -b option. For some specific videos, maybe YouTube does not report them to be available in a specific high quality format you’re interested in. In that case, simply request it with the -f option and youtube-dl will try to download it.

Youtube(可能因为它应该是一个视频播放网站)也显然压缩音频(见here)低至120~KB,这是相当低的,但在大多数情况下似乎没问题。

要比较下载视频,您可以查询下载的视频/音轨(您可能需要使用avprobe而不是ffprobe)来查看哪个最佳:

$ ffprobe -hide_banner output-output-loads-of-options.webm
Input #0, matroska,webm, from 'output-output-loads-of-options.webm':
  Metadata:
    encoder         : google
  Duration: 00:04:02.63, start: 0.000000, bitrate: 118 kb/s
    Stream #0:0(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
$ ffprobe -hide_banner output_with_just_-x.ogg 

Input #0, ogg, from 'output_with_just_-x':
  Duration: 00:04:02.65, start: 0.000000, bitrate: 117 kb/s
    Stream #0:0(eng): Audio: vorbis, 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      LANGUAGE        : eng
      ENCODER         : Lavf56.4.101

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2991.html,未经允许,请勿转载。