(FFmpeg) How to Loop a Section of Video Multiple Times?

Looping a section of a video can be useful for presentations or advanced video editing. In this example the input.mp4 is a 5 minute long video with the requirement that after the 1 minute mark, the video will loop 10 times.

  $ ffmpeg -i input.mp4 -filter_complex "loop=loop=10:size=1500:start=1500" -pix_fmt yuv420p output.mp4  

In the requirements for this example, it was stated after the 1 minute mark the video will loop yet the start number is 1500. This is because start requires a frame value not time.  To calculate the frame value one must know the framerate of the input. In this example, input.mp4 has a 30 frames per second framerate (FPS). 30 FPS * 1 minute (60 seconds) = 1500 frames.

In addition to the start parameter, a size parameter must be set as well. size is the number of frames that are repeated. So from frames 0 to 1500 the loop will play 10 times after the 1500th frame has been played.

	Indicates the name of the loop filter

	Indicates the number of loops (default is 0 with -1 being infinite)

	Indicates the number of frames (from 0 to value) that are repeated

	Indicates first or starting frame of the loop (default is 0)

The loop filter works great with video but does not apply to audio output. Thus another method is required. Using concat multiple times on the same file could be used instead:

 $ ffmpeg -i input.mp4 -i input.mp4 -i input.mp4 -filter_complex "concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

Then with the use of seeking, -ss, the section to be repeated can be specified. 

Challenge: Try looping a specific section of a video using concat.

Below is the command to identify the frame rate of an input file, this one has a frame rate of 29.97:

$ ffmpeg -i input.mp4 
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  Configuration: <removed for clarity>
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:20.05, start: 0.000000, bitrate: 1802 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x480 [SAR 40:33 DAR 20:11], 1667 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
      handler_name    : Core Media Video
      timecode        : 01:00:00:00
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
      handler_name    : Core Media Audio
    Stream #0:2(eng): Data: none (tmcd / 0x64636D74), 0 kb/s
      handler_name    : Core Media Video
      timecode        : 01:00:00:00

Related Posts