Search code examples
ffmpegscaleaspect

Auto aspect on stream with ffmpeg


I currently try to get an image each second from a udp stream with the following configuration:

ffmpeg -i udp://localhost:1234 -vf "scale=iw*sar:ih , pad=max(iw\,ih*(16/9)):ow/(16/9):(ow-iw)/2:(oh-ih)/2" -aspect 16:9  -update 1 img.jpg

  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: mjpeg, yuvj420p(pc), 1024x576 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.107.100 mjpeg
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1

If the first frame of stream is 4:3 black borders are added to the output image. If the aspect is changed in stream from 4:3 to 16:9 the output image still contains the black broder and the aspect of the image between the borders is 4:3.

If I restart ffmpeg while the aspect is 16:9 the borders are removed from the output and the output image looks fine.

In the console I get the following log on the aspect change of stream:

[swscaler @ 0x7fc6b1035600] deprecated pixel format used, make sure you did set range correctly

Is there any option to fix this issue.


Solution

  • Set the filters to evaluate expressions each frame,

    ffmpeg -i udp://localhost:1234 \
           -vf "scale=iw*sar:ih:eval=frame,setsar=1,\
                pad=max(iw\,ih*(16/9)):ow/(16/9):(ow-iw)/2:(oh-ih)/2:eval=frame" \
           -aspect 16:9  -update 1 img.jpg