Video Stabilization Output Length Change

asked 2017-05-03 13:35:08 -0500

VideoCurious gravatar image

updated 2017-05-03 17:27:18 -0500

I'm performing video stabilization in OpenCV 3.2.0 using code from the videostab sample reduced down to simply perform one pass stabilization using OnePassStabilizer with a GaussianMotionFilter. The code was also enhanced to copy the source codec to the output writer. However, after copying the audio track from the original video to the stabilized video with FFmpeg I noticed that the audio wasn't correctly aligned. Further inspection revealed that the stabilized video (3:49.03) is slightly longer than the input video (3:47.05). The OpenCV code copies the source FPS to the output FPS, however, when comparing the frame rate in VLC the original video appears as 30.303030 while the stabilized video appears as 30.

Why is the stabilized video slightly longer than the original? Why is the stabilized video frame rate rounded down?

FFmpeg information for input video:

$ ffmpeg -i original.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'original.mp4':
    major_brand     : isom
    minor_version   : 0
    compatible_brands: isom3gp4
    creation_time   : 2016-02-25T14:09:56.000000Z   Duration: 00:03:47.05, start:
0.000000, bitrate: 4593 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 4498 kb/s, SAR 1:1 DAR 16:9, 30.30 fps, 30 tbr, 90k tbn, 180k tbc (default)
      creation_time   : 2016-02-25T14:09:56.000000Z
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
      creation_time   : 2016-02-25T14:09:56.000000Z
      handler_name    : SoundHandle At least one output file must be specified

FFmpeg information for stabilized video:

$ ffmpeg -i videostab.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'videostab.mp4':
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.1.0
  Duration: 00:03:49.03, start: 0.000000, bitrate: 2449 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 2446 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
      handler_name    : VideoHandler
At least one output file must be specified
edit retag flag offensive close merge delete



Why the framerate is rounded down, I don't know. Maybe a limitation in VideoWriter?

Possible workaround:

  • save the output frames on disk in image format (jpeg, png, ...)
  • use ffmpeg to create a video from pictures
Eduardo gravatar imageEduardo ( 2017-05-04 04:22:04 -0500 )edit

I appreciate the suggestion. Given the stabilized video is slightly longer I'm thinking the audio sync issue would still exist.

VideoCurious gravatar imageVideoCurious ( 2017-05-04 15:50:42 -0500 )edit

Not sure to understand? It is because VideoWriter encodes the output video at 30fps instead of 30.30fps you get a longer stabilized video.

If you save the stabilized output frames in an image sequence (image_0000.png, image_0001.png) and encodes with ffmpeg at the correct fps or correct length, it should be ok.

Eduardo gravatar imageEduardo ( 2017-05-05 03:22:38 -0500 )edit

I asked the question because I'm not certain what is happening exactly. I'd love to see what happens if VideoWriter encoded the video at 30.303030 fps as I'm requesting. It should be alright unless the fps is not the only problem. I wish someone familiar with the videostab code would respond! I appreciate you taking the time to provide a possible workaround, while it might work it would not be suitable for my needs.

VideoCurious gravatar imageVideoCurious ( 2017-05-09 16:23:33 -0500 )edit

I still haven't figured out why the FPS is not as I assign or why the video length changes. I'm surprised no one has noticed the FPS issue.

VideoCurious gravatar imageVideoCurious ( 2017-06-02 11:56:36 -0500 )edit