8

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?

Icarus
  • 117
  • 3
  • 10
  • The command `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 in `bash` than `cmd`!). – AFH Dec 22 '17 at 22:22
  • I will try this out as well. Thanks! Does bash allow looping? – Icarus Dec 22 '17 at 22:30
  • Yes, of course: `for` and `while` are the basic loop commands, with variants `select` and `until`. – AFH Dec 23 '17 at 12:57
  • @AFH Using `ffprobe` is better for getting duration than `ffmpeg`, because the output from `ffmpeg` was not designed or intended for machine parsing. – llogan Dec 23 '17 at 19:00

1 Answers1

11

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,588
  • 4
  • 17
  • 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
  • `ffprobe` is very useful and can do many things. Called like written it gives the duration as 1234.56 (seconds) from which the`cut` 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. – Patrick Mevzek Dec 22 '17 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, with `mplayer`/`mencoder`, among other choices. – Patrick Mevzek Dec 22 '17 at 22:45
  • I will give this a shot and report back. Thanks! – Icarus Dec 22 '17 at 22:47
  • 1
    Like 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 using `calc`). As an aside, if you don't specify output formatting then the outputs of `ffprobe` and `ffmpeg -i` are virtually identical, with both outputs on `stderr`. – AFH Dec 23 '17 at 14:01
  • @Icarus If you want HH:MM:SS output add the `-sexagesimal` option as shown in [FFmpeg Wiki: FFprobe Tips](https://trac.ffmpeg.org/wiki/FFprobeTips#Duration). – 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\. -f1` ffprobe returned `/usr/bin/cut: .: Is a directory` instead of total duration. When using that in conjunction with ffmpeg, the output video was blank. – Icarus Dec 28 '17 at 13:40
  • You may need to escape twice: `cut -d\\. -f1` – Patrick Mevzek Dec 28 '17 at 14:22
  • I just get "'cut' is not recognized as an internal or external command..." – Moss Jun 13 '18 at 00:14