admin管理员组

文章数量:1122846

I'm working with a MJPEG stream source

Stream #0:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn

I want to capture this stream and save it to an MP4 file using FFmpeg, but I am running into a weird issue.

The only way it works fine is when I just copy the stream without reencoding and using wallclock as timestamps (cmd 1):

ffmpeg -use_wallclock_as_timestamps 1 -i "http://IP/video/1280x720" -c:v copy output_good.mp4

However, when I try encoding the stream with libx264, the playback speed of the resulting video becomes slower than 100%, causing the video to gradually fall out of sync with real-time. This happens even when I use any encoding, also when I explicitly set the frame rate or vsync. For example, these commands fail:

  • CMD 2: ffmpeg -i "http://IP/video/1280x720" -c:v libx264 -r 30 -preset fast output_bad1.mp4
  • CMD 3: ffmpeg -use_wallclock_as_timestamps 1 -i "http://IP/video/1280x720" -c:v libx264 output.mp4

As you can see in the comparison of the resulting videos, the playback gradually slows down with CMD 2 and CMD 3, while it's alright with CMD 1.

What I've noticed is that on the FFmpeg stdout, when using encoding, the speed= and FPS= go up and up, e.g. to 31 FPS even though my source is technically at 25 FPS.

What I've tried:

  • different encoding codecs (e.g. libvpx)
  • -re
  • preset ultrafast on encoding
  • vsync (fps_mode), both ctr and vfr
  • fps=30 filter

Has anyone encountered a similar issue or know how to force FFmpeg to preserve the correct playback speed while encoding or enforcing a frame rate? Thanks!

本文标签: videoIssue with MJPEG stream playback speed when encoding with FFmpegStack Overflow