linux – How to detect with ffmpeg if the stream has video or audio or both?

I am listening on rtsp stream. The stream may potentially have only audio, only video, or have both, depending on how the remote side ffmpeg was configured. I want to dump the stream into hls. This is the code:

r ffmpeg \
        -hide_banner \
        -nostats \
        -i "rtsps://....." \
        -c:v copy \
        -c:a copy \
        -hls_init_time 2 \
        -hls_time 2 \
        -hls_list_size 100 \
        -strftime 1 \
        -hls_allow_cache 1 \
        -hls_segment_type fmp4 \
        -hls_segment_filename "$MTX_PATH-%Y%m%d%H%M%S.mp4" \
        -hls_flags 'append_list+discont_start+delete_segments+omit_endlist' \
        "$MTX_PATH.m3u8"

However, because of -c:v copy -v:a copy if the stream does not have video, or audio, ffmpeg will fail.

How can I tell ffmpeg to copy video if input has video, and copy audio if input has audio, and copy video and audio if input has video and audio?

Read more here: Source link