Cutting a video with ffmpeg causes pixelation in the first few seconds — but not always

Problem Description:

I’m being sent videos by someone, and I need to remove a portion of the video from the front. For example, if the video is 1m30s, I might need only the last 1m of the video (in other words, I need to cut out the first 30s).

I use ffmpeg like this:

ffmpeg -ss 30 -i original.mp4 -codec copy shortened.mp4

What I find in nearly every case is that when I play the cropped video with VLC, the first several seconds are badly pixelated (as much as 10 seconds). The sound quality is perfect, however.

The original video doesn’t have this problem at all.

Replicating this is a bit of a problem:

  • When I try to make a video for the purpose of replicating the problem, I usually don’t get the problem, which is frustrating!

  • The videos that I’m sent contain people who haven’t given permission for me to share publicly, so I can’t post an example here. However, I include a cropped screenshot from VLC to show how bad it is.

Sample

So, I’m wondering if there is a problem with the codec inside the videos that I’m sent? They are created on a Samsung phone, but they aren’t all from the same model. (My phone, which doesn’t seem to cause this problem, is a Pixel.)

Or, maybe — I’m no expert! — is there something about synchronising the video that gets missed in the front of the copy?

Most important, of course, is how can I fix this, please?

Relevant System Information:

  • Ubuntu Version 22.04
  • ffmpeg version 4.4.2-0ubuntu0.22.04.1+esm6
  • VLC version 3.0.21

Thank you!

The issue occurs because when using -codec copy with -ss, FFmpeg can only cut at keyframes (I-frames) when stream copying. When you specify a timestamp that isn’t at a keyframe, FFmpeg will start at the nearest previous keyframe and the video player has to reconstruct the frames from there, leading to the pixelation you’re seeing.

One option:

ffmpeg -i original.mp4 -ss 30 -codec copy shortened.mp4

Notice the -ss comes after -i. This forces FFmpeg to decode the video first, then cut at the exact timestamp.

Second option:

ffmpeg -ss 30 -i original.mp4 -c:v libx264 -c:a aac shortened.mp4

This re-encodes the video instead of stream copying, ensuring clean cuts but taking longer.

Option three:

ffmpeg -skip_frame nokey -select_streams v -show_entries frame=pkt_pts_time -of csv=print_section=0 -i original.mp4

This command will show you keyframe timestamps. You can then choose the nearest keyframe to cut from.

For your use case, I’d recommend starting with solution #1 (two-pass method) since it’s fast and usually resolves the pixelation issue without quality loss. If you still see problems, then try solution #2 (re-encode method).

6 Likes

Thank you for the advice and explanation, @popey . I learned a new term, “keyframe”.

Solution #1

This was strange. For the first few seconds, VLC simply showed its logo, as though there were no video at all! So, unfortunately, this doesn’t work for me.

Solution #2

This is perfect, thank you!

I experimented with using copy instead of aac (for the audio). The results both seemed, to my ear at least, identical, but the one with aac resulted in a file that was 22% smaller! It’s strange how the audio can make such a difference to a video file.

I’ll stick with copy for the audio, because these are relatively small videos, and space isn’t a problem, unless you suggest that I do otherwise.

In hindsight, I should have looked at the encoding of the original file. “Properties” in Nautilus shows these:

Original video

  • Video codec: H.264 (High Profile), 25.00 fps, 1268 kbps
  • Sound codec: MPEG-4 AAC, Stereo, 48,000 Hz, 256 kbps

Shortened video (using copy for the audio)

  • Video codec: H.264 (High Profile), 29.67 fps, 620 kbps
  • Sound codec: MPEG-4 AAC, Stereo, 48,000 Hz, 256 kbps

I don’t know if there’s any point in trying to retain the fps in the re-encoding? I did try adding -r 25.00, and that unexpectedly made the video look slightly choppy, as if there were too few frames per second.

Thank you again, Alan!

1 Like

If you are interested there is a new world in video (and other assets) juggling found in the Wolfram language.

More at Wolfram Alpha.

1 Like

Oh, wow. I haven’t used Wolfram Alpha in years! Thank you

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.