~~CLOSETOC~~
====== youtube-dl - YouTube や他の動画サイトから動画をダウンロード ======
{{python:python_youtube-dl_mp4.png?470|Python youtube-dl format mp4}}\\
youtube-dl は、YouTube などの動画共有サービスから動画のダウンロードや音声の抽出を行うコマンドラインプログラムである。\\
Python で書かれているのでクロスプラットフォームである。Unlicense の下で配布されている。\\
[[wpjp>Youtube-dl|youtube-dl - Wikipedia]] より\\
[[:python:yt-dlp|yt-dlp]] を推奨。\\
===== pip によるインストール =====
**youtube-dl** CUI\\
$ pip install youtube-dl
**youtube-dl-gui** GUI\\
$ pip install youtube-dl-gui
===== ダウンロードによるインストール =====
[[windows:youtube-dl-gui|Youtube-DLG - YouTube や他の動画サイトから動画をダウンロード]] (Windows)\\
$ git clone https://github.com/ytdl-org/youtube-dl.git
$ git clone https://github.com/MrS0m30n3/youtube-dl-gui.git
**youtube-dl-gui** のバイナリインストーラは以下からダウンロードできる。\\
[[https://mrs0m30n3.github.io/youtube-dl-gui/|youtube-dlg]]\\
===== ダウンロード形式の指定 =====
**mp4** コンテナに最高画質/最高音質で保存する。\\
$ youtube-dl --format "bestvideo+bestaudio[ext=m4a]/bestvideo+bestaudio/best" --merge-output-format mp4 <url>
\\
$ youtube-dl --format "bestvideo+bestaudio[ext=m4a]/bestvideo+bestaudio/best" --merge-output-format mp4 https://www.youtube.com/watch?v=PQhWrgVWHXU
[youtube] PQhWrgVWHXU: Downloading webpage
[download] Destination: 吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.f137.mp4
[download] 100% of 101.66MiB in 00:18
[download] Destination: 吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.f140.m4a
[download] 100% of 4.72MiB in 00:01
[ffmpeg] Merging formats into "吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.mp4"
Deleting original file 吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.f137.mp4 (pass -k to keep)
Deleting original file 吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.f140.m4a (pass -k to keep)
**m4a** コンテナに最高音質で音声のみ保存する。\\
youtube-dl が **AtomicParsley** を使用して m4a にサムネイル画像を埋め込むので、予め [[windows:scoop|scoop]] などでインストールしておく。\\
Windows の場合は [[windows:scoop|scoop]] コマンドで AtomicParsley をインストールしてから youtube-dl でダウンロードする。\\
$ scoop install atomicparsley
Installing 'atomicparsley' (0.9.0) [64bit]
AtomicParsley-win32-0.9.0.zip (438.9 KB) [================================================] 100%
Checking hash of AtomicParsley-win32-0.9.0.zip ... ok.
Extracting AtomicParsley-win32-0.9.0.zip ... done.
Linking ~\scoop\apps\atomicparsley\current => ~\scoop\apps\atomicparsley\0.9.0
Creating shim for 'AtomicParsley'.
'atomicparsley' (0.9.0) was installed successfully!
$ youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <url>
\\
$ youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata https://youtu.be/mzGo1Ar-f1w
[youtube] mzGo1Ar-f1w: Downloading webpage
[youtube] mzGo1Ar-f1w: Downloading thumbnail ...
[youtube] mzGo1Ar-f1w: Writing thumbnail to: ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.webp
[download] ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a has already been downloaded
[download] 100% of 3.90MiB
[ffmpeg] Correcting container in "ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a"
[ffmpeg] Adding metadata to 'ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a'
[ffmpeg] Converting thumbnail "ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.webp" to JPEG
[atomicparsley] Adding thumbnail to "ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a"
エクスプローラで m4a のサムネイルをプレビューした様子。\\
{{python:youtube-dl_m4a_atomicparsley_thumbnail_001.png?640|エクスプローラ m4a サムネイル 001}}\\
==== 手動で形式を指定する場合 ====
ダウンロード可能なフォーマットコードを調べる。\\
**youtube-dl -F **\\
$ youtube-dl -F https://www.youtube.com/watch?v=PQhWrgVWHXU
[youtube] PQhWrgVWHXU: Downloading webpage
[info] Available formats for PQhWrgVWHXU:
format code extension resolution note
249 webm audio only tiny 54k , opus @ 50k (48000Hz), 1.79MiB
250 webm audio only tiny 72k , opus @ 70k (48000Hz), 2.37MiB
140 m4a audio only tiny 130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 4.72MiB
251 webm audio only tiny 140k , opus @160k (48000Hz), 4.65MiB
394 mp4 256x144 144p 88k , av01.0.00M.08, 30fps, video only, 2.51MiB
278 webm 256x144 144p 97k , webm container, vp9, 30fps, video only, 3.20MiB
160 mp4 256x144 144p 112k , avc1.4d400c, 30fps, video only, 2.77MiB
395 mp4 426x240 240p 191k , av01.0.00M.08, 30fps, video only, 5.01MiB
242 webm 426x240 240p 223k , vp9, 30fps, video only, 5.82MiB
133 mp4 426x240 240p 293k , avc1.4d4015, 30fps, video only, 5.77MiB
396 mp4 640x360 360p 392k , av01.0.01M.08, 30fps, video only, 10.14MiB
243 webm 640x360 360p 409k , vp9, 30fps, video only, 10.61MiB
134 mp4 640x360 360p 658k , avc1.4d401e, 30fps, video only, 11.75MiB
397 mp4 854x480 480p 720k , av01.0.04M.08, 30fps, video only, 18.16MiB
244 webm 854x480 480p 758k , vp9, 30fps, video only, 17.61MiB
135 mp4 854x480 480p 858k , avc1.4d401f, 30fps, video only, 17.72MiB
398 mp4 1280x720 720p 1479k , av01.0.05M.08, 30fps, video only, 35.88MiB
247 webm 1280x720 720p 1511k , vp9, 30fps, video only, 33.09MiB
136 mp4 1280x720 720p 1836k , avc1.4d401f, 30fps, video only, 29.26MiB
248 webm 1920x1080 1080p 2673k , vp9, 30fps, video only, 77.89MiB
399 mp4 1920x1080 1080p 2689k , av01.0.08M.08, 30fps, video only, 63.94MiB
137 mp4 1920x1080 1080p 4568k , avc1.640028, 30fps, video only, 101.66MiB
18 mp4 640x360 360p 683k , avc1.42001E, 30fps, mp4a.40.2@ 96k (44100Hz), 24.91MiB (best)
フォーマットコードで形式を指定してダウンロードする。\\
**youtube-dl -f **\\
$ youtube-dl -f 137 https://www.youtube.com/watch?v=PQhWrgVWHXU
[youtube] PQhWrgVWHXU: Downloading webpage
[download] Destination: 吉幾三「百年桜」MV(2020年4月29日発売)-PQhWrgVWHXU.mp4
[download] 100% of 101.66MiB in 00:23
===== トラブルシューティング =====
==== ダウンロードが遅い... ようつべさんが激怒💢❓😅 ====
2021/6/17 頃から、サーバー側でスロットル制限が行なわれてランダムにダウンロード速度が低下している報告があがってましたが、2021/10/21 現在はすべてのダウンロード速度に規制がかかっている模様です🤔以下の文献などでも回避方法が議論されています😅💦\\
[[git>ytdl-org/youtube-dl/issues/29326|[YouTube] Randomly slow youtube download speed · Issue #29326 · ytdl-org/youtube-dl]]\\
[[git>fent/node-ytdl-core/issues/964|Randomly slow youtube download speed · Issue #964 · fent/node-ytdl-core]]\\
VLC の本家でも議論されていますが、何点か回避方法が示されています🤤\\
[[https://code.videolan.org/videolan/vlc/-/issues/26174#note_286229|YouTube Lua Video Lockup (#26174) · Issues · VideoLAN / VLC]]\\
==== ERROR: AtomicParsley was not found. Please install. が発生する場合 ====
$ youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata https://youtu.be/mzGo1Ar-f1w
[youtube] mzGo1Ar-f1w: Downloading webpage
[youtube] mzGo1Ar-f1w: Downloading thumbnail ...
[youtube] mzGo1Ar-f1w: Writing thumbnail to: ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.webp
[download] Destination: ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a
[download] 100% of 3.85MiB in 00:06
[ffmpeg] Correcting container in "ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a"
[ffmpeg] Adding metadata to 'ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.m4a'
[ffmpeg] Converting thumbnail "ぜんぶ君のせいだ。「無題合唱」アニメーションMusicVideo-mzGo1Ar-f1w.webp" to JPEG
ERROR: AtomicParsley was not found. Please install.
Windows の場合は [[windows:scoop|scoop]] コマンドで AtomicParsley をインストールしてから youtube-dl でダウンロードする。\\
$ scoop install atomicparsley
Installing 'atomicparsley' (0.9.0) [64bit]
AtomicParsley-win32-0.9.0.zip (438.9 KB) [================================================] 100%
Checking hash of AtomicParsley-win32-0.9.0.zip ... ok.
Extracting AtomicParsley-win32-0.9.0.zip ... done.
Linking ~\scoop\apps\atomicparsley\current => ~\scoop\apps\atomicparsley\0.9.0
Creating shim for 'AtomicParsley'.
'atomicparsley' (0.9.0) was installed successfully!
==== HTTP Error 403: Forbidden が発生する場合 ====
$ youtube-dl https://www.youtube.com/watch?v=Hs-h6xmzaaU
[youtube] Hs-h6xmzaaU: Downloading webpage
ERROR: unable to download video data: HTTP Error 403: Forbidden
youtube-dl のダウンロードキャッシュを削除する。\\
$ youtube-dl --rm-cache-dir
Removing cache dir C:\Users\tomoyan/.cache\youtube-dl ...
キャッシュ削除後に再度ダウンロードしなおす。\\
$ youtube-dl https://www.youtube.com/watch?v=Hs-h6xmzaaU
[youtube] Hs-h6xmzaaU: Downloading webpage
[youtube] Hs-h6xmzaaU: Downloading js player vflset
[youtube] Hs-h6xmzaaU: Downloading js player vflset
[download] Destination: 高橋真梨子(たかはし まりこ) For You..( 歌詞付き).中日字幕.譚詠麟 霧之戀 - 原曲-Hs-h6xmzaaU.f248.webm
[download] 100% of 19.66MiB in 00:06
[download] Destination: 高橋真梨子(たかはし まりこ) For You..( 歌詞付き).中日字幕.譚詠麟 霧之戀 - 原曲-Hs-h6xmzaaU.f251.webm
[download] 100% of 4.46MiB in 00:01
[ffmpeg] Merging formats into "高橋真梨子(たかはし まりこ) For You..( 歌詞付き).中日字幕.譚詠麟 霧之戀 - 原曲-Hs-h6xmzaaU.webm"
Deleting original file 高橋真梨子(たかはし まりこ) For You..( 歌詞付き).中日字幕.譚詠麟 霧之戀 - 原曲-Hs-h6xmzaaU.f248.webm (pass -k to keep)
Deleting original file 高橋真梨子(たかはし まりこ) For You..( 歌詞付き).中日字幕.譚詠麟 霧之戀 - 原曲-Hs-h6xmzaaU.f251.webm (pass -k to keep)
===== 参考文献 =====
[[https://aprico-media.com/posts/1374|youtube-dlgの使い方解説!日本語化/ダウンロードできない/評判まとめ! | Aprico]]\\
[[https://qastack.jp/unix/272868/download-only-format-mp4-on-youtube-dl|youtube-dlで形式mp4のみをダウンロードする]]\\
[[https://askubuntu.com/questions/634584/how-to-download-youtube-videos-as-a-best-quality-audio-mp3-using-youtube-dl|sound - How to download youtube videos as a best quality audio mp3 using youtube-dl - Ask Ubuntu]]\\