1

I am working on a hobby project using Pi 3. I have bunch of videos (approx 4-5 min long) recorded from Pi camera. Now, I want to cut each video (6-7 seconds) and then want to stitch them back to back. How to do this ?

3

concat demuxer

You can use the concat demuxer to do this without needing to re-encode.

First make a text file:

file '/path/to/video_01.mp4'
inpoint 30
outpoint 35
file '/path/to/video_02.mp4'
inpoint 2
outpoint 6
file '/path/to/video_03.mp4'
inpoint 128
outpoint 142
file '/path/to/video_04.mp4'
inpoint 48
outpoint 51
file '/path/to/video_05.mp4'
inpoint 180
outpoint 200
file '/path/to/video_06.mp4'
inpoint 0
outpoint 8

Then run ffmpeg:

ffmpeg -f concat -i input.txt -c copy output.mp4
  • Note that all segments must be the same format, width x height, frame rate, etc. If they differ see the next method below.

  • The in and out points may not be accurate unless your inputs are intra-frame codecs (if you are unsure they probably are not). See the inpoint and output documentation for details. If frame accuracy is required see the method below.

concat filter

If you want to perform filtering, such as scaling to conform some segments so they are all the same, then use the trim, setpts, and concat filters:

ffmpeg -i input0 -i input1 -i input2 -i input3 -i input4 -i input5 \
-filter_complex \
"[0:v]trim=3:9,scale=1280:720,setpts=PTS-STARTPTS[v0]; \
 [1:v]trim=30:36,setpts=PTS-STARTPTS[v1]; \
 [2:v]trim=5:12,setpts=PTS-STARTPTS[v2]; \
 [3:v]trim=46:56,scale=1280:720,setpts=PTS-STARTPTS[v3]; \
 [4:v]trim=2:19,setpts=PTS-STARTPTS[v4]; \
 [5:v]trim=128:166,setpts=PTS-STARTPTS[v5]; \
 [v0][v1][v2][v3][v4][v5]concat=n=6:v=1:a=0[v]"
-map "[v]" -c:v h264_omx output.mp4
  • Filtering will require re-encoding. This example uses the encoder h264_omx which utilizes OpenMAX IL hardware to encode. If your ffmpeg does not support this, and you want to use it, then see FFmpeg hardware acceleration on Raspberry PI.

  • You didn't mention audio, so I assumed there was none. If you need it add the atrim filter, adjust the concat a value, and add the proper -map.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.