如何下載 YouTube 私人影片 (使用 youtube-dl 或 ytl-dp)

文章目錄

需求

YouTube 預設是不能讓人下載影片的, 即使是你自己上傳的影片也是一樣. 然後我的影片大多都設成私人, 就更沒辦法用一堆線上工具下載了. 研究了一下兩年前曾被 GitHub 下架的 youtube-dl, 看來他支援下載私人影片 (不需要帳號密碼, 用 cookie 即可).

工具

需要 1.下載工具 2. 轉檔工具(合併檔案用) 3. 抓 cookie 的工具, 如下:

  1. youtube-dl 或是 ytl-dp

  2. FFmpeg, 轉檔用

  3. cookie 工具, 我是用 Get cookies.txt 這個 extension, 各 browser 的下載位置如下:

    (這個 extension 下架的相關討論可參考 https://www.reddit.com/r/youtubedl/comments/vuatbt)

方法

  1. 準備 cookie

    既然這是私人影片, 就會卡權限, 因此要準備 cookie. (注意: 這個套件不會去抓無痕模式的 cookie). 安裝好後, 連上 youtube.com, 然後 export 檔案出來就好. 預設他取的名字會是 youtube.com_cookies.txt, 這邊就不修改檔案名稱.

  2. 下載檔案

    1. 使用 youtube-dl 指令如下, 執行後就可以下載檔案了, 要注意的是, 如果沒有 FFmpeg, youtube-dl 就不會合併檔案, 最後就會有兩個檔案, 一個影片(mp4), 一個聲音(m4a), 得要再另外 merge, 很麻煩, 如下圖.

       1C:\youtube>youtube-dl.exe --format bestvideo+bestaudio --limit-rate 2M cookies=youtube.com_cookies.txt https://www.youtube.com/watch?v=********/
       2[youtube] 7zTujXuka2c: Downloading webpage
       3[youtube] 7zTujXuka2c: Downloading m3u8 information
       4[youtube] 7zTujXuka2c: Downloading MPD manifest
       5WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
       6[dashsegments] Total fragments: 1047
       7[download] Destination *****.f136.mp4
       8[download] 100% of 208.99MiB in 02:59
       9[dashsegments] Total fragments: 1047
      10[download] Destination *****.f140.mp4
      11[download] 100% of 81.84MiB in 01:48
      
    2. 因此, 直接在下載參數指定 FFmpeg 的目錄位置, 這樣在下載完後, 他就會去呼叫 FFmpeg 執行合併檔案的動作. 他會把 mp4/m4a 合併成一個 mp4 檔案, 然後把原來的檔案刪除.

       1C:\youtube>youtube-dl.exe --ffmpeg-location C:\bin\ffmpeg\bin --format bestvideo+bestaudio --limit-rate 2M --cookies=youtube.com_cookies.txt https://www.youtube.com/watch?v=********/
       2[youtube] 7zTujXuka2c: Downloading webpage
       3[youtube] 7zTujXuka2c: Downloading m3u8 information
       4[youtube] 7zTujXuka2c: Downloading MPD manifest
       5WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
       6[dashsegments] Total fragments: 1047
       7[download] Destination *****.f136.mp4
       8[download] 100% of 208.99MiB in 05:17
       9[dashsegments] Total fragments: 1047
      10[download] Destination *****.f140.mp4
      11[download] 100% of 81.84MiB in 02:16
      12[ffmpeg] Merging formats into "******.mp4"
      13Delete original file *****.f136.mp4 (pass -k to keep)
      14Delete original file *****.f140.mp4 (pass -k to keep)
      
    3. 另外也可以使用 YT-DLP 下載, YT-DLP 是 youtube-dl 的一個 fork, 他自己的官方說明是 "A youtube-dl fork with additional features and fixes", 兩者的指令也都差不多, 試用了一下也沒甚麼問題, 只是下載完是 .mkv

       1C:\youtube>yl-dlp.exe --ffmpeg-location C:\ffmpeg\bin --format bestvideo+bestaudio --limit-rate 2M --cookies=youtube.com_cookies.txt https://www.youtube.com/watch?v=********/
       2[youtube] 7zTujXuka2c: Downloading webpage
       3[youtube] 7zTujXuka2c: Downloading android player API JSON
       4[youtube] 7zTujXuka2c: Downloading m3u8 information
       5[youtube] 7zTujXuka2c: Downloading MPD manifest
       6[youtube] 7zTujXuka2c: Downloading m3u8 information
       7[youtube] 7zTujXuka2c: Downloading MPD manifest
       8[info] 7zTujXuka2c: Downloading 1 format(s): 247+140
       9WARNING: Requested formats are incompatible for merge and will be merged into mkv
      10[dashsegments] Total fragments: 1047
      11[download] Destination *****.ff247.webm
      12[download] 100% of 208.99MiB in 05:17
      13[dashsegments] Total fragments: 1047
      14[download] Destination *****.f140.mp4
      15[download] 100% of 81.84MiB in 02:16
      16[Merger] Merging formats into "******.mkv"
      17Delete original file *****.f247.webm (pass -k to keep)
      18Delete original file *****.f140.mp4 (pass -k to keep)
      

下載直播中的影片 (2022/7/15 更新)

  1. 嘗試下載直播中的影片, 失敗. 錯誤訊息是說影片格式不支援.

    1PS D:\youtube> .\yt-dlp.exe --format bestvideo+bestaudio --limit-rate 2M https://www.youtube.com/********
    2
    3[youtube] wYyS4yx07mc: Downloading webpage
    4[youtube] wYyS4yx07mc: Downloading android player API JSON
    5[youtube] wYyS4yx07mc: Downloading m3u8 information
    6[youtube] wYyS4yx07mc: Downloading m3u8 information
    7ERROR: [youtube] wYyS4yx07mc: Requested format is not available. Use --list-formats for a list of available formats
    
  2. 在直播過程中看一下影片格式

     1PS D:\youtube> .\yt-dlp.exe --list-formats https://www.youtube.com/watch?v=**********
     2[youtube] wYyS4yx07mc: Downloading webpage
     3[youtube] wYyS4yx07mc: Downloading android player API JSON
     4[youtube] wYyS4yx07mc: Downloading m3u8 information
     5[youtube] wYyS4yx07mc: Downloading m3u8 information
     6[info] Available formats for wYyS4yx07mc:
     7ID  EXT RESOLUTION FPS │   TBR PROTO │ VCODEC        VBR ACODEC    ABR
     8──────────────────────────────────────────────────────────────────────
     991  mp4 256x144     30 │  269k m3u8  │ avc1.4d400c  269k mp4a.40.5  0k
    1092  mp4 426x240     30 │  507k m3u8  │ avc1.4d4015  507k mp4a.40.5  0k
    1193  mp4 640x360     30 │  962k m3u8  │ avc1.4d401e  962k mp4a.40.2  0k
    1294  mp4 854x480     30 │ 1282k m3u8  │ avc1.4d401f 1282k mp4a.40.2  0k
    13300 mp4 1280x720    60 │ 2922k m3u8  │ avc1.4d4020 2922k mp4a.40.2  0k
    
  3. 直播結束後, 再看一次下影片格式 => 這是後來直播結束後再補的

     1PS D:\bin\youtube>  .\youtube-dl.exe --list-formats https://www.youtube.com/watch?v=wYyS4yx07mc
     2[youtube] wYyS4yx07mc: Downloading webpage
     3[youtube] wYyS4yx07mc: Downloading MPD manifest
     4[info] Available formats for wYyS4yx07mc:
     5format code  extension  resolution note
     6139          m4a        audio only DASH audio   59k , m4a_dash container, mp4a.40.5 (22050Hz), 58.95MiB
     7251          webm       audio only tiny  106k , webm_dash container, opus @106k (48000Hz), 128.35MiB
     8140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 156.44MiB
     9278          webm       256x144    DASH video   95k , webm_dash container, vp9, 30fps, video only
    10160          mp4        256x144    DASH video  108k , mp4_dash container, avc1.4d400b, 30fps, video only
    11242          webm       426x240    DASH video  220k , webm_dash container, vp9, 30fps, video only
    12133          mp4        426x240    DASH video  242k , mp4_dash container, avc1.4d400c, 30fps, video only
    13134          mp4        640x360    360p  284k , mp4_dash container, avc1.4d401e@ 284k, 30fps, video only, 343.35MiB
    14243          webm       640x360    DASH video  405k , webm_dash container, vp9, 30fps, video only
    15244          webm       854x480    DASH video  752k , webm_dash container, vp9, 30fps, video only
    16135          mp4        854x480    DASH video 1155k , mp4_dash container, avc1.4d400c, 30fps, video only
    17136          mp4        1280x720   720p 1110k , mp4_dash container, avc1.64001f@1110k, 30fps, video only, 1.31GiB
    18298          mp4        1280x720   720p60 1476k , mp4_dash container, avc1.640020@1476k, 60fps, video only, 1.74GiB
    19247          webm       1280x720   DASH video 1505k , webm_dash container, vp9, 30fps, video only
    20302          webm       1280x720   DASH video 2646k , webm_dash container, vp9, 60fps, video only
    2118           mp4        640x360    360p  380k , avc1.42001E, 30fps, mp4a.40.2 (44100Hz), 459.69MiB
    2222           mp4        1280x720   720p 1238k , avc1.64001F, 30fps, mp4a.40.2 (44100Hz) (best)
    
  4. 隨便找一個影片看一下格式 (非直播中的影片)

     1PS D:\youtube> .\yt-dlp.exe --list-formats https://www.youtube.com/watch?v=******
     2[youtube] Vbz4zPwsavc: Downloading webpage
     3[youtube] Vbz4zPwsavc: Downloading android player API JSON
     4[youtube] Vbz4zPwsavc: Downloading MPD manifest
     5[youtube] Vbz4zPwsavc: Downloading MPD manifest
     6[info] Available formats for Vbz4zPwsavc:
     7ID       EXT   RESOLUTION FPS    FILESIZE   TBR PROTO  VCODEC        VBR ACODEC      ABR ASR MORE INFO
     8────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
     9sb2      mhtml 48x27                            mhtml  images                                storyboard
    10sb1      mhtml 80x45                            mhtml  images                                storyboard
    11sb0      mhtml 160x90                           mhtml  images                                storyboard
    12139-dash m4a   audio only       734.38KiB   50k dash   audio only        mp4a.40.5   50k 22k DASH audio, m4a_dash
    13139      m4a   audio only       734.38KiB   47k https  audio only        mp4a.40.5   47k 22k low, m4a_dash
    14140-dash m4a   audio only         1.91MiB  129k dash   audio only        mp4a.40.2  129k 44k DASH audio, m4a_dash
    15140      m4a   audio only         1.91MiB  127k https  audio only        mp4a.40.2  127k 44k medium, m4a_dash
    16251-dash webm  audio only         1.98MiB  142k dash   audio only        opus       142k 48k DASH audio, webm_dash
    17251      webm  audio only         1.98MiB  132k https  audio only        opus       132k 48k medium, webm_dash
    1817       3gp   176x144      8     1.04MiB   69k https  mp4v.20.3     69k mp4a.40.2    0k 22k 144p
    19160      mp4   256x144     30  ~  1.66MiB  108k dash   avc1.4d400b  108k video only          DASH video, mp4_dash
    20278      webm  256x144     30  ~  1.46MiB   95k dash   vp9           95k video only          DASH video, webm_dash
    21133      mp4   426x240     30  ~  3.72MiB  242k dash   avc1.4d400c  242k video only          DASH video, mp4_dash
    22242      webm  426x240     30  ~  3.39MiB  220k dash   vp9          220k video only          DASH video, webm_dash
    23134-dash mp4   640x360     30     5.31MiB  635k dash   avc1.4d401e  635k video only          DASH video, mp4_dash
    24134      mp4   640x360     30     5.31MiB  353k https  avc1.4d401e  353k video only          360p, mp4_dash
    2518       mp4   640x360     30  ~  7.38MiB  479k https  avc1.42001E  479k mp4a.40.2    0k 44k 360p
    26243      webm  640x360     30  ~  6.24MiB  405k dash   vp9          405k video only          DASH video, webm_dash
    27135      mp4   854x480     30  ~ 17.76MiB 1155k dash   avc1.4d4014 1155k video only          DASH video, mp4_dash
    28244      webm  854x480     30  ~ 11.58MiB  752k dash   vp9          752k video only          DASH video, webm_dash
    2922       mp4   1280x720    30  ~ 27.88MiB 1812k https  avc1.64001F 1812k mp4a.40.2    0k 44k 720p
    30136      mp4   1280x720    30  ~ 35.53MiB 2310k dash   avc1.4d4016 2310k video only          DASH video, mp4_dash
    31247      webm  1280x720    30  ~ 23.15MiB 1505k dash   vp9         1505k video only          DASH video, webm_dash
    32298      mp4   1280x720    60  ~ 53.29MiB 3465k dash   avc1.4d4016 3465k video only          DASH video, mp4_dash
    33302      webm  1280x720    60  ~ 40.70MiB 2646k dash   vp9         2646k video only          DASH video, webm_dash
    34299-dash mp4   1920x1080   60    47.01MiB 5817k dash   avc1.64002a 5817k video only          DASH video, mp4_dash
    35299      mp4   1920x1080   60    47.01MiB 3130k https  avc1.64002a 3130k video only          1080p60, mp4_dash
    36303      webm  1920x1080   60  ~ 67.83MiB 4410k dash   vp9         4410k video only          DASH video, webm_dash
    
  5. 拿掉 --format bestvideo+bestaudio 硬是要下載, 還是失敗

     1PS D:\youtube> .\yt-dlp.exe --limit-rate 2M --ffmpeg-location d:\bin\ffmpeg\bin https://www.youtube.com/watch?v=**********
     2[youtube] wYyS4yx07mc: Downloading webpage
     3[youtube] wYyS4yx07mc: Downloading android player API JSON
     4[youtube] wYyS4yx07mc: Downloading m3u8 information
     5[youtube] wYyS4yx07mc: Downloading m3u8 information
     6[info] wYyS4yx07mc: Downloading 1 format(s): 300
     7[download] Destination: American Eagle Kindergarten Graduation 2022 2022-07-15 20_29 [wYyS4yx07mc].mp4
     8[hls @ 00000268ad39c7c0] Skip ('#EXT-X-VERSION:3')
     9[hls @ 00000268ad39c7c0] Skip ('#EXT-X-PROGRAM-DATE-TIME:2022-07-15T12:29:26.014+00:00')
    10......
    

結論: 乖乖等直播完再下載