I have tried looking up a solution but all of them involve copying, say 10 seconds from 00:00:00 of the video instead of removing enough of the video starting from the end to leave only 10 seconds remaining. I have a bunch of videos that I needs exactly 18 seconds removed from the end but the total duration varies from videos to video. Is this not possible to automate using ffmpeg or some other program?
1 Answer
See Using ffmpeg to cut up video for answers on how to use ffmpeg
to cut videos, with the -ss
and -t
flags.
To get you video duration you can use: ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4
as seen on http://superuser.com/questions/650291/ddg#945604
So by combining both things you could do
-t $(( $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4 |cut -d\. -f1) - 18 ))
to have a duration equal to the previous one minus 18 seconds.
-
I will try this out. I didn't know ffprobe was a thing. Will ffprobe return time in HH:mm:ss.xxx format. I believe in the post you linked the answer mentions that time format is important.– IcarusDec 22, 2017 at 22:22
-
ffprobe
is very useful and can do many things. Called like written it gives the duration as 1234.56 (seconds) from which thecut
retains only the integer part in order to remove 18. If you use HH:MM:SS the computation will be more complex. But indeed, try the results of your chopped video before removing original as it could be more or less what you want depending on a lot of stuff, including the encoding and format of original file. Dec 22, 2017 at 22:33 -
ffmpeg
command accept timestamps both as HH:MM:SS.sssss format and as SSSSSS.ss format, so you are free to use the one most suited to the task at hand. And note that you probably could do the same thing, but different syntax, withmplayer
/mencoder
, among other choices. Dec 22, 2017 at 22:45 -
-
1Like the questioner, I didn't know about
ffprobe
, so thanks for that. Note that you can retain the sub-second resolution with-t $(bc <<< $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 Kon-Tiki.mp4)-18)
(or the equivalent usingcalc
). As an aside, if you don't specify output formatting then the outputs offfprobe
andffmpeg -i
are virtually identical, with both outputs onstderr
.– AFHDec 23, 2017 at 14:01
ffmpeg -i VideoFile 2>&1|grep Duration
, will give the length of the video, and you can do some arithmetic on the result to subtract 18 seconds to work out the length you need (a lot easier inbash
thancmd
!).for
andwhile
are the basic loop commands, with variantsselect
anduntil
.ffprobe
is better for getting duration thanffmpeg
, because the output fromffmpeg
was not designed or intended for machine parsing.