map metadata stream by filter in ffmpeg

Viewed 827

I try to extract only one (meta)data stream from a mpegts stream

This are the streams returned by ffprobe

Stream #0:0[0x12c]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x12d]: Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s
Stream #0:2[0x5]: Data: bin_data ([6][0][0][0] / 0x0006)
Stream #0:3[0x262]: Data: bin_data (FBID / 0x44494246)
Stream #0:4[0x258]: Data: klv (KLVA / 0x41564C4B)

I only need the video, audio and the klv stream (0:4).

While I could map this stream by specifying its index
ffmpeg -i udp://[IP] -map 0:4 -map 0:v -map 0:a -f mpegts udp://[IP]

I don't want to rely on the the klv streams position in the container.
I also tried it like
ffmpeg -i udp://[IP] -map 0:d:m:klv -map 0:v -map 0:a -f mpegts udp://[IP]
But this was more guesswork.

How can I either include video, audio and klv or exclude the streams 0:2 and 0:3 without specifying the stream index so the resulting mpegts stream looks like?

Stream #0:0[0x12c]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x12d]: Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s
Stream #0:2[0x258]: Data: klv (KLVA / 0x41564C4B)
1 Answers

You can use ffprobe to determine index:

ffprobe -v error -select_streams d -show_entries stream=index,codec_name -of csv=p=0 udp://[IP]

Result:

2,klv

Bash example using ffprobe with awk to automatically get the KLV index:

ffmpeg -i udp://[IP] -map 0:v -map 0:a -map 0:"$(ffprobe -v error -select_streams d -show_entries stream=index,codec_name -of csv=p=0 udp://[IP] | awk -F, '/klv/ {print $1}')" -c copy -f mpegts udp://[IP]
Related