1

I am trying to build an application that will divide an input video file (usually mp4) into chunks so that I can apply some processing to them concurrently and then merge them back into a single file.

To do this, I have outlined 4 steps:

  1. Forcing keyframes at specific intervals so to make sure that each chunk can be played on its own. For this I am using the following command:

    ffmpeg -i input.mp4 -force_key_frames "expr:gte(t,n_forced*chunk_length)" keyframed.mp4

    where chunk_length is the duration of each chunk.

  2. Dividing keyframed.mp4 into multiple chunks. Here is where I have my problem. I am using the following command:

    `ffmpeg -i keyframed.mp4 -ss 00:00:00 -t chunk_length -vcodec copy -acodec copy test1.mp4`
    

    to get the first chunk from my keyframed file but it isn't capturing the output correctly, since it appears to miss the first keyframe.

    On other chunks, the duration of the output is also sometimes slightly less than chunk_length, even though I am always using the same -t chunk_length option

  3. Processing each chunk For this task, I am using the following commands:

    ffmpeg -y -i INPUT_FILE -threads 1 -pass 1 -s 1280x720 -preset medium -vprofile baseline -c:v libx264 -level 3.0 -vf "format=yuv420p" -b:v 2000k -maxrate:v 2688k -bufsize:v 2688k -r 25 -g 25 -keyint_min 50 -x264opts "keyint=50:min-keyint=50:no-scenecut" -an -f mp4 -movflags faststart /dev/null

    ffmpeg -y -i INPUT_FILE -threads 1 -pass 2 -s 1280x720 -preset medium -vprofile baseline -c:v libx264 -level 3.0 -vf "format=yuv420p" -b:v 2000k -maxrate:v 2688k -bufsize:v 2688k -r 25 -g 25 -keyint_min 50 -x264opts "keyint=50:min-keyint=50:no-scenecut" -acodec libfaac -ac 2 -ar 48000 -ab 128k -f mp4 -movflags faststart OUTPUT_FILE.mp4

    This commands are not allowed to be modified, since my goal here is to parallelize this process.

  4. Finally, to merge the files I am using concat and a list of the outputs of the 2nd step, as follows:

    ffmpeg -f concat -i mylist.txt -c copy final.mp4

In conclusion, I am trying to find out a way to solve the problem with step 2 and also get some opinions if there is a better way to do this.

1 Answers1

0

I found a solution with the following code, which segments the file without the need to force keyframes (it cuts on the nearest keyframe) and multiple commands.

ffmpeg -i test.mp4 -f segment -segment_time chunk_length -reset_timestamps 1 -c copy test%02d.mp4