Remove duplicate frames with FFmpeg

Run -vf mpdecimate -fps_mode vfr and copy the audio. Every kept frame keeps its timestamp, so the sound stays in sync: on a 60 fps clip made from 24 fps footage it kept 241 of 720 frames. Retime with setpts only when you want a shorter video, and cut or drop the audio with it.

Keep timing Re-encode

Drops every repeat and keeps the sound in sync. The file keeps its length.

ffmpeg -i in.mp4 -vf mpdecimate -fps_mode vfr -c:a copy out.mp4

Kept 241 of 720 frames, exactly the 241 distinct pictures. Each kept frame keeps its timestamp, so the copied audio stays in sync and the file still runs 12.0 s.

Frames
720 in, 241 out
Audio
Copied, 12.0 s, in sync
Run time
1.1 s
Output
3.3 MB
Shorter Re-encode

A shorter file at one steady frame rate, freezes cut out. Video only.

ffmpeg -i in.mp4 -vf "mpdecimate,setpts=N/24/TB" -r 24 -an out.mp4

241 frames at a constant 24 fps, 10.04 s. The repeats and the 2 s freeze are gone. The audio is left out because it no longer lines up with the picture.

Frames
241 at 24 fps
Length
12.0 s in, 10.04 s out
Run time
1.1 s
Output
3.0 MB

Proof

Eight frames in a row, before and after

Every frame of the test clip carries the number of the picture it came from, so a repeat shows as a repeated number.
in.mp4, frames 290 to 297 Each picture 2 or 3 times
Eight consecutive frames of the input: frame 116 three times, 117 twice, 118 three times
720 frames 24 fps footage stored at 60 fps: picture 116 three times, 117 twice, 118 three times.
mpdecimate -fps_mode vfr, frames 116 to 123 Each picture once
Eight consecutive frames of the output: 116, 117, 118, 119, 120, then 168, 169, 170
241 frames Frame 120 is followed by 168. The 2 s freeze is one stored frame now, shown from 5.0 s to 7.0 s, so the sound still matches.
setpts=N/FRAME_RATE/TB, at 2.0 s Video runs 2.5 times fast
Frame 120, a close-up of the girl, shown at 2.0 s of the output
frame 120 At 2.0 s this output shows frame 120, the picture from 5.0 s of the input. The copied audio is still at 2.0 s.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Frame numbers, the freeze and the 60 fps repeats added by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/remove-duplicate-frames/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -vf mpdecimate -fps_mode vfr -c:a copy out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/remove-duplicate-frames/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -vf mpdecimate -fps_mode vfr -c:a copy out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. See which frames are duplicates

    Read only
    ffmpeg -i in.mp4 -vf mpdecimate -loglevel debug -f null -

    One line per frame with its pts_time and the word keep or drop. The test clip logged 241 keep lines and 479 drop lines.

    290 ms run

  2. Find frozen sections

    Read only
    ffmpeg -i in.mp4 -vf freezedetect=d=1 -an -f null -

    Logged freeze_start: 5, freeze_duration: 2 and freeze_end: 7. The 60 fps repeats last 50 ms at most, so they never reach the 1 s minimum set by d=1.

    259 ms run

  3. Cut out a frozen section with its audio

    Re-encode
    ffmpeg -i in.mp4 -filter_complex "[0:v]trim=0:5,setpts=PTS-STARTPTS[v0];[0:a]atrim=0:5,asetpts=PTS-STARTPTS[a0];[0:v]trim=7,setpts=PTS-STARTPTS[v1];[0:a]atrim=7,asetpts=PTS-STARTPTS[a1];[v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

    The freeze from 5 s to 7 s is cut from picture and sound together: 10.0 s of video and 10.01 s of audio, still in step. Take the times from freezedetect.

    1.7 s run 4.7 MB 10.00 s long

  4. Remove duplicates and keep a constant frame rate

    Re-encode
    ffmpeg -i in.mp4 -vf "mpdecimate,fps=24" -c:a copy out.mp4

    288 frames at a constant 24 fps, 12.0 s, audio in sync. fps fills each gap with the last kept frame, so the 2 s freeze comes back as 48 repeats. Use it when an editor needs a fixed frame rate.

    1.2 s run 3.4 MB 12.00 s long

  5. Count the frames in a video

    Read only
    ffprobe -v error -select_streams v -count_frames -show_entries stream=nb_read_frames -of csv=p=0 in.mp4

    Prints 720. It decodes every frame to count them. Run it on the output to see what is left: 241 after the first answer.

    797 ms run

Gotchas

Where it goes wrong

Why is the audio out of sync after mpdecimate?

Because setpts=N/FRAME_RATE/TB packs the kept frames back to back at the input's frame rate, and nothing touches the audio. On the test clip the video ended after 4.02 s while the audio ran 12.0 s. This is the command most answers give, run with the audio copied:

ffmpeg -i in.mp4 -vf "mpdecimate,setpts=N/FRAME_RATE/TB" -c:a copy out.mp4

241 frames at 60 fps: 4.02 s of video against 12.0 s of audio. At 2.0 s the picture already shows what the input showed at 5.0 s.

FRAME_RATE is the input rate, 60 here, while the real pictures came 24 a second. Keep the timestamps instead (the first answer: -fps_mode vfr and no setpts) and the sound stays in step. To get a shorter file, retime at the real rate with setpts=N/24/TB and cut the audio at the same places, as in the freeze cut above.

Do I need -vsync vfr with mpdecimate?

Not on FFmpeg 8.0. Without it, MP4, MKV, MOV, AVI and MPEG-TS outputs all kept 241 frames. -vsync itself now prints "-vsync is deprecated. Use -fps_mode".

What puts the duplicates back is a fixed output rate:

ffmpeg -i in.mp4 -vf mpdecimate -r 60 -c:a copy out.mp4

720 frames again at a constant 60 fps. -fps_mode cfr did the same.

The commands on this page still write -fps_mode vfr, so the output keeps the gaps whatever the defaults do.

Does mpdecimate drop frames that are not duplicates?

Yes, in slow shots. It compares 8x8 pixel blocks against thresholds, not exact equality, so two real frames that barely differ count as a repeat. On 10 s of Sintel at its own 24 fps, which has no repeats, the defaults dropped 6 of 240 frames, all in two slow close-ups:

ffmpeg -i clean.mp4 -vf mpdecimate -loglevel debug -f null -

6 drop lines out of 240 frames. With -fps_mode vfr each dropped frame shows as the one before it, held twice as long.

Lowering hi keeps more real frames, and it also leaves more repeats behind, because re-encoded repeats are not bit-identical. Measured on both clips:

  • hi=384: 0 real frames dropped, but 266 frames left in in.mp4 where 241 are distinct.
  • hi=512: 2 real frames dropped, 243 left.
  • hi=640: 5 real frames dropped, 241 left.
  • Default hi=768: 6 real frames dropped, 241 left.

Will removing duplicate frames make the file smaller?

Barely. A repeated frame costs H.264 almost nothing to store. With the 479 repeats removed, the first answer wrote 3,274,043 bytes. The same re-encode with all 720 frames kept:

ffmpeg -i in.mp4 -c:a copy out.mp4

3,316,906 bytes. Removing the repeats saved 1.3%.

Remove duplicates to get one frame per real picture or to cut frozen stretches, not to save space.

Related: Change frame rate, Change video speed, Trim a video, FFprobe video info.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.