yt-dlp

yt-dlp开源项目youtube-dl 的fork项目,提供了新功能和patch(合并了很多修复),可以解决一些下载问题。

安装

  • pip 安装:

    python3 -m pip install -U yt-dlp
    
  • Homebrew 安装:

    # 安装
    brew install yt-dlp
    # 更新
    brew upgrade yt-dlp
    

备注

配置方法和 youtube-dl 基本相同,例如配置文件是 ~/.config/yt-dlp/config

举例

我非常喜欢的 《杀死那个石家庄人》--万能青年旅店 影视混剪MV

  • 首先获取视频列表:

    yt-dlp -F "https://www.youtube.com/watch?v=npHbCnf-Lpk"
    

显示输出:

yt-dlp -F 列出下载视频的输出案例 《杀死那个石家庄人》--万能青年旅店 影视混剪MV
[youtube:tab] Extracting URL: https://www.youtube.com/watch?v=npHbCnf-Lpk&list=PLnqzKl0S_xnl8xgGJxWKDPuFrgafqSEfo&index=3
[youtube:tab] Downloading playlist PLnqzKl0S_xnl8xgGJxWKDPuFrgafqSEfo - add --no-playlist to download just the video npHbCnf-Lpk
[youtube:tab] PLnqzKl0S_xnl8xgGJxWKDPuFrgafqSEfo: Downloading webpage
WARNING: [youtube:tab] Unable to recognize playlist. Downloading just video npHbCnf-Lpk
[youtube] Extracting URL: https://www.youtube.com/watch?v=npHbCnf-Lpk
[youtube] npHbCnf-Lpk: Downloading webpage
[youtube] npHbCnf-Lpk: Downloading android player API JSON
[info] Available formats for npHbCnf-Lpk:
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        0    │                  mhtml │ images                                storyboard
sb0 mhtml 160x90       0    │                  mhtml │ images                                storyboard
599 m4a   audio only      2 │    1.50MiB   31k dash  │ audio only        mp4a.40.5   31k 22k ultralow, m4a_dash
600 webm  audio only      2 │    1.74MiB   36k dash  │ audio only        opus        36k 48k ultralow, webm_dash
139 m4a   audio only      2 │    2.37MiB   49k dash  │ audio only        mp4a.40.5   49k 22k low, m4a_dash
249 webm  audio only      2 │    2.57MiB   53k dash  │ audio only        opus        53k 48k low, webm_dash
250 webm  audio only      2 │    3.37MiB   69k dash  │ audio only        opus        69k 48k low, webm_dash
140 m4a   audio only      2 │    6.29MiB  129k dash  │ audio only        mp4a.40.2  129k 44k medium, m4a_dash
251 webm  audio only      2 │    6.56MiB  135k dash  │ audio only        opus       135k 48k medium, webm_dash
17  3gp   176x144      7  1 │    3.77MiB   78k https │ mp4v.20.3     78k mp4a.40.2    0k 22k 144p
597 mp4   256x144     15    │    1.63MiB   34k dash  │ avc1.4d400b   34k video only          144p, mp4_dash
598 webm  256x144     15    │    1.38MiB   28k dash  │ vp9           28k video only          144p, webm_dash
160 mp4   256x144     30    │    3.60MiB   74k dash  │ avc1.4d400c   74k video only          144p, mp4_dash
278 webm  256x144     30    │    3.63MiB   75k dash  │ vp9           75k video only          144p, webm_dash
133 mp4   426x240     30    │    8.22MiB  169k dash  │ avc1.4d4015  169k video only          240p, mp4_dash
242 webm  426x240     30    │    5.89MiB  121k dash  │ vp9          121k video only          240p, webm_dash
134 mp4   640x360     30    │   15.75MiB  324k dash  │ avc1.4d401e  324k video only          360p, mp4_dash
18  mp4   640x360     30  2 │ ~ 22.52MiB  452k https │ avc1.42001E  452k mp4a.40.2    0k 44k 360p
243 webm  640x360     30    │    9.95MiB  205k dash  │ vp9          205k video only          360p, webm_dash
135 mp4   854x480     30    │   29.06MiB  598k dash  │ avc1.4d401f  598k video only          480p, mp4_dash
244 webm  854x480     30    │   16.39MiB  337k dash  │ vp9          337k video only          480p, webm_dash
22  mp4   1280x720    30  2 │ ~ 63.28MiB 1270k https │ avc1.64001F 1270k mp4a.40.2    0k 44k 720p
136 mp4   1280x720    30    │   55.51MiB 1142k dash  │ avc1.64001f 1142k video only          720p, mp4_dash
247 webm  1280x720    30    │   29.94MiB  616k dash  │ vp9          616k video only          720p, webm_dash
137 mp4   1920x1080   30    │  102.77MiB 2115k dash  │ avc1.640028 2115k video only          1080p, mp4_dash
248 webm  1920x1080   30    │   48.61MiB 1000k dash  │ vp9         1000k video only          1080p, webm_dash

