youtube – matoken's blog https://matoken.org/blog Is there no plan B? Wed, 20 Apr 2022 12:05:15 +0000 ja hourly 1 https://wordpress.org/?v=6.8.1 https://matoken.org/blog/wp-content/uploads/2025/03/cropped-1865f695c4eecc844385acef2f078255036adccd42c254580ea3844543ab56d9-32x32.jpeg youtube – matoken's blog https://matoken.org/blog 32 32 yt-dlpでダウンロードしたYouTube動画を視聴履歴に https://matoken.org/blog/2022/04/20/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history-2/ https://matoken.org/blog/2022/04/20/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history-2/#respond Wed, 20 Apr 2022 11:57:45 +0000 http://matoken.org/blog/?p=3619

yt-dlpで動画をダウンロードすると既定値では視聴履歴に残りません.視聴履歴を残すようにしてみました.

yt-dlp -h を見ると以下のあたりで実現できそうです.

    --cookies FILE                   Netscape formatted file to read cookies
                                     from and dump cookie jar in
    --no-cookies                     Do not read/dump cookies from/to file
                                     (default)
    --cookies-from-browser BROWSER[+KEYRING][:PROFILE]
                                     The name of the browser and (optionally)
                                     the name/path of the profile to load
                                     cookies from, separated by a ":". Currently
                                     supported browsers are: brave, chrome,
                                     chromium, edge, firefox, opera, safari,
                                     vivaldi. By default, the most recently
                                     accessed profile is used. The keyring used
                                     for decrypting Chromium cookies on Linux
                                     can be (optionally) specified after the
                                     browser name separated by a "+". Currently
                                     supported keyrings are: basictext,
                                     gnomekeyring, kwallet
    --no-cookies-from-browser        Do not load cookies from browser (default)

YouTubeにログイン済のブラウザのcookie情報を利用

--cookies-from-browser を利用してYouTubeにログイン済のウェブブラウザのcookie情報からYouTubeに閲覧情報を残します.ローカルPCで使う場合に便利です.

対応ウェブブラウザは,brave, chrome, chromium, edge, firefox, opera, safari, vivaldi となっています.
複数プロファイルを利用している場合でも引数で指定可能です.
今回はFirefoxでYouTubeというプロファイルで試します.

Note
$ firefox --ProfileManager (1)
$ firefox -P YouTube (2)
$ firefox --profile  ~/.mozilla/firefox/$( grep YouTube ~/.mozilla/firefox/installs.ini | cut -f2 -d= )/ (3)
  1. プロファイルの確認のためにプロファイルマネージャの起動
  2. YouTube というプロファイルで起動
  3. Youtube の含まれているプロファイル名で起動
$ yt-dlp \
    --cookies-from-browser firefox:YouTube \ (1)
    --playlist-items 1 https://www.youtube.com/c/OSPNjp/videos (2)
  1. firefoxのYouTubeというプロファイルのcookieを利用
  2. 適当なYouTube URL

プロファイルが1つしかなかったり,デフォルトの場合はプロファイルを書く必要はありません.

うまくいけばyt-dlp終了後YouTubeの履歴を確認すると該当の動画が履歴に登録されているはずです.

ウェブブラウザからYouTubeのcookieを書き出して利用

--cookies-from-browser は便利ですが,リモートやヘッドレス環境だとGUIが必要になり面倒です.ウェブブラウザから該当ページのcookie をファイルに書き出し,--cookies オプションで利用します.

まず,YouTubeにログインしているウェブブラウザにcookieを書き出すための cookies.txt という拡張機能を導入します.

次にYouTubeのページを開いた状態で,拡張機能のアイコンをクリックして,「Current Site」を選び,cookieをファイルに保存します.

Fx cookies.txt

保存したcookieファイルを --cookies オプションに指定して実行します.

$ yt-dlp \
    --cookies ./YouTube-cookie.txt (1)
    --playlist-items 1 https://www.youtube.com/c/OSPNjp/videos (2)
  1. 保存したcookieファイルを指定
  2. 適当なYouTube URL

うまくいけばyt-dlp終了後YouTubeの履歴を確認すると該当の動画が履歴に登録されているはずです.

cookieファイルを持っていけば別の端末でも利用可能です.セッションが切れたらcookieファイルは再度作成する必要があると思います.

ユーザ名,パスワードをnetrcファイルに書いて認証(失敗)

