如何使用youtube-dl从YouTube视频下载mp3曲目


从YouTube下载MP3曲目有多种方法,例如Web应用程序、Windows应用程序和Linux中的命令行工具。youtube-DL是市场上最好的下载YouTube音轨工具之一。它是用Python编程语言开发的。该程序几乎可以在所有Linux发行版中使用。如果您从未听说过此工具,那么本文就是为您准备的。

安装youtube-DL

要安装youtube-DL,请使用以下命令:

$ sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl

示例输出应如下所示:

--2016-03-08 11:41:21-- https://yt-dl.org/downloads/latest/youtube-dl
Resolving yt-dl.org (yt-dl.org)... 95.143.172.170, 2001:1a50:11:0:5f:8f:acaa:177
Connecting to yt-dl.org (yt-dl.org)|95.143.172.170|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://yt-dl.org/downloads/2016.03.06/youtube-dl [following]
--2016-03-08 11:41:23-- https://yt-dl.org/downloads/2016.03.06/youtube-dl
Connecting to yt-dl.org (yt-dl.org)|95.143.172.170|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1133512 (1.1M) [application/octet-stream]
Saving to: ‘/usr/local/bin/youtube-dl’
100%[======================================>] 11,33,512 218KB/s in 6.2s
2016-03-08 11:41:30 (180 KB/s) - ‘/usr/local/bin/youtube-dl’ saved [1133512/1133512]

更改youtube-dl目录权限以访问youtube-dl命令。要更改目录权限,请使用以下命令:

$ sudo chmod a+rx /usr/local/bin/youtube-dl

要获取有关youtube-DL的更多信息,请使用以下命令:

# youtube-dl --help

上述命令需要root权限,其输出应如下所示:

Options:

-h, --help                  Print this help text and exit
--version                   Print program version and exit
-U, --update                Update this program to latest version. Make sure that you have sufficient permissions (run with sudo if needed)
-i, --ignore-errors         Continue on download errors, for example to skip unavailable videos in a playlist
--abort-on-error            Abort downloading of further videos (in the playlist or the command line) if an error occurs
--dump-user-agent           Display the current browser identification
--list-extractors           List all supported extractors
--extractor-descriptions    Output descriptions of all supported extractors
--force-generic-extractor   Force extraction to use the generic extractor
--default-search PREFIX     Use this prefix for unqualified URLs. For example "gvsearch2:" downloads two videos from google videos for youtube-dl "large apple". Use the value "auto" to let youtube-dl guess ("auto_warning" to emit a warning when guessing). "error" just throws an error. The default value "fixup_error" repairs broken URLs, but emits an error if this is not possible instead of searching.
--ignore-config             Do not read configuration files. When given in the global configuration file /etc /youtube-dl.conf:           Do not read the user configuration in ~/.config/youtube- dl/config (%APPDATA%/youtube-dl/config.txt on Windows)
--flat-playlist             Do not extract the videos of a playlist, only list them.
--mark-watched              Mark videos watched (YouTube only)
--no-mark-watched           Do not mark videos watched (YouTube only)
--no-color                  Do not emit color codes in output
.........................

将视频下载为MP3曲目

要将音乐转换为不同的音频格式,youtube-dl需要ffmpegavconv及其帮助程序ffprobeavprobe

要安装其中一个,请使用以下命令:

$ sudo apt-get install ffmpeg

示例输出应如下所示:

Building dependency tree
Reading state information... Done
ffmpeg is already the newest version.
The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
libgphoto2-port10:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386
libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386
libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386
libhx509-5-heimdal:i386 libice6:i386 libieee1284-3:i386
..........................

要下载MP3曲目,请使用以下命令:

$ youtube-dl -x --audio-format mp3 --prefer-ffmpeg https://www.youtube.com/watch?v=jwD4AEVBL6Q

示例输出应如下所示:

[youtube] jwD4AEVBL6Q: Downloading webpage
[youtube] jwD4AEVBL6Q: Downloading video info webpage
[youtube] jwD4AEVBL6Q: Extracting video information
[youtube] jwD4AEVBL6Q: Downloading MPD manifest
[download] Destination: Bad Style - Time back-jwD4AEVBL6Q.m4a
[download] 100% of 7.45MiB in 00:08
[ffmpeg] Correcting container in "Bad Style - Time back-jwD4AEVBL6Q.m4a"
[ffmpeg] Destination: Bad Style - Time back-jwD4AEVBL6Q.mp3
Deleting original file Bad Style - Time back-jwD4AEVBL6Q.m4a (pass -k to keep)

