Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut video with ffmpeg.

Tags:

video

ffmpeg

cut

This is my idea.

I want to cut the first 10s of video and the last 10s of video

Thank for help

enter image description here

like image 862
rinofcan Avatar asked Apr 20 '17 09:04

rinofcan


2 Answers

With re-encoding:

ffmpeg -ss 10 -i video.mp4 -filter_complex "[0]trim=10,setpts=PTS-STARTPTS[b];[b][0]overlay=shortest=1" -shortest -c:a copy out.mp4

-ss 10 sets the the amount to cut from beginning. trim=10 sets amount to cut from end. Caveat here is that due to a current bug with shortest=1, this may not work on ffmpeg builds from 2017.


A bit of a hack method, which skips transcoding:

ffmpeg -ss 10 -i video.mp4 -ss 20 -i video.mp4 -c copy -map 1:0 -map 0 -shortest -f nut - | ffmpeg -f nut -i - -map 0 -map -0:0 -c copy out.mp4

Depending on the location keyframes, the trims at start and end won't be perfect. First ss is starting trim. Second ss is starting + ending trim

like image 130
Gyan Avatar answered Oct 27 '22 13:10

Gyan


this is a bit nicer.

ffmpeg -i "<FILE>" -ss 00:00:10 -to  $( echo "$(ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1 "<FILE>") - 35"  | bc) -c copy  "trimmed-output.mp4"

this uses ffprobe to get the duration: ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1 "<FILE>"

and then pipes <duration> - 10 to bc to subtract 10 from duration, which is then used in -to param on ffmpeg.

like image 22
Chris Avatar answered Oct 27 '22 11:10

Chris