如何使用 youtube-dl 只下载音频
音频 youtube-dl
![](/data/attachment/album/202109/05/153110dkamc1kv0173ggc3.jpg)


[youtube-dl](https://github.com/ytdl-org/youtube-dl) 是一个多功能的命令行工具，用于从 YouTube 和许多其他网站下载视频。我用它来做我自己的 YouTube 视频的备份。


默认情况下，你会 [使用 youtube-dl 下载视频](https://itsfoss.com/download-youtube-linux/)。用 youtube-dl 只提取音频怎么样？ 其实很简单。让我告诉你步骤。



> 
> **注意**
> 
> 
> 从网站下载视频可能违反他们的政策。这取决于你是否选择下载视频或音频。
> 
> 
> 


### 使用 youtube-dl 只下载音频


请确保你已经在你的 Linux 发行版上安装了 `youtube-dl`。



```
sudo snap install youtube-dl

```

如果你只想从 YouTube 视频中下载音频，你可以使用 `youtube-dl` 的 `-x` 选项。这个提取音频的选项将视频文件转换为纯音频文件。



```
youtube-dl -x video_URL

```

该文件被保存在你运行 `youtube-dl` 命令的同一目录下。


这是我下载 Zorin OS 16 评论视频的画外音的示例：



```
youtube-dl -x https://www.youtube.com/watch?v=m_PmLG7HqbQ
[youtube] m_PmLG7HqbQ: Downloading webpage
[download] Destination: Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a
[download] 100% of 4.26MiB in 00:03
[ffmpeg] Correcting container in "Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a"
[ffmpeg] Post-process file Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a exists, skipping

```

你注意到音频格式了吗？它是 .m4a 格式。你可以把音频格式指定为你所选择的格式。


比如你想提取 MP3 格式的音频。你可以像这样使用它：



```
youtube-dl -x --audio-format mp3 video_URL

```

下面是我之前展示的同一个例子。你可以看到它 [使用 ffmpeg 转换](https://itsfoss.com/ffmpeg/) m4a 文件为 mp3：



```
youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=m_PmLG7HqbQ
[youtube] m_PmLG7HqbQ: Downloading webpage
[download] Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a has already been downloaded
[download] 100% of 4.26MiB
[ffmpeg] Correcting container in "Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a"
[ffmpeg] Destination: Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.mp3
Deleting original file Zorin OS 16 Review - It's a Visual Masterpiece-m_PmLG7HqbQ.m4a (pass -k to keep)

```

### 以 MP3 格式下载整个 YouTube 播放列表


是的，你完全可以这样做。最主要的是要在这里得到播放列表的 URL。它通常是以下格式：



```
https://www.youtube.com/playlist?list=XXXXXXXXXXXXXXXXXXX

```

要获得一个播放列表的 URL，当播放列表显示在右边栏时，点击其名称。


![Click on the playlist title](/data/attachment/album/202109/05/153203ey4rms8zss5r4vzz.png)


它将带你到播放列表页面，你可以在这里复制 URL。


![Grab the playlist URL](/data/attachment/album/202109/05/153204rcqliukzjiy2x2cz.png)


现在你有了播放列表的 URL，你可以用它来下载 MP3 格式的音频文件，方法如下：



```
youtube-dl --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" playlist_URL

```

那个看起来很可怕的 `-o "%(title)s.%(ext)s"` 指定了输出文件（选项 `-o`），并指示它使用视频的标题和扩展名（本例为 mp3）来命名音频文件。


![](/data/attachment/album/202109/05/153206gkkraao6xqknqxxf.png)


我希望你觉得这个技巧对你有帮助。享受音频文件吧。




---


via: <https://itsfoss.com/youtube-dl-audio-only/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

[youtube-dl](https://github.com/ytdl-org/youtube-dl?ref=itsfoss.com) is a versatile command line tool for downloading videos from YouTube and many other websites. I use it for making back up of my own YouTube videos.

By default, you [use youtube-dl for downloading videos](https://itsfoss.com/download-youtube-linux/). How about extracting only the audio with youtube-dl? That’s very simple actually. Let me show you the steps.

## Install youtube-dl first

Please make sure that you have installed youtube-dl on your Linux distribution first.

Since 2021, the usual snap and default package manager installations of youtube-dl give a broken utility. So, you need to install the Nightly version of youtube-dl to make it work.

The Nightly release contains recent changes and fixes so that you can run it without any error. I will describe the easiest way to set it up. First go to the Nightly release GitHub page.

Here, you can get a python file, named `youtube-dl`

. Just download it. Once downloaded, copy that file to `/usr/local/bin/`

. For this, open a terminal where you downloaded the file and then run:

```
sudo cp youtube-dl /usr/local/bin/
```


Now, give the file execution permission using the `chmod`

command.

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


You cannot just run `youtube-dl`

now, because of issue with `python`

naming. It uses `python`

while in Ubuntu-based systems, it is `python3`

.

![Python: No such file or directory error, when try to rrun youtube-dl initially](https://itsfoss.com/content/images/2024/05/youtube-dl-python-env-python-command-error.png)

So, [you can create an alias](https://linuxhandbook.com/linux-alias-command/) for youtube-dl in your `.bashrc`

.

```
alias youtube-dl='python3 /usr/local/bin/youtube-dl'
```


That’s it. You have set up youtube-dl in your system.


**Remember**In order to download audio using youtube-dl, you should have

[FFMPEG installed on your system](https://itsfoss.com/ffmpeg/).

## Download only audio with youtube-dl

If you only want to download audio from a YouTube video, you can use the `-x`

option with youtube-dl. This extract-audio option converts the video files to audio-only files.

```
youtube-dl -x video_URL
```


The file is saved in the same directory from where you ran the youtube-dl command.

Here’s an example where I downloaded the voice-over of our [Install Google Chrome on Ubuntu 24.04](https://www.youtube.com/watch?v=Cfo5BR2FGEQ) video.

Did you notice the audio format? It is in .m4a format. You may specify the audio format to something of your choice.

Say you want to extract the audio in MP3 format. You can use it like this:

```
youtube-dl -x --audio-format mp3 video_URL
```


Here’s the same example I showed previously. You can see that it [uses ffmpeg to convert](https://itsfoss.com/ffmpeg/) the m4a file into MP3.

`-k`

option to preserve the old file along with the new one.## Download entire YouTube playlist in MP3 format

Yes, you can totally do that. The main thing is to get the URL of the playlist here. It is typically in the following format:

```
https://www.youtube.com/playlist?list=XXXXXXXXXXXXXXXXXXX
```


To get the URL of a playlist, click on its name when the playlist is being displayed in the right sidebar.

![Click on the title of the playlist, appears on the right side, while you play a video.](https://itsfoss.com/content/images/2024/05/click-on-the-heading-of-the-playlist.png)

It will take you to the playlist page, and you can copy the URL here.

![Copy the URL of the playlist from the address bar of the browser](https://itsfoss.com/content/images/2024/05/select-and-copy-the-playlist-url.png)

Now that you have the playlist URL, you can use it to download the audio files in MP3 format in the following fashion:

```
youtube-dl --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" playlist_URL
```


That scary looking `-o "%(title)s.%(ext)s"`

specifies the output file (with option -o) and instructs it to use the title of the video and the extension (MP3 in this case) for naming the audio files.

![Download audio from all the videos on a playlist](https://itsfoss.com/content/images/2024/05/downloading-youtube-playlist-audio.webp)

I hope you find this quick tip helpful. Enjoy the audio files 😄