mattintosh note

どこかのエンジニアモドキの備忘録

コマンドラインから Chromecast に動画を送りたい

先日、会社用に Chromecast(第2世代)を買ってなかなか便利だなぁと思ったので自宅用にも買ってみた。

YouTube なんかの動画を再生するには当然便利なんだけど、ローカルの動画とかを再生しようとなると Google ChromeChromium)で file:/// にアクセスして Google Chrome の画面をキャスト…みたいなことになるのでプレイリストが使えなくて面倒くさい。普段は mpv を使っているので Chromecast 用のプラグインでも追加されてないかな〜と思ったけど今のところは非対応っぽい。

「じゃあ FFmpeg でしょ!」で、探してみると castnow っていうのがあった。

https://github.com/xat/castnow

へぇ〜こいつはすごいぜ!って入れようと思ったら Ubuntu の npm のバージョン低かったのでまず npm の更新からやらされる…orz

環境以下の通り。

Distributor ID:  Ubuntu
Description:    Ubuntu 17.04
Release:    17.04
Codename:   zesty

普通に npm パッケージを入れる。curlwget が無いとエラーになるので入ってなければ入れておく。

$ sudo apt-get install npm

んで npm で n というパッケージを入れて lts にしとく。n のバージョンは執筆時点では 2.1.7。

$ sudo npm install n -g
$ sudo n lts

npm をアップデートする。5.6.0 になった。

$ sudo npm update npm -g
$ npm -v
5.6.0

最後に castnow をインストールする。

$ sudo npm install castnow -g

そしてメディアファイルを指定するだけ。すばら〜♪

$ castnow video.mp4

FFmpeg から FLAC に変換しつつ再生とか。

$ ffmpeg -i audio.wav -f flac pipe:1 | castnow --quiet -

youtube-dl との連携もできる。マジ優秀。

$ youtube-dl -o - 'https://www.youtube.com/watch?v=ojM-qvNxgW4' | castnow --quiet -

castnow で再生しているものはスマートフォンからも操作ができる。M4A もそのまま再生できるし、標準入力の場合でも再生・停止などのコントロールできる。

f:id:mattintosh4:20171205001919p:plain f:id:mattintosh4:20171205001936p:plain

オプションはこんな感じ。

Usage: castnow [<media>, <media>, ...] [OPTIONS]

Option                   Meaning
--tomp4                  Convert file to mp4 during playback
--device <name>          The name of the Chromecast device that should be used
--address <ip>           The IP address or hostname of your Chromecast device
--subtitles <path/url>   Path or URL to an SRT or VTT file
--subtitle-scale <scale> Subtitle font scale
--subtitle-color <color> Subtitle font RGBA color
--subtitle-port <port>   Specify the port to be used for serving subtitles
--myip <ip>              Your local IP address
--quiet                  No output
--peerflix- <value>     Pass options to peerflix
--ffmpeg- <value>       Pass options to ffmpeg
--type <type>            Explicitly set the mime-type (e.g. "video/mp4")
--bypass-srt-encoding    Disable automatic UTF-8 encoding of SRT subtitles
--seek <hh:mm:ss>        Seek to the specified time on start using the format hh:mm:ss or mm:ss
--loop                   Loop over playlist, or file, forever
--shuffle                Play in random order
--recursive              List all files in directories recursively
--volume-step <step>     Step at which the volume changes. Helpful for speakers that are softer or louder than normal. Value ranges from 0 to 1 (e.g. ".05")
--localfile-port <port>  Specify the port to be used for serving a local file
--transcode-port <port>  Specify the port to be used for serving a transcoded file
--torrent-port <port>    Specify the port to be used for serving a torrented file
--stdin-port <port>      Specify the port to be used for serving a file read from stdin
--command <key1>,<key2>  Execute key command(s) (where each <key> is one of the keys listed below)
--exit                   Exit when playback begins or --command completes
--help                   This help screen

Player controls

Key                      Action
space                    Toggle between play and pause
m                        Toggle mute
t                        Toggle subtitles
up                       Volume Up
down                     Volume Down
left                     Seek backward
right                    Seek forward
n                        Next in playlist
s                        Stop playback
quit                     Quit

これは捗る!(;゚∀゚)=3ムッハー