netrcファイルにYouTubeのユーザ名,パスワード(2要素認証を有効にしているのでアプリケーションパスワードを生成)を登録してみましたが,この手順だと認証エラーになりうまく行きませんでした.
恐らくreCAPTCHAで引っかかっているのではないかと思います.
お手軽なんですが残念.

環境

$ yt-dlp --version
2022.03.08.1
$ dpkg-query -W firefox-esr ffmpeg
ffmpeg  7:4.3.3-0+deb11u1
firefox-esr     78.15.0esr-1~deb11u1
$ lsb_release -dr
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
$ arch
x86_64
]]>
https://matoken.org/blog/2022/04/20/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history-2/feed/ 0
yt-dlpでダウンロードしたYouTube動画を視聴履歴に https://matoken.org/blog/2022/04/14/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history/ https://matoken.org/blog/2022/04/14/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history/#respond Wed, 13 Apr 2022 22:19:23 +0000 http://matoken.org/blog/?p=3597

yt-dlpで動画をダウンロードすると既定値では視聴履歴に残りません.視聴履歴を残すようにしてみました.


yt-dlp -h を見ると以下のあたりで実現できそうです.

    --cookies FILE                   Netscape formatted file to read cookies
                                     from and dump cookie jar in
    --no-cookies                     Do not read/dump cookies from/to file
                                     (default)
    --cookies-from-browser BROWSER[+KEYRING][:PROFILE]
                                     The name of the browser and (optionally)
                                     the name/path of the profile to load
                                     cookies from, separated by a ":". Currently
                                     supported browsers are: brave, chrome,
                                     chromium, edge, firefox, opera, safari,
                                     vivaldi. By default, the most recently
                                     accessed profile is used. The keyring used
                                     for decrypting Chromium cookies on Linux
                                     can be (optionally) specified after the
                                     browser name separated by a "+". Currently
                                     supported keyrings are: basictext,
                                     gnomekeyring, kwallet
    --no-cookies-from-browser        Do not load cookies from browser (default)
  Authentication Options:
    -u, --username USERNAME          Login with this account ID
    -p, --password PASSWORD          Account password. If this option is left
                                     out, yt-dlp will ask interactively
    -2, --twofactor TWOFACTOR        Two-factor authentication code
    -n, --netrc                      Use .netrc authentication data
    --netrc-location PATH            Location of .netrc authentication data;
                                     either the path or its containing
                                     directory. Defaults to ~/.netrc
    --video-password PASSWORD        Video password (vimeo, youku)
    --ap-mso MSO                     Adobe Pass multiple-system operator (TV
                                     provider) identifier, use --ap-list-mso for
                                     a list of available MSOs
    --ap-username USERNAME           Multiple-system operator account login
    --ap-password PASSWORD           Multiple-system operator account password.
                                     If this option is left out, yt-dlp will ask
                                     interactively
    --ap-list-mso                    List all supported multiple-system
                                     operators

==

youTubeにログイン済のブラウザのcookie情報を利用

--cookies-from-browser を利用してYouTubeにログイン済のウェブブラウザのcookie情報からYouTubeに閲覧情報を残します.ローカルPCで使う場合に便利です.

対応ウェブブラウザは,brave, chrome, chromium, edge, firefox, opera, safari, vivaldi となっています.
複数プロファイルを利用している場合でも引数で指定可能です.
今回はFirefoxでYouTubeというプロファイルで試します.

Note
$ firefox --ProfileManager (1)
$ firefox -P YouTube (2)
$ firefox --profile  ~/.mozilla/firefox/$( grep YouTube ~/.mozilla/firefox/installs.ini | cut -f2 -d= )/ (3)
  1. プロファイルの確認のためにプロファイルマネージャの起動
  2. YouTube というプロファイルで起動
  3. Youtube の含まれているプロファイル名で起動
$ yt-dlp \
    --cookies-from-browser firefox:YouTube \ (1)
    --playlist-items 1 https://www.youtube.com/c/OSPNjp/videos (2)
  1. firefoxのYouTubeというプロファイルのcookieを利用
  2. 適当なYouTube URL

プロファイルが1つしかなかったり,デフォルトの場合はプロファイルを書く必要はありません.

うまくいけばyt-dlp終了後YouTubeの履歴を確認すると該当の動画が履歴に登録されているはずです.

ウェブブラウザからYouTubeのcookieを書き出して利用