我来下载最小的一个视频mp4,编号 18

yt-dlp -f 18 "https://www.youtube.com/watch?v=npHbCnf-Lpk"

备注

请注意,上述列表中,很多视频文件是没有声音的( video only ),有些则只有声音( audio only )。如果你需要一个完整的有声音的视频,务必 不要 选择 video only

显示输出:

[youtube:tab] Extracting URL: https://www.youtube.com/watch?v=npHbCnf-Lpk
[youtube:tab] Downloading playlist PLnqzKl0S_xnl8xgGJxWKDPuFrgafqSEfo - add --no-playlist to download just the video npHbCnf-Lpk
[youtube:tab] PLnqzKl0S_xnl8xgGJxWKDPuFrgafqSEfo: Downloading webpage
WARNING: [youtube:tab] Unable to recognize playlist. Downloading just video npHbCnf-Lpk
[youtube] Extracting URL: https://www.youtube.com/watch?v=npHbCnf-Lpk
[youtube] npHbCnf-Lpk: Downloading webpage
[youtube] npHbCnf-Lpk: Downloading android player API JSON
[info] npHbCnf-Lpk: Downloading 1 format(s): 18
[dashsegments] Total fragments: 1
[download] Destination: /Users/huataihuang/Movies/《杀死那个石家庄人》--万能青年旅店   影视混剪MV.mp4
[download] 100% of    22MiB in 00:00:04 at 399.36KiB/s

非常赞!!!

结合 Sphinx文档嵌入视频 就可以在我的个人网站上嵌入一段演示视频: 墙裂推荐观看YouTube原高清视频 => 《杀死那个石家庄人》--万能青年旅店 影视混剪MV

快速下载喜爱的音频文件

经常听的的歌曲,建一个 playlist 列表,然后一次性从Youtube下载音频:

下载自己喜欢的歌曲
#!/usr/bin/env bash
#
:<<'EOF'
获得音质最好的音频
yt-dlp -F 过滤列表案例
599 m4a   audio only      2 |   1.45MiB   31k dash  | audio only        mp4a.40.5   31k 22k ultralow, m4a_dash
139 m4a   audio only      2 |   2.30MiB   49k dash  | audio only        mp4a.40.5   49k 22k low, m4a_dash
140 m4a   audio only      2 |   6.11MiB  129k dash  | audio only        mp4a.40.2  129k 44k medium, m4a_dash
EOF

for i in `cat playlist`;do
    id=`yt-dlp -F "https://www.youtube.com/watch?v=$i" | grep "m4a   audio only" | awk '{print $1" "$8}' | awk -Fk '{print $1}' | sort -r -n -k2 | head -1 | awk '{print $1}'`
    echo $id
    yt-dlp -f $id "https://www.youtube.com/watch?v=$i"
done

参考