Isabelle
Isabelle

Reputation: 41

ffmpeg: cutting 20 seconds of video starting from the middle time in a bash cycle

im in a bash script looping a dir full of .mp4 video files each one with a different lenght. Now: how can i cut 20 seconds starting in the exact middle of any video? For a single file i can read manually the duration of the video so i know at what second start cutting: e.g. for a 120 sec. video i use

ffmpeg -i "tmpfile.mp4" -ss 60 -t 20 -c copy "outputfile.mp4" 2>/dev/null

but now the problem is that the "-ss" value should be a variable with the (total lenght of video/2) some videos are long less than a minute, some are long many minutes, and some are more than one hour.

Thanks

Cant find a middle time value to start cutting

Upvotes: 1

Views: 121

Answers (1)

oguz ismail
oguz ismail

Reputation: 50815

Use ffprobe

len=$(ffprobe -v error -show_entries format=duration -of compact=p=0:nk=1 input.mp4)
mid=$((${len%.*} / 2))
ffmpeg -v error -ss $mid -i input.mp4 -t 20 -c copy output.mp4

Upvotes: 1

Related Questions