--cookies-from-browser は便利ですが,リモートやヘッドレス環境だとGUIが必要になり面倒です.ウェブブラウザから該当ページのcookie をファイルに書き出し,--cookies オプションで利用します.

まず,YouTubeにログインしているウェブブラウザにcookieを書き出すための cookies.txt という拡張機能を導入します.

次にYouTubeのページを開いた状態で,拡張機能のアイコンをクリックして,「Current Site」を選び,cookieをファイルに保存します.

Fx cookies.txt

保存したcookieファイルを --cookies オプションに指定して実行します.

$ yt-dlp \
    --cookies ./YouTube-cookie.txt (1)
    --playlist-items 1 https://www.youtube.com/c/OSPNjp/videos (2)
  1. 保存したcookieファイルを指定
  2. 適当なYouTube URL

うまくいけばyt-dlp終了後YouTubeの履歴を確認すると該当の動画が履歴に登録されているはずです.

cookieファイルを持っていけば別の端末でも利用可能です.セッションが切れたらcookieファイルは再度作成する必要があると思います.

ユーザ名,パスワードをnetrcファイルに書いて認証(失敗)

netrcファイルにYouTubeのユーザ名,パスワード(2要素認証を有効にしているのでアプリケーションパスワードを生成)を登録してみましたが,この手順だと認証エラーになりうまく行きませんでした.
恐らくreCAPTCHAで引っかかっているのではないかと思います.
お手軽なんですが残念.

環境

$ yt-dlp --version
2022.03.08.1
$ dpkg-query -W firefox-esr ffmpeg
ffmpeg  7:4.3.3-0+deb11u1
firefox-esr     78.15.0esr-1~deb11u1
$ lsb_release -dr
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
$ arch
x86_64
]]>
https://matoken.org/blog/2022/04/14/youtube-videos-downloaded-with-yt-dlp-in-your-viewing-history/feed/ 0
yt-dlpでYouTubeで配信されているラジオをダウンロードして視聴する https://matoken.org/blog/2022/04/06/download-and-watch-the-radio-delivered-on-youtube-with-yt-dlp/ https://matoken.org/blog/2022/04/06/download-and-watch-the-radio-delivered-on-youtube-with-yt-dlp/#respond Wed, 06 Apr 2022 10:19:49 +0000 http://matoken.org/blog/?p=3574

YouTubeで配信されているラジオ番組の1話を視聴して面白かったのでダウンロードして移動中などに聞こうと思いました.ダウンロードにはyt-dlpを利用しました.

使い方はyoutube-dlとほぼ同じですが少し動作が違う部分があるので,NEW FEATURES, Differences in default behavior に目を通しておくといいと思います.

対応サイトは以下を見ると現在1500以上ありそうです.

$ curl -s https://raw.githubusercontent.com/yt-dlp/yt-dlp/master/supportedsites.md | grep -E -- ^\ -\ ** | wc -l
1532

ということでまずは導入.いくつか導入方法がありますが,シングルバイナリをダウンロードするのがお手軽かなと思います.

$ wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp https://github.com/yt-dlp/yt-dlp/releases/latest/download/SHA2-512SUMS
$ sha512sum -c ./SHA2-512SUMS 2>&1 | grep OK
yt-dlp: OK
$ install -m700 ./yt-dlp ~/bin/
$ rm ./yt-dlp ./SHA2-512SUMS

アップデートは -U オプションをつけて実行するだけでOKなのでお手軽です.

$ yt-dlp  -U
Latest version: 2022.03.08.1, Current version: 2022.03.08.1
yt-dlp is up to date (2022.03.08.1)

まずは -F オプションでフォーマットを確認.

