ffmpeg を使って BS/CS の TS ファイルを分離する

ffmpeg の stream と map を使えばストリームを指定して動画を作ることが出来る。

  • p:program_id[:stream_index]
    • If stream_index is given, then it matches the stream with number stream_index in the program with the id program_id. Otherwise, it matches all streams in the program.

サービス ID を program_id として与えることでマルチ編成のストリームを分離することが出来る。

ffmpeg -i input.ts -map 0:p:23610:0 -map 0:p:23610:1 -vcodec copy -acodec copy -f mpegts mx2.ts

地上波だけなら program_id さえ与えれば、トラックは 0 (video) , 1 (audio) で固定なので、 MX2 や NHK のマルチ編成のストリームを分離することが出来るが、 BS や CS だとトラック ID が不定なので固定値ではチャンネルを分離することが出来ない。

  • stream_type[:stream_index]
    • stream_type is one of following: ’v’ or ’V’ for video, ’a’ for audio, ’s’ for subtitle, ’d’ for data, and ’t’ for attachments. ’v’ matches all video streams, ’V’ only matches video streams which are not attached pictures, video thumbnails or cover arts. If stream_index is given, then it matches stream number stream_index of this type. Otherwise, it matches all streams of this type.

stream_type を使うことで、 v なら video 、 a なら audio をトラック ID を指定することなく取得することが出来る。

ffmpeg -i input.ts -map 0:v:0 -map 0:a:0 -vcodec copy -acodec copy -f mpegts bs1.ts

しかし、この 2 つの指定を同時に行なうことは出来ない、ので標準入出力を使って複数の ffmpeg でストリームを分離していく。

ffmpeg -i input.ts -map 0:p:102 -vcodec copy -sn -dn -ignore_unknown -f mpegts pipe:1 | ffmpeg -i pipe:0 -map 0:v:0 -map 0:a:0 -vcodec copy -acodec copy -f mpegts bsp.ts

program_id だけ指定してエンコードを行なおうとした場合 data track や unknown track 含めることが出来ないようなので、 -dn-ignore_unknown を指定しておく。

これで BS プレミアムを録画した場合にうっかり BS910 が出てくることも無くなる。