6

I removed many duplicate frames from video (and completely removed audio) by this command

ffmpeg -i scene.mkv -vf mpdecimate,setpts=N/FRAME_RATE/TB -map:v 0  scene3.mp4

(by an Mulvya's answer, thanks again).

The resulting video is OK but the total duration will not change - the last frame of original video is frozen to fill the remaining time (when playing back the video).

Is there some way to trim the video so that it will finish at this last frame?

Community
  • 1
  • 1
MarianD
  • 13,096
  • 12
  • 42
  • 54

2 Answers2

7

The map syntax should be

ffmpeg -i scene.mkv -vf mpdecimate,setpts=N/FRAME_RATE/TB -map 0:v scene3.mp4
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 2
    Is there any way to trim the audio as well while trimming the duplicate frames, I have duplicate frame in 1000s after every few 100 actual frames. And in those segments there is no audio. So 8 minutes video is trimmed to 2 minutes but the audio is untouched, Is it possible to remove the unwanted audio parts as well? – Saif Jun 18 '18 at 08:30
  • Works wonderfully, but the -map 0:v option will drop the audio track from the trimmed video. I think there should be a way to filter the video track while copying the audio using the timestamps of the keeped frames. It may lead to odd result but for a basic video with just comments on animated portions of the video should be fine. – Claudio Floreani Aug 24 '22 at 15:23
1

I tried the solution by Gyan, but it omits the audio. I also tried multiple other solutions and none of them seem to work. However there is a mpdecimate_trim repo that does a good job. It basically list all the frames to be dropped (using mpdecimate) and then creates a complex filter to trim all those frames (and the corresponding audio) from the video by splitting the video and only including the portion without duplicate frames.

I did have to tweak a few options in the code though. For instance, in mpdecimate_trim.py, I had to change this line:

dframes2 = get_dframes(ffmpeg(True, "-vf", "mpdecimate=hi=576", "-loglevel", "debug", "-f", "null", "-").stderr)

I had to detect duplicates a bit more aggressively, so I changed the mpdecimate option to mpdecimate=hi=64*32:lo=64*24:frac=0.05

Human
  • 8,380
  • 2
  • 19
  • 15