曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
杂七杂八

youtube-dl is too slow, I chose yt-dlp

Preface It's Chinese New Year recently, so as usual before the New Year I download some New Year songs. Nowadays hardly anyone in China makes New Year albums; for that we have to look to Malaysian Chinese, who release albums every year, and the quality is quite good! Naturally they're not on domestic platforms (or incomplete), so we need to download from YouTube~ Previously I used a Chrome extension to download then used a script to merge the videos, which was a bit tedious. This year I'll try the black magic of automatic downloading~ The two tools being compared can both be installed with pip pip install youtube-dl pip install

曦远 (DealiAxy)约 14 分钟
目录4 个章节

Preface

It's Chinese New Year recently, so as usual before the New Year I download some New Year songs. Nowadays hardly anyone in China makes New Year albums; for that we have to look to Malaysian Chinese, who release albums every year, and the quality is quite good!

Naturally they're not on domestic platforms (or incomplete), so we need to download from YouTube~

Previously I used a Chrome extension to download then used a script to merge the videos, which was a bit tedious. This year I'll try the black magic of automatic downloading~

The two tools being compared can both be installed with pip

pip install youtube-dl
pip install yt-dlp

youtube-dl

PS: youtube-dl was previously banned by GitHub; later, presumably under pressure from the open-source community, the repository was restored

First, here's the project URL: https://github.com/ytdl-org/youtube-dl

How to use it specifically is documented in great detail

The parameters are quite complex

The main problem is that the download speed is too slow (50–60k per second) (probably identified by sites like YouTube and rate-limited)

Downloading multiple videos from a playlist

 youtube-dl --proxy http://127.0.0.1:10809 -f "bestvideo[ext=mp4,height<=1080]+bestaudio[ext=m4a]/bestvideo[ext=mp4,height<=720]+bestaudio[ext=m4a]/bestvideo[ext=mp4,height<=480]+bestaudio[ext=m4a]/best[ext=mp4,height<=480]/best" -o "%(title)s.%(ext)s" --embed-thumbnail --playlist-start 1 --playlist-end 9 "https://www.youtube.com/watch?v=xxx&list=xxx"

Parameter explanation

Combining the following two means downloading videos 1–9 from the playlist

--playlist-start 1
--playlist-end 9

The long string after -f means starting the download from 1080p, and if that's not available, gradually falling back to 480p

I won't go into setting up a proxy and such

yt-dlp

The experience is excellent, highly recommended!

This is a newer tool that came out later. The parameters are much simpler, and it supports using an external downloader (e.g. Aria2)

Let's first look at what the basic commands look like~

Download a single video

yt-dlp -f 137+140 --proxy http://127.0.0.1:10809 --merge-output-format mp4 https://www.youtube.com/watch?v=xxx

Download multiple videos from a playlist

yt-dlp -I 1:9 --proxy http://127.0.0.1:10809 --merge-output-format mp4 "https://www.youtube.com/watch?v=xxx&list=xxx"

Format selection uses a parameter like this

-f 137+140

At first glance it might be confusing, but these are actually YouTube quality codes

On YouTube, for videos at 1080p and above, the video and audio are separate. The video is generally in a format like mp4 or webm, and the audio is m4a, so to download HD video you need to download the video and audio separately and then merge them yourself.

PS: Previously I did indeed download them separately and then wrote a script to batch-merge them with ffmpeg, but with this tool you don't need to go through all that—it can automatically call ffmpeg to merge the video (though you need to make sure ffmpeg is already installed on your system).

The 137 and 140 above are the codes for video and audio.

So how do you know what the various video and audio quality codes are? Just use the -F parameter.

yt-dlp -F --proxy http://127.0.0.1:10809 "https://www.youtube.com/watch?v=xxx"

With this parameter, you can display all the quality codes for the specified video, then pick and combine them as needed

[youtube] Extracting URL: https://www.youtube.com/watch?v=kjuB12RFtDE
[youtube] kjuB12RFtDE: Downloading webpage
[youtube] kjuB12RFtDE: Downloading android player API JSON
[info] Available formats for kjuB12RFtDE:
ID  EXT   RESOLUTION FPS CH │   FILESIZE   TBR PROTO │ VCODEC          VBR ACODEC      ABR ASR MORE INFO
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sb2 mhtml 48x27        0    │                  mhtml │ images                                  storyboard
sb1 mhtml 80x45        1    │                  mhtml │ images                                  storyboard
sb0 mhtml 160x90       1    │                  mhtml │ images                                  storyboard
140 m4a   audio only      2 │    4.42MiB  129k https │ audio only          mp4a.40.2  129k 44k medium, m4a_dash
251 webm  audio only      2 │    4.66MiB  137k https │ audio only          opus       137k 48k medium, webm_dash
22  mp4   1280x720    25  2 │ ~ 44.82MiB 1284k https │ avc1.64001F   1284k mp4a.40.2    0k 44k 720p
136 mp4   1280x720    25    │   39.41MiB 1155k https │ avc1.4d401f   1155k video only          720p, mp4_dash
247 webm  1280x720    25    │   39.53MiB 1158k https │ vp9           1158k video only          720p, webm_dash
137 mp4   1920x1080   25    │  117.11MiB 3431k https │ avc1.640028   3431k video only          1080p, mp4_dash
248 webm  1920x1080   25    │   75.15MiB 2202k https │ vp9           2202k video only          1080p, webm_dash

From this result you can see that 137 above is 1080p mp4 video; 140 is 129k audio.

As for an external downloader, I didn't bother. The default download speed is already fast, several MB per second, which is enough so I didn't mess with it.

References

_EOF_

youtube-dl is too slow, I chose yt-dlp

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…