$ yt-dlp -F https://www.youtube.com/watch?v=A3vKWR3WXWk
[youtube] A3vKWR3WXWk: Downloading webpage
[youtube] A3vKWR3WXWk: Downloading android player API JSON
[info] Available formats for A3vKWR3WXWk:
ID  EXT   RESOLUTION FPS │  FILESIZE  TBR PROTO │ VCODEC       VBR ACODEC      ABR     ASR MORE INFO
────────────────────────────────────────────────────────────────────────────────────────────────────────────
sb2 mhtml 48x27          │                mhtml │ images                                   storyboard
sb1 mhtml 80x45          │                mhtml │ images                                   storyboard
sb0 mhtml 160x90         │                mhtml │ images                                   storyboard
139 m4a   audio only     │  11.91MiB  48k https │ audio only       mp4a.40.5   48k 22050Hz low, m4a_dash
249 webm  audio only     │  12.64MiB  51k https │ audio only       opus        51k 48000Hz low, webm_dash
250 webm  audio only     │  16.25MiB  66k https │ audio only       opus        66k 48000Hz low, webm_dash
140 m4a   audio only     │  31.61MiB 129k https │ audio only       mp4a.40.2  129k 44100Hz medium, m4a_dash
251 webm  audio only     │  30.82MiB 126k https │ audio only       opus       126k 48000Hz medium, webm_dash
17  3gp   176x144     12 │  13.01MiB  53k https │ mp4v.20.3    53k mp4a.40.2    0k 22050Hz 144p
160 mp4   256x144     24 │   3.25MiB  13k https │ avc1.4d400c  13k video only              144p, mp4_dash
278 webm  256x144     24 │   4.25MiB  17k https │ vp9          17k video only              144p, webm_dash
133 mp4   426x240     24 │   5.94MiB  24k https │ avc1.4d4015  24k video only              240p, mp4_dash
242 webm  426x240     24 │   6.80MiB  27k https │ vp9          27k video only              240p, webm_dash
134 mp4   640x360     24 │   9.56MiB  39k https │ avc1.4d401e  39k video only              360p, mp4_dash
18  mp4   640x360     24 │  48.99MiB 200k https │ avc1.42001E 200k mp4a.40.2    0k 44100Hz 360p
243 webm  640x360     24 │  24.36MiB  99k https │ vp9          99k video only              360p, webm_dash
135 mp4   854x480     24 │  13.12MiB  53k https │ avc1.4d401e  53k video only              480p, mp4_dash
244 webm  854x480     24 │  19.99MiB  81k https │ vp9          81k video only              480p, webm_dash
136 mp4   1280x720    24 │  17.42MiB  71k https │ avc1.4d401f  71k video only              720p, mp4_dash
22  mp4   1280x720    24 │ ~50.09MiB 200k https │ avc1.64001F 200k mp4a.40.2    0k 44100Hz 720p
247 webm  1280x720    24 │  38.85MiB 159k https │ vp9         159k video only              720p, webm_dash
137 mp4   1920x1080   24 │  42.01MiB 172k https │ avc1.640028 172k video only              1080p, mp4_dash
248 webm  1920x1080   24 │  71.89MiB 294k https │ vp9         294k video only              1080p, webm_dash

ラジオで静止画しか付いていないので,音質が良くて小さいopusの 251 がいいかなと思ったのですが, --embed-thumbnail オプションでサムネイルを埋め込もうとするとopusのwebpには埋め込めないようだったのでmp4aの 140 にすることにしました.

yt-dlp -f 140 –embed-thumbnail A3vKWR3WXWk
[ThumbnailsConvertor] Converting thumbnail "/tmp/【公式】その着せ替え人形はラジオをする(第1回) [A3vKWR3WXWk].webp" to png
ERROR: Supported filetypes for thumbnail embedding are: mp3, mkv/mka, ogg/opus/flac, m4a/mp4/mov

ということでこういう感じでサムネイル画像埋め込みでダウンロードできました.

$ yt-dlp -f 140 --embed-thumbnail A3vKWR3WXWk

ファイラーでもサムネイルが表示されていい感じです.

yt dlp thumbnail

1話目がダウンロードできましたが,現在13話まで配信されています.1話づつダウンロードするのは面倒.プレイリストはあるけどラジオ以外のものも配信されています.でもタイトルを見るとラジオかどうかはわかるのでタイトルでフィルターしてラジオだけをダウンロードすることにしました.

フィルタには --match-filter オプションを使い,title~=フィルタ という感じで使います.ということでこんな感じでダウンロードしてみると,プレイリストをチェックしてフィルタにマッチするものだけをダウンロードしてくれました.これでオフラインでもいつでも聞けます :)

$ yt-dlp -f 140 --embed-thumbnail --match-filter "title~=ラジオをする" https://www.youtube.com/watch?list=PLUH30mfo5wJwdafMdAJFcrcryUbYiixl4
環境
$ yt-dlp --version
2022.03.08.1
$ dpkg-query -W python3 ffmpeg
ffmpeg  7:4.3.3-0+rpt3+deb11u1
python3 3.9.2-3
$ lsb_release -dr
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
$ arch
armv7l
$ cat /proc/device-tree/model && echo
Raspberry Pi 3 Model B Rev 1.2
]]>
https://matoken.org/blog/2022/04/06/download-and-watch-the-radio-delivered-on-youtube-with-yt-dlp/feed/ 0
YouTube動画をダウンロードして無音部分をカットしたオーディオデータにして聞く https://matoken.org/blog/2022/03/13/ownload-youtube-video-and-listen-to-it-as-audio-data-with-silence-cut/ https://matoken.org/blog/2022/03/13/ownload-youtube-video-and-listen-to-it-as-audio-data-with-silence-cut/#respond Sun, 13 Mar 2022 14:40:28 +0000 http://matoken.org/blog/?p=3531

オープンソースカンファレンス2022 Online Springに参加しました.

ビデオミーティングソフトウェアのZoomとYouTube Liveを使っています.セッションは複数トラックあって同時に参加するのは難しいです.ほとんどのセッションは後日YouTubeで公開されますが,YouTube Liveの動画をリモートのサーバで録画しておいたのでこれをスマートフォンで移動中などに聞けるようにしてみました.

YouTubeの動画ダウンロードにはyoutube-dlやyt-dlpが便利です.セッション毎に起動しておきました.

$ youtube-dl ${URL}
$ yt-dlp ${URL}

今回ダウンロードした動画はvp9+aacのようです.

$ ffprobe -i ./YOUTUBEFILE.mkv 2>&1 | grep ^Input -A99
Input #0, matroska,webm, from './YOUTUBEFILE.mkv':
  Metadata:
    HTTP://YOUTUBE.COM/STREAMING/OTF/DURATIONS/112015: Segment-Count: 6109
                    : Segment-Durations-Ms: 5067,5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),5066,5067(r=1),506
                    :
                    :
    ENCODER         : Lavf58.20.100
  Duration: 08:35:50.04, start: 0.000000, bitrate: 551 kb/s
    Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv, bt709/unknown/unknown), 1280x720, SAR 1:1 DAR 16:9, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      DURATION        : 08:35:49.996000000
    Stream #0:1(eng): Audio: aac (LC), 44100 Hz, stereo, fltp (default)
    Metadata:
      HANDLER_NAME    : ISO Media file produced by Google Inc.
      DURATION        : 08:35:50.040000000

ffmpegで音声だけを抜き出してみます.

$ ffmpeg -i ./YOUTUBEFILE.mkv -vn -acodec copy ./YOUTUBEFILE.aac

元ファイルが2GBほどに対して500MBほどのファイルになりました.
ちょっと大きいのでコーデックをopusに変更してみます.

$ ffmpeg -i ./YOUTUBEFILE.mkv -vn -acodec libopus -b:a 64k ./YOUTUBEFILE.opus

213MB程になりました.
これをローカルにダウンロードして再生してみるとセッション間の無音部分が気になります.無音部分をカットできないかなと探すと,ffmpegのフィルタにそれらしい silenceremove というフィルタがあったので試してみました.

とりあえずexampleを真似して,先頭から最後までの間の1秒以上の無音部分をカットします.ついでにビットレートも32kに下げました.

$ ffmpeg -i ./YOUTUBEFILE.mkv -vn -af silenceremove=stop_periods=-1:stop_duration=1:stop_threshold=-90dB -acodec libopus -b:a 32k ./YOUTUBEFILE.opus

動画ファイルの長さが 08:35:50.040000000 から 07:55:03.66 になりました.ファイルサイズは107MB程です.

ついでにもう少し低いビットレートも試しました.

ビットレート 容量

32k

107324kB

24k

82484kB

16k

56380kB

少し聞いた感じでは16kでも問題無さそうな感じです.大本のZoomが確か24kなので24k以上はあまり意味がない気がします.

追記)高音質化機能があるようなので動画によって変わりそうです:

High Fidelity Audio mode – Windows, macOS
This option in Advanced Audio enhances “Original Audio” mode, allowing for disabling echo cancellation & post-processing, while raising audio codec quality to 48Khz, 96Kbps mono/192kbps stereo for professional audio transmis
sion in music education and performance applications. Professional audio interface, microphone, and headphones required.

Release notes for September 1
2020 – Zoom Support https://support.zoom.us/hc/en-us/articles/360048388632-New-Updates-for-September-1-2020

ということで他のセッションも同様に変換してAndroidスマートフォンに転送してVLCで1.5倍再生してみて画面スリープで再生できるのを確認しました.これで空き時間に聞けるようになりました :)

