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?
Asked
Active
Viewed 8,766 times
9
1 Answers
12
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.
Patrick Mevzek
- 1,622
- 4
- 19
- 22
-
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. – Icarus Dec 22 '17 at 22:22
-
ffprobeis very useful and can do many things. Called like written it gives the duration as 1234.56 (seconds) from which thecutretains 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. – Patrick Mevzek Dec 22 '17 at 22:33 -
ffmpegcommand 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. – Patrick Mevzek Dec 22 '17 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 offfprobeandffmpeg -iare virtually identical, with both outputs onstderr. – AFH Dec 23 '17 at 14:01 -
@Icarus If you want HH:MM:SS output add the
-sexagesimaloption as shown in FFmpeg Wiki: FFprobe Tips. – llogan Dec 23 '17 at 19:00 -
Just tried this out and it worked like a charm except the following; I am not sure if this is a windows thing but with
|cut -d\. -f1ffprobe returned/usr/bin/cut: .: Is a directoryinstead of total duration. When using that in conjunction with ffmpeg, the output video was blank. – Icarus Dec 28 '17 at 13:40 -
-
I just get "'cut' is not recognized as an internal or external command..." – Moss Jun 13 '18 at 00:14
-
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 inbashthancmd!). – AFH Dec 22 '17 at 22:22forandwhileare the basic loop commands, with variantsselectanduntil. – AFH Dec 23 '17 at 12:57ffprobeis better for getting duration thanffmpeg, because the output fromffmpegwas not designed or intended for machine parsing. – llogan Dec 23 '17 at 19:00