1

I have several 30fps videos to convert to videos with acceptable resolution and framerate for a DVD using this command:

ffmpeg.exe" -i "INPUT.MP4" -vf "select='mod(n,6)',crop=720:576:x=280:y=72" -r 25 -metadata:s:v:0 language= -metadata:s:a:0 language= -acodec pcm_s16le -ar 48000 "OUTPUT.MKV"

This does mostly what I anticipate. However, I want the command to select frames to remove using this arithmetic sequence: 6x-3 (3, 9, 15, 21, etc.). I experimented with the modulus function, however, I couldn't achieve the desired result.

I am not at all very experienced with FFmpeg, and I want to avoid having to extract frames from the video, remove the frames and re-encode the video.

Any suggestions or solutions?

4
  • Are you sure it's even true 30fps? US drop-frame would actually be 29.997. I'd be looking for some kind of pre-made filter "NTSC > PAL" [because the colours won't be the same either] though most 'DVD' players these days can do that on the fly
    – Tetsujin
    Commented Aug 15, 2023 at 18:06
  • The videos are truly 30fps, I checked with ffprobe and calculated with total frames / total duration in seconds.
    – azio
    Commented Aug 15, 2023 at 18:33
  • 1
    What happens if one of your pseudo-randomly-chosen frames is a keyframe?
    – Tetsujin
    Commented Aug 15, 2023 at 18:36
  • I completely misunderstood you, Ignore my other comment. There is a keyframe every 16 frames. Taking out every 6th starting from the 3rd frame shouldn't take out any I-frames. But do I have to re-encode P-frames?
    – azio
    Commented Aug 15, 2023 at 19:22

2 Answers 2

2

For removing every 6'th frame starting from the third frame, we may use:

select='mod(n+4,6)'

The frames counting starts from zero, and when mod(n+4,6) = 0, the frame is not selected.

  • mod(0+4,6) = 4 - first frame is selected.
  • mod(1+4,6) = 5 - second frame is selected.
  • mod(2+4,6) = 0 - third frame is not selected.

Using -r 25 as output argument, duplicates frames.
Instead of -r 25 as output, we may use -vsync 0 (passthrough) and setpts filter for correcting the timestamps. We may also add -r 25 as input argument for correcting the frame period:

ffmpeg -y -r 25 -vsync 0 -i test.mp4 -vf "select='mod(n+4,6)',setpts='N/25/TB'" "OUTPUT.MKV"


Testing:

Build 30fps synthetic video with running counter:

ffmpeg -y -r 30 -f lavfi -i testsrc=size=192x108:rate=1:duration=60 -vf setpts=N/TB/30 test.mp4

Removing every 6'th frame starting from the third, and converting to 25fps:

ffmpeg -y -r 25 -vsync 0 -i test.mp4 -vf "select='mod(n+4,6)',setpts='N/25/TB'" "OUTPUT.MKV"

Convert to sequence of PNG images (for testing):

ffmpeg -i OUTPUT.MKV %04d.png

Output frames:
enter image description here enter image description here enter image description here
enter image description here enter image description here enter image description here
enter image description here enter image description here enter image description here

We can see that frames 2 and 8 are missing.

We may also verify that the framerate is 25fps using MediaInfo tool:

ID                                       : 1
Format                                   : AVC
Duration                                 : 2 s 0 ms
Frame rate mode                          : Constant
Frame rate                               : 25.000 FPS
2
  • Thank you, this is exactly what I wanted, however, I have one more enquiry about this answer. The original video has a total duration of 167s and 900ms, however, the command outputs a file with a total duration of 167s and 880ms. Is it possible to retain the duration of the original file?
    – azio
    Commented Aug 15, 2023 at 22:03
  • Never mind, the 1st frame was duplicated and even though it was removed, it caused unexpected behaviour with the modulo function. Now that I know that it actually return the remainder, I can fix that by myself.
    – azio
    Commented Aug 15, 2023 at 22:40
0

I would use shuffleframes:

ffmpeg -i INPUT.mp4 -vf "**shuffleframes**=0 1 -1 2 3 4", setpts=N/(25*TB) -r 25 OUTPUT.mp4

The -1 index in the third place instructs FFmpeg to drop the third frame.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .