ヘッドレスブラウザのPuppeteerを使いWebRTCを通して、Videoタグの動画を相手に送るというのを行おうとしていたが、はじめうまく動作しなかった。
結果として、動画の形式(codec)が悪かったという話
動く動画形式
簡単に言うと、PuppeteerのデフォルトブラウザはChromiumで、Chromiumで動く動画・音声形式は以下ですよ、という感じらしい。
https://www.chromium.org/audio-video
Container formats
- MP4 (QuickTime/ MOV / MPEG4)
- Ogg
- WebM
- WAV
Codec formats (Decode Only)
Audio
- FLAC
- MP3
- Opus
- PCM 8-bit unsigned integer
- PCM 16-bit signed integer little endian
- PCM 32-bit float little endian
- Vorbis
- AAC [Main, LC, HE profiles only, xHE-AAC on Android P+] [Google Chrome only]
- AMR-NB [Google Chrome OS only]
- AMR-WB [Google Chrome OS only]
- PCM μ-law [Google Chrome OS only]
- GSM [Google Chrome OS Only]
Video
- VP8
- VP9
- AV1 [Only Chrome OS, Linux, macOS, and Windows X86 at present]
- Theora [Except on Android variants]
- H.264 [Google Chrome only]
- MPEG-4 [Google Chrome OS only]
で、適当な動画を再生しようとしたらH.264で、Google Chromeなら動くらしいが素のChromiumでは残念ながら動かないので、puppeteerでも動かなかった。
音声は対応形式だったので流れてた模様
というのでcodecを変えて、試しに
WebMコンテナ
で、Audio:Vorbis
、Video:VP8
でエンコードしたところ再生されました。
資料とそれ以外の方法
実はここに書いてあって、音声・動画系はうまく行かない時があるよとのこと。
https://github.com/puppeteer/puppeteer
You may find that Puppeteer does not behave as expected when controlling pages that incorporate audio and video. (For example, video playback/screenshots is likely to fail.)
これとは別に回避方法としては、Chromeをインストールしてそれを使う方法もあるようです
const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
この場合はpuppeteerではなくpuppeteer-coreで良いようです