90.5 Video Cut and Slice

20240315

To cut a segment of video to a file, starting at the 30 seconds mark and going until 40 seconds:

ffmpeg -i orig.mp4 -ss 00:00:30 -to 00:00:40 -c copy output.mp4

We can illustrate the more complex task of cutting out sections of a video and splicing them together. As a use case we want to illustrate the effect of a 8dB boost to the audio track of the video and to do this for a 10s segment of the original and boosted video.

We can first boost the audio by 8dB and save the result to a new file.

ffmpeg -i orig.mp4 -vcodec copy -af "volume=8dB" plus8db.mp4

We now have two videos and we want to cut 10 seconds of video from 30 seconds into the video, for both videos and to then splice them together. As a bonus we will add an overlay of text to be able to identify each section.

ffmpeg -i orig.mp4 -i plus8db.mp4 -filter_complex "[0:v]trim=start=30:end=40,setpts=PTS-STARTPTS[v0];[1:v]trim=start=30:end=40,setpts=PTS-STARTPTS[v1];[0:a]atrim=start=30:end=40,asetpts=PTS-STARTPTS[a0];[1:a]atrim=start=30:end=40,asetpts=PTS-STARTPTS[a1];[v0]drawtext=text='ORIGINAL':fontsize=24:fontcolor=black:x=(w-text_w)/2:y=h-th-20[with_text_0];[v1]drawtext=text='INCREASED':fontsize=24:fontcolor=black:x=(w-text_w)/2:y=h-th-20[with_text_1];[with_text_0][a0][with_text_1][a1]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4

The work is done by the complex filter.

[0:v]trim=start=30:end=40,setpts=PTS-STARTPTS[v0]; This trims the video stream from the first input file [0:v] to the 10 seconds from 30 seconds in to 40 seconds in and resets the presentation timestamps.

[1:v]trim=start=30:end=40,setpts=PTS-STARTPTS[v1]; Similarly for the second video [1:v].

[0:a]atrim=start=30:end=40,asetpts=PTS-STARTPTS[a0]; Similarly for the audio from the first video [0:a].

[1:a]atrim=start=30:end=40,asetpts=PTS-STARTPTS[a1]; Similarly for the audio from the second video [1:a]

[v0]drawtext=text='ORIGINAL':fontsize=24:fontcolor=black:x=(w-text_w)/2:y=h-th-20[with_text_0]; For the first video segment [v0] we add the text ORIGINAL with font size 24 and colour black. The x location is determined so as to centre the text and the y location will place the text 20 pixels above the bottom of the frame.

[v1]drawtext=text='INCREASED':fontsize=24:fontcolor=black:x=(w-text_w)/2:y=h-th-20[with_text_1]; Similarly for the second video segment [v1].

[with_text_0][a0][with_text_1][a1]concat=n=2:v=1:a=1[outv][outa] Here we splice then together.

The -map "[outv]" and similarly for the audio maps the output video and audio streams to be included in the final output.



Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0