虎(牛)龍未酉2.1

記録帳|+n年後のジブンが思い出せますように……

【記録】YouTubeのダウンロード

YouTubeの動画をダウンロードしたいとの依頼

 

ウェブツール、macOSアプリ、Safai Extensiton……いろいろ調べた。

で、結論的には、「youtube-dl」。

これめっちゃいいわ。ドンピシャ。

 

Linuxで、コマンドだけでyoutubeから動画をダウンロードして任意の時間を切り出してgifアニ化する - Qiita

 

上記リンクの、前半だけが必要。

 

homebrewでインストール。homebrewは事前にインストールが必要。

$ brew install youtube-dl

 

次に、ダウンロードできる動画の種類を調べる。

$ youtube-dl -F https://www.youtube.com/watch?v=hogehoge

 

format code  extension  resolution note

249          webm       audio only tiny   69k , opus @ 50k (48000Hz), 38.47MiB

250          webm       audio only tiny   87k , opus @ 70k (48000Hz), 50.26MiB

140          m4a        audio only tiny  136k , m4a_dash container, mp4a.40.2@128k (44100Hz), 96.31MiB

251          webm       audio only tiny  170k , opus @160k (48000Hz), 98.50MiB

278          webm       256x144    144p  116k , webm container, vp9, 24fps, video only, 65.20MiB

160          mp4        256x144    144p  135k , avc1.4d400c, 24fps, video only, 64.27MiB

242          webm       426x240    240p  271k , vp9, 24fps, video only, 122.38MiB

133          mp4        426x240    240p  298k , avc1.4d4015, 24fps, video only, 147.03MiB

243          webm       640x360    360p  510k , vp9, 24fps, video only, 221.54MiB

134          mp4        640x360    360p  679k , avc1.4d401e, 24fps, video only, 310.76MiB

244          webm       854x480    480p 1027k , vp9, 24fps, video only, 392.59MiB

135          mp4        854x480    480p 1297k , avc1.4d401e, 24fps, video only, 634.09MiB

136          mp4        1280x720   720p 2580k , avc1.4d401f, 24fps, video only, 1.26GiB

247          webm       1280x720   720p 2999k , vp9, 24fps, video only, 760.29MiB

248          webm       1920x1080  1080p 5541k , vp9, 24fps, video only, 1.26GiB

137          mp4        1920x1080  1080p 5598k , avc1.640028, 24fps, video only, 2.49GiB

18           mp4        640x360    360p  553k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 411.38MiB (best)

 

まだよくわからんけど、audio only, video onlyが大半。(best)を選んでおけば良さそう。

 

-f オプションに、上記の番号をつけて、ダウンロード。

$ youtube-dl -f 18 https://www.youtube.com/watch?v=hogehoge

 

さすがlinuxワールド。

ドンピシャで必要なツールがある。

ハッピー。

 

後日談

字幕付きでダウンロードしたいとの要望があった

 

youtube-dlで字幕付きの動画をダウンロードする - mactkg-pub

 

に書いてあるとおりなんだけど。

youtube-dl -v -f mp4 --embed-subs --sub-lang ja --write-sub "playlistId/videoId"

 を書き換えて、

youtube-dl -v -f 18 --embed-subs --sub-lang en --write-sub https://www.youtube.com/watch?v=xxxxxxxxxxxx

これですごくうまく行ったのでハッピー。

 

オプションの意味は下記

youtube-dl オプション一覧及びそのメモ - †MASAYOSHI†のオンラインメモ帳

 

youtube-dl --list-subs https://www.youtube.com/watch?v=xxxxxxxxxxxx

で対応字幕一覧を取れる。

 

ただし、

ERROR: ffmpeg or avconv not found. Please install one.

と言われたので、

$ brew install ffmpeg

なんかいっぱいインストールしてはったのと、注意書きがあったので貼っておく。

==> gettext

gettext is keg-only, which means it was not symlinked into /usr/local,

because macOS provides BSD gettext.

 

If you need to have gettext first in your PATH run:

  echo 'export PATH="/usr/local/opt/gettext/bin:$PATH"' >> ~/.bash_profile

 

For compilers to find gettext you may need to set:

  export LDFLAGS="-L/usr/local/opt/gettext/lib"

  export CPPFLAGS="-I/usr/local/opt/gettext/include"

 

==> unbound

To have launchd start unbound now and restart at startup:

  sudo brew services start unbound

==> glib

Bash completion has been installed to:

  /usr/local/etc/bash_completion.d

==> tesseract

This formula contains only the "eng", "osd", and "snum" language data files.

If you need any other supported languages, run `brew install tesseract-lang`.

 

さらに後日談

ERROR: unable to download video data: HTTP Error 403: Forbidden

 

と言われるやつがあった。

youtube-dl -f 18 -v --rm-cache-dir https://www.youtube.com/watch?v=xxxxxxx

で解決。理屈はよくわかっていない。-vはデバッグオプション。便利。