youtube2audio

応用としてYouTubeのRSSから動画をダウンロードして音声のみにしてその音声をRSSに埋め込んで公開するとPodcastアグリゲーターで購読できるので便利かもしれません.人の動画を勝手に一般公開するとまずいでしょうが.

環境
$ youtube-dl --version
2021.12.17
$ yt-dlp --version
2022.03.08.1
$ dpkg-query -W ffmpeg
ffmpeg  7:4.1.8-0+deb10u1
$ lsb_release -dr
Description:    Debian GNU/Linux 10 (buster)
Release:        10
$ arch
x86_64
]]>
https://matoken.org/blog/2022/03/13/ownload-youtube-video-and-listen-to-it-as-audio-data-with-silence-cut/feed/ 0
Youtube-dlコマンドで字幕をダウンロードする https://matoken.org/blog/2020/05/05/download-subtitles-with-the-youtube-dl-command/ https://matoken.org/blog/2020/05/05/download-subtitles-with-the-youtube-dl-command/#respond Mon, 04 May 2020 16:05:55 +0000 http://matoken.org/blog/?p=2725
add 2020-06-12

以下の例では動画ファイルと字幕ファイルが別々に作成されます.動画に字幕ファイルを埋め込めないかなとオプションを確認したら --embed-subs というオプションがありました.これを利用したら1つの動画ファイルの中に字幕も埋め込めました :)

$ youtube-dl --help|grep -- --embed
    --embed-subs                     Embed subtitles in the video (only for mp4, webm and mkv videos)
    --embed-thumbnail                Embed thumbnail in the audio as cover art

ただし,Debian sid amd64環境では別途 atomicparsley パッケージも必要でした.

atomicparsley パッケージを入れる前のエラー
ERROR: AtomicParsley was not found. Please install.
atomicparsley パッケージの導入
$ sudo apt install atomicparsley
480pでダウンロードして日本語機械翻訳字幕を埋め込み
$ youtube-dl --write-auto-sub --sub-lang=ja --embed-subs -c -f 133 -o './%(title)s.%(ext)s' 'https://www.youtube.com/playlist?list=PLYUtdmpYPTTKgmkaIUFDiNvYPxQUzuY8y'
以下のあたりでも書いたのですが,回線が不安定で途中で止まったり解像度が低かったりするのでVODをローカルにダウンロードしてから視聴することが多いです.

日本語の動画の場合はこれで問題ないのですが, id:naruoga/@naru0gaさんがLibreOffice Asia Conference 2019 Tokyo 基調講演の動画に日本語字幕を付けてくれました.OSSが興味ある人なら気になる感じの内容のようなので視聴したいけど字幕ダウンロードしたこと無いなと試してみました.

導入していない人はインストールしましょう.バイナリを1つダウンロードして実行権を付けるだけです.以下は ~/bin に導入する例.

$ curl -L https://yt-dl.org/downloads/latest/youtube-dl -o ~/bin/youtube-dl
$ chmod +rx ~/bin/youtube-dl

先ずはhelpを確認します.

$ youtube-dl --help|grep 'Subtitle Options:' -A7
  Subtitle Options:
    --write-sub                      Write subtitle file
    --write-auto-sub                 Write automatically generated subtitle file (YouTube only)
    --all-subs                       Download all the available subtitles of the video
    --list-subs                      List all available subtitles for the video
    --sub-format FORMAT              Subtitle format, accepts formats preference, for example: "srt" or "ass/srt/best"
    --sub-lang LANGS                 Languages of the subtitles to download (optional) separated by commas, use --list-subs for available
                                     language tags

結構シンプルな感じですね.早速試してみます.

先ずは --list-subs で字幕の一覧を取得してみます.

