Search code examples
ffmpegresolution

Force ffmpeg to quit when input resolution changes


I'm using ffmpeg to restream a live feed. Unfortunately occasionally the input resolution changes but ffmpeg continues running. The nginx rtmp server I'm using doesn't cope well with this, and continues the stream with audio, but the video is mostly black or green with some artifacts.

Ideally what I want to happen is for ffmpeg to stop on an input resolution change, as I have a script that detects ffmpeg stopping and will restart it again.

I'm using -c:v copy in my ffmpeg command as unfortunately my machine is not powerful enough to re-encode the live video on the fly to a constant resolution (not without a significant quality reduction at least)

ffmpeg -i "http://mpegts-live-stream" -c:v copy -c:a aac -ac 2 -f flv "rtmp://nginxserver/live/streamname"


Solution

  • So, ffmpeg can't detect input parameter change during streamcopy but we can work around that by adding a minimal decoding load to the process.

    ffmpeg -xerror -skip_frame:v nokey -flags:v +drop_changed -i "http://mpegts-live-stream" -c:v copy -c:a aac -ac 2 -f flv "rtmp://nginxserver/live/streamname" -an -f null -

    -xerror : exits upon error
    -skip_frame:v nokey : only decodes keyframes of video stream
    -flags:v +drop_changed : drops frame and signals error when parameters (like resolution) change
    -an -f null - : maps a video output stream which needs a decoded stream, needed else no decoding will occur.