videoffmpegvideo-editing

Segmenting video with ffmpeg not producing durations specified with -segment_time


I have an .mp4 file that I'm trying to segment into equal duration clips of 5s each, but I cannot exactly achieve this. The input file was encoded to force key frames every 5s as follws:

ffmpeg -i master.mp4 -force_key_frames expr:gte(t,n_forced*5) master_2.mp4

The first command I tried to used to achieve this is as follows, but the output files were all 8.91s in duration:

ffmpeg -i master_2.mp4 -f segment -segment_time 5 -reset_timestamps 1 -output_%03d.mp4

After some research, I modified to the following, which produced better results - 4.91s each:

ffmpeg -i master_2.mp4 -f segment -segment_time 5 -reset_timestamps 1 -break_non_keyframes 1 output_%03d.mp4

What parameters should be used to achieve 5.0s clips?

The ffprobe of the input file (master_2.mp4) is as follows:

  built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      56. 66.100 / 56. 66.100
  libavcodec     58.126.100 / 58.126.100
  libavformat    58. 68.100 / 58. 68.100
  libavdevice    58. 12.100 / 58. 12.100
  libavfilter     7.107.100 /  7.107.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'master_2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.68.100
  Duration: 00:04:58.90, start: 0.000000, bitrate: 4754 kb/s
  Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 4617 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : WowzaStreamingEngine
      vendor_id       : [0][0][0][0]
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : WowzaStreamingEngine
      vendor_id       : [0][0][0][0]

Follows is a snippet of the key frame timings within this file:

5.005000
10.010000
15.015000
20.020000
25.025000
30.030000
35.001633
40.006633
45.011633
50.016633
55.021633
60.026633
....

Thanks in advance for any help offered!


Solution

  • So I decided to run a test conversion for you in that program I mentioned in my comment above and then review the log to see if it spit out the ffmpeg commands used to do the equal-length chunking. I've got something which I've edited to more closely align with your nearly-working sample command.

    Try this and see what happens: ffmpeg.exe -i "master_2.mp4" -reset_timestamps 1 -sc_threshold 0 -g 5 -force_key_frames "expr:gte(t, n_forced * 5)" -segment_time 5 -f segment output_%03d.mp4