$ youtube-dl --ignore-config --list-subs QS-Zz-2ovo0
[youtube] QS-Zz-2ovo0: Downloading webpage
[youtube] QS-Zz-2ovo0: Looking for automatic captions
[youtube] QS-Zz-2ovo0: Downloading MPD manifest
Available automatic captions for QS-Zz-2ovo0:
Language formats
gu       vtt, ttml, srv3, srv2, srv1
zh-Hans  vtt, ttml, srv3, srv2, srv1
zh-Hant  vtt, ttml, srv3, srv2, srv1
gd       vtt, ttml, srv3, srv2, srv1
ga       vtt, ttml, srv3, srv2, srv1
gl       vtt, ttml, srv3, srv2, srv1
lb       vtt, ttml, srv3, srv2, srv1
la       vtt, ttml, srv3, srv2, srv1
lo       vtt, ttml, srv3, srv2, srv1
tt       vtt, ttml, srv3, srv2, srv1
tr       vtt, ttml, srv3, srv2, srv1
lv       vtt, ttml, srv3, srv2, srv1
lt       vtt, ttml, srv3, srv2, srv1
tk       vtt, ttml, srv3, srv2, srv1
th       vtt, ttml, srv3, srv2, srv1
tg       vtt, ttml, srv3, srv2, srv1
te       vtt, ttml, srv3, srv2, srv1
fil      vtt, ttml, srv3, srv2, srv1
haw      vtt, ttml, srv3, srv2, srv1
yi       vtt, ttml, srv3, srv2, srv1
ceb      vtt, ttml, srv3, srv2, srv1
yo       vtt, ttml, srv3, srv2, srv1
de       vtt, ttml, srv3, srv2, srv1
da       vtt, ttml, srv3, srv2, srv1
el       vtt, ttml, srv3, srv2, srv1
eo       vtt, ttml, srv3, srv2, srv1
en       vtt, ttml, srv3, srv2, srv1
eu       vtt, ttml, srv3, srv2, srv1
et       vtt, ttml, srv3, srv2, srv1
es       vtt, ttml, srv3, srv2, srv1
ru       vtt, ttml, srv3, srv2, srv1
rw       vtt, ttml, srv3, srv2, srv1
ro       vtt, ttml, srv3, srv2, srv1
bn       vtt, ttml, srv3, srv2, srv1
be       vtt, ttml, srv3, srv2, srv1
bg       vtt, ttml, srv3, srv2, srv1
uk       vtt, ttml, srv3, srv2, srv1
jv       vtt, ttml, srv3, srv2, srv1
bs       vtt, ttml, srv3, srv2, srv1
ja       vtt, ttml, srv3, srv2, srv1
or       vtt, ttml, srv3, srv2, srv1
xh       vtt, ttml, srv3, srv2, srv1
co       vtt, ttml, srv3, srv2, srv1
ca       vtt, ttml, srv3, srv2, srv1
cy       vtt, ttml, srv3, srv2, srv1
cs       vtt, ttml, srv3, srv2, srv1
ps       vtt, ttml, srv3, srv2, srv1
pt       vtt, ttml, srv3, srv2, srv1
pa       vtt, ttml, srv3, srv2, srv1
vi       vtt, ttml, srv3, srv2, srv1
pl       vtt, ttml, srv3, srv2, srv1
hy       vtt, ttml, srv3, srv2, srv1
hr       vtt, ttml, srv3, srv2, srv1
ht       vtt, ttml, srv3, srv2, srv1
hu       vtt, ttml, srv3, srv2, srv1
hmn      vtt, ttml, srv3, srv2, srv1
hi       vtt, ttml, srv3, srv2, srv1
ha       vtt, ttml, srv3, srv2, srv1
mg       vtt, ttml, srv3, srv2, srv1
uz       vtt, ttml, srv3, srv2, srv1
ml       vtt, ttml, srv3, srv2, srv1
mn       vtt, ttml, srv3, srv2, srv1
mi       vtt, ttml, srv3, srv2, srv1
mk       vtt, ttml, srv3, srv2, srv1
ur       vtt, ttml, srv3, srv2, srv1
mt       vtt, ttml, srv3, srv2, srv1
ms       vtt, ttml, srv3, srv2, srv1
mr       vtt, ttml, srv3, srv2, srv1
ug       vtt, ttml, srv3, srv2, srv1
ta       vtt, ttml, srv3, srv2, srv1
my       vtt, ttml, srv3, srv2, srv1
af       vtt, ttml, srv3, srv2, srv1
sw       vtt, ttml, srv3, srv2, srv1
is       vtt, ttml, srv3, srv2, srv1
am       vtt, ttml, srv3, srv2, srv1
it       vtt, ttml, srv3, srv2, srv1
iw       vtt, ttml, srv3, srv2, srv1
sv       vtt, ttml, srv3, srv2, srv1
ar       vtt, ttml, srv3, srv2, srv1
su       vtt, ttml, srv3, srv2, srv1
zu       vtt, ttml, srv3, srv2, srv1
az       vtt, ttml, srv3, srv2, srv1
id       vtt, ttml, srv3, srv2, srv1
ig       vtt, ttml, srv3, srv2, srv1
nl       vtt, ttml, srv3, srv2, srv1
no       vtt, ttml, srv3, srv2, srv1
ne       vtt, ttml, srv3, srv2, srv1
ny       vtt, ttml, srv3, srv2, srv1
fr       vtt, ttml, srv3, srv2, srv1
ku       vtt, ttml, srv3, srv2, srv1
fy       vtt, ttml, srv3, srv2, srv1
fa       vtt, ttml, srv3, srv2, srv1
fi       vtt, ttml, srv3, srv2, srv1
ka       vtt, ttml, srv3, srv2, srv1
kk       vtt, ttml, srv3, srv2, srv1
sr       vtt, ttml, srv3, srv2, srv1
sq       vtt, ttml, srv3, srv2, srv1
ko       vtt, ttml, srv3, srv2, srv1
kn       vtt, ttml, srv3, srv2, srv1
km       vtt, ttml, srv3, srv2, srv1
st       vtt, ttml, srv3, srv2, srv1
sk       vtt, ttml, srv3, srv2, srv1
si       vtt, ttml, srv3, srv2, srv1
so       vtt, ttml, srv3, srv2, srv1
sn       vtt, ttml, srv3, srv2, srv1
sm       vtt, ttml, srv3, srv2, srv1
sl       vtt, ttml, srv3, srv2, srv1
ky       vtt, ttml, srv3, srv2, srv1
sd       vtt, ttml, srv3, srv2, srv1
Available subtitles for QS-Zz-2ovo0:
Language formats
en       vtt, ttml, srv3, srv2, srv1
ja       vtt, ttml, srv3, srv2, srv1

大量に出てきましたが,上の方の Available automatic captions の物はYoutubeの自動生成&自動翻訳されたもののようです.
Available subtitles 以下のものが人の手で翻訳されたものだと思います.今回の動画では en, ja があるようです.

--write-sub --sub-lang=ja で日本語の字幕を書き出すよう指定してみます.

$ youtube-dl --ignore-config --write-sub --sub-lang=ja QS-Zz-2ovo0

動画名.mp4動画名.ja.vtt というファイルが入手できました.動画名.言語.vtt というファイルが字幕ファイルのようです.この2ファイルが同じディレクトリにある状態で動画プレイヤーで .mp4 を再生すると字幕が表示されました :)

Totemでは表示されませんでしたが,メニューから字幕ファイルを指定すると表示されました.

$ ls LibreOffice\ Asia\ Conference\ 2019\ -\ LibreOffice\,\ the\ many\ different\ faces\ of\ a\ global\ community.*
'LibreOffice Asia Conference 2019 - LibreOffice, the many different faces of a global community.ja.vtt'
'LibreOffice Asia Conference 2019 - LibreOffice, the many different faces of a global community.mp4'
$ mpv ./LibreOffice\ Asia\ Conference\ 2019\ -\ LibreOffice\,\ the\ many\ different\ faces\ of\ a\ global\ community.mp4
$ cvlc ./LibreOffice\ Asia\ Conference\ 2019\ -\ LibreOffice\,\ the\ many\ different\ faces\ of\ a\ global\ community.mp4

youtu dl subtitle

これでとりあえず目的は果たせました.
でもYoutubeでのみですが,自動生成翻訳字幕のダウンロードが気になるのでこれも試してみます.
今度は日本語字幕の存在しないDebconf19のもので日本語の自動生成字幕 --sub-lang=ja --write-auto-sub オプション&解像度を240pにしておきます.

$ youtube-dl --sub-lang=ja --write-auto-sub -f 'bestvideo[height<=240]+bestaudio/best[height<=360]' U17DID4vdpc

自動生成&機械翻訳なのでおかしなところはありますがいけたようです.

youtube dl subtitle auto

後はライブ動画やライブ音声のリアルタイム翻訳ができるといいなーと思ったりも.DeepSpeechで文字起こししてみんなの翻訳とかDeeplに都賀具感じでリアルタイムで出来ないかなとか.

環境
$ youtube-dl --version
2020.05.03
$ dpkg-query -W mpv vlc totem firejail
firejail        0.9.62-3
mpv     0.32.0-1
totem   3.34.1-2+b1
vlc     3.0.10-1

9

]]>
https://matoken.org/blog/2020/05/05/download-subtitles-with-the-youtube-dl-command/feed/ 0