上述命令使用以下两个选项:

–extract-audio (简短选项 -x) – 将视频文件转换为仅音频文件。–audio-format – 指定将下载文件的音频格式。支持的音频格式为“best”、“aac”、“vorbis”、“mp3”、“m4a”、“opus”或“wav”;默认情况下设置为“best”。

从播放列表下载多个文件

要从播放列表下载多个文件,请使用以下命令:

[youtube:playlist] PL9LUD5Kp855InMnKTaRy3LH3kTIYJyBzs: Downloading webpage
[download] Downloading playlist: Top Tracks - Imagine Dragons
[youtube:playlist] playlist Top Tracks - Imagine Dragons: Downloading 5 videos
[download] Downloading video 1 of 5
[youtube] mWRsgZuwf_8: Downloading webpage
[youtube] mWRsgZuwf_8: Downloading video info webpage
[youtube] mWRsgZuwf_8: Extracting video information
[youtube] mWRsgZuwf_8: Downloading js player en_US-vflteKQR7
[youtube] mWRsgZuwf_8: Downloading js player en_US-vflteKQR7
[download] Destination: Imagine Dragons - Demons (Official)-mWRsgZuwf_8.webm
[download] 100% of 3.38MiB in 00:06
[ffmpeg] Destination: Imagine Dragons - Demons (Official)-mWRsgZuwf_8.mp3
Deleting original file Imagine Dragons - Demons (Official)-mWRsgZuwf_8.webm (pass -k to keep)
[download] Downloading video 2 of 5
[youtube] ktvTqknDobU: Downloading webpage
[youtube] ktvTqknDobU: Downloading video info webpage
[youtube] ktvTqknDobU: Extracting video information
[youtube] ktvTqknDobU: Downloading js player en_US-vflteKQR7
[download] Destination: Imagine Dragons - Radioactive-ktvTqknDobU.webm
[download] 100% of 3.81MiB in 00:01
[ffmpeg] Destination: Imagine Dragons - Radioactive-ktvTqknDobU.mp3
Deleting original file Imagine Dragons - Radioactive-ktvTqknDobU.webm (pass -k to keep)
[download] Downloading video 3 of 5
[youtube] sENM2wA_FTg: Downloading webpage
[youtube] sENM2wA_FTg: Downloading video info webpage
[youtube] sENM2wA_FTg: Extracting video information
[download] Destination: Imagine Dragons - It's Time-sENM2wA_FTg.webm
[download] 100% of 3.81MiB in 00:01
[ffmpeg] Destination: Imagine Dragons - It's Time-sENM2wA_FTg.mp3
Deleting original file Imagine Dragons - It's Time-sENM2wA_FTg.webm (pass -k to keep)
[download] Downloading video 4 of 5
[youtube] w5tWYmIOWGk: Downloading webpage
[youtube] w5tWYmIOWGk: Downloading video info webpage
[youtube] w5tWYmIOWGk: Extracting video information
[download] Destination: Imagine Dragons - On Top Of The World (Official Music Video)-w5tWYmIOWGk.webm
[download] 100% of 3.92MiB in 00:03
[ffmpeg] Destination: Imagine Dragons - On Top Of The World (Official Music Video)-w5tWYmIOWGk.mp3
Deleting original file Imagine Dragons - On Top Of The World (Official Music Video)-w5tWYmIOWGk.webm (pass -k to keep)
[download] Downloading video 5 of 5
[youtube] VP9I8ZUwKpc: Downloading webpage
[youtube] VP9I8ZUwKpc: Downloading video info webpage
[youtube] VP9I8ZUwKpc: Extracting video information
[download] Destination: Imagine Dragons - Warriors (Lyric Video)-VP9I8ZUwKpc.webm
[download] 100% of 2.57MiB in 00:01
[ffmpeg] Destination: Imagine Dragons - Warriors (Lyric Video)-VP9I8ZUwKpc.mp3
Deleting original file Imagine Dragons - Warriors (Lyric Video)-VP9I8ZUwKpc.webm (pass -k to keep)
[download] Finished downloading playlist: Top Tracks - Imagine Dragons

恭喜!现在,您已经知道“如何使用YouTube-DL从YouTube视频下载MP3曲目”。我们将在下一篇文章中学习更多关于此类Linux命令的内容。继续关注!

更新于:2019年10月21日

3K+ 次浏览

启动您的职业生涯

完成课程后获得认证

开始学习
广告