youtube-dl 簡介

Introduction

youtube-dl is a command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform specific. It should work on your Unix box, on Windows or on macOS. It is released to the public domain, which means you can modify it, redistribute it or use it however you like.

youtube-dl ── ytdl-org/youtube-dl

youtube-dl 是一個開源程式碼專案,,可以從 Youtube 或是許多網站上下載影片。由於是基於 Python 開發,使得 youtube-dl 並不限定於特定平台使用。在本篇文章中將簡單介紹幾個 youtube-dl 用法。

Installation

由於需要 Python 環境,因此在下載之前請使用者安裝 Python。

  • Windows 使用者可以從官方網站上下載最新版 Python。
  • Linux、MacOS 使用者由於電腦已經內建 Python,因此不須再額外安裝。

安裝 Python 後,接著安裝 pip,pip 是 Python 套件的安裝程式,我們將在接下來的步驟中透過 pip 安裝 youtube-dl。

  • Windows 使用者請先下載 https://bootstrap.pypa.io/get-pip.py (右鍵 > 另存新檔),接著開啟命令提示字元(cmd.exe)並鍵入:

    1
    $ python get-pip.py
  • Linux 使用者請先開啟終端機(Terminal)並鍵入:

    1
    $ sudo apt install python3-pip
  • macOS一般都內建Python,並包含 pip,通過在終端機鍵入 pip --version 檢查是否已安裝。若尚未安裝則開啟終端機並鍵入:

    1
    $ sudo easy_install pip

安裝完 pip 後,我們便可以透過下列指令來安裝 youtube-dl:

1
$ pip3 install youtube-dl

若是要在 youtube-dl 下載時轉換檔案格式、或是加入字幕、專輯圖案等 Post-processing 功能,則需額外下載並安裝 ffmpeg

1
$ pip3 install ffmpeg

Usage

下載影片

一般下載

這邊將以 Youtube 影片 羅志祥 Show Lo - 愛不單行 You Won't Be Alone (官方完整版MV) 為例,若要下載影片則鍵入:

1
$ youtube-dl 'https://www.youtube.com/watch?v=iBrOFd1wDuQ'

youtube-dl 預設將以最高畫質做下載,預設副檔名為 mp4。

指定檔案格式

若要指定下載的檔案格式 (以 mkv 為例)可以鍵入:

1
$ youtube-dl --format=mkv 'https://www.youtube.com/watch?v=iBrOFd1wDuQ'

指定影片品質

這將告訴 youtube-dl下載最高解析度且高度不大於 480 的 mp4 影片:

1
$ youtube-dl -f 'bestvideo[height<=480][ext=mp4]' 'https://www.youtube.com/watch?v=iBrOFd1wDuQ'

嵌入字幕

  • --list-subs 列出可下載字幕語言
    • –write-sub 下載字幕
    • –embed-sub 將字幕嵌入影片中
    • –all-subs 下載所有語言字幕
    • –sub-lang= 指定字幕語言

這邊將以 Youtube 影片 林宥嘉 Yoga Lin [ 我已經敢想你 Courage to Remember You ] Official Music Video為例,我們可以通過下面指令來確認影片中的的字幕:

1
$ youtube-dl --list-subs 'https://www.youtube.com/watch?v=sDhdlHrRLtk'

我們發現到 zh-Hant 是可選的,於是我們便可以透過下列指令下載影片並嵌入字幕:

1
$ youtube-dl --write-sub --embed-subs --sub-lang=zh-Hant 'https://www.youtube.com/watch?v=sDhdlHrRLtk'

下載音樂

一般下載

這邊將以 Youtube 影片 A-Lin《有一種悲傷 A Kind of Sorrow》Official Music Video 為例,若要下載影片則鍵入:

1
$ youtube-dl --extract-audio 'https://www.youtube.com/watch?v=BRcudpJzy1I'

youtube-dl 預設將以最高音質做下載,預設副檔名為 webp。

指定檔案格式

若要指定下載的檔案格式 (以 mp3 為例)可以鍵入:

1
$ youtube-dl --extract-audio --audio-format=mp3 'https://www.youtube.com/watch?v=BRcudpJzy1I'

縮圖和資料

  • --embed-thumbnail 加入專輯封面(若是下載 Youtube 影片則即為影片上的縮圖)
  • --add-metadata 加入影片資訊
1
$ youtube-dl --extract-audio --audio-format=mp3 --embed-thumbnail --add-metadata 'https://www.youtube.com/watch?v=BRcudpJzy1I'

下載播放清單

下載整份播放清單

這邊將以 Youtube 影片清單 English Playlist 為例,若要下載影片則鍵入:

1
$ youtube-dl 'https://www.youtube.com/playlist?list=PL6N1fBKG1TQ8GZIMZ7MLA2-TC261O8iE5'

下載部份播放清單

  • --playlist-start=<NUMBER> 指定下載播放清單之起點
  • --playlist-end=<NUMBER> 指定下載播放清單之終點
1
$ youtube-dl --playlist-start=4 --playlist-end=4 'https://www.youtube.com/playlist?list=PL6N1fBKG1TQ8GZIMZ7MLA2-TC261O8iE5'

輸出檔名

  • -o, --output TEMPLATE

我們可以在命令列中加入 -o, --output 來設定輸出檔名,預設為 %(title)s-%(id)s.%(ext)s

這邊將以 Youtube 影片 HEBE TIEN 田馥甄 [你就不要想起我 You Better Not Think About Me] Official MV HD 為例,我們將下載的檔名改為 田馥甄 - 你就不要想起我

1
$ youtube-dl --output='田馥甄 - 你就不要想起我.%(ext)s' 'https://www.youtube.com/watch?v=GsKbnsUN2RE'

我們也可以透過官方所提供的模板參數來設定,例如我們透過模板參數來達成剛剛的檔名 田馥甄 - 你就不要想起我

1
$ youtube-dl --output='%(artist)s - %(alt_title)s.%(ext)s' 'https://www.youtube.com/watch?v=GsKbnsUN2RE'

登入下載

有些影片因為要登入後才能觀看,youtube-dl 提供對帳戶登入的支援:

  • -u, –username USERNAME
  • -p, –password PASSWORD

Example:

1
$ youtube-dl -u '[email protected]' -p 'password' 'https://www.youtube.com/?v=<video_id>'

References