0

I have no history with ffmpeg, but I am assuming this would be the right tool for the job. I am trying to cut a folder of videos with different lengths. I want to cut them all to be 12 seconds from the end. That is: on a 30 second video I would be left with 00:18 - 00:30. 00:00-00:17 would be deleted.

I am on mac OS Mojave. It seems that ffmpeg is the right tool for the job to batch edit these videos. Can someone walk me through this? I have some basic understanding but will need the code/script explained so that I can apply it to my own use. Thank you very much.

1 Answers1

2

Get duration from input. Calc time position with bc utility: $(echo "$DUR" -12 | bc -l)

#!/bin/bash
for f in *.mkv; do
  DUR="$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$f")"
  ffmpeg -ss $(echo "$DUR" -12 | bc -l) -i "$f" -map 0 -c copy "${f%.*}_trim.mkv"
done

will copy from key frame, so will not equal 12 sec. for accuracy you will have to convert, replace "-c copy" with something like "-c:v h264_nvenc -cq 18 -c:a aac -q:a 4" or what you want.

#!/bin/bash
for f in *.mkv; do
  ffmpeg -sseof -12 -i "$f" -map 0 -c:v h264_nvenc -cq 18 -c:a aac -q:a 4 "${f%.*}_trim.mkv"
done

Thanks to Gyan

[add info]

Create the above script in folder with videos. Or run one string, edit for every file:

ffmpeg -sseof -12 -i "input.mp4" -map 0 -c:v libx264 -b:v 3M -c:a aac -b:a 320k "output.mkv"
  • Use sseof instead - https://stackoverflow.com/q/36120709 – Gyan Oct 23 '20 at 07:45
  • Thank you very much, so I am a little confused on what this code means.. Do I replace the "f" just with the file name or the folder location? Again I am new to this. Would help me to understand the code so that I can apply it to my situation. Thank you in advance – Jack Fitzpatrick Oct 25 '20 at 13:49
  • @JackFitzpatrick `f` is just an arbitrary name for the variable representing the files in the folder. Just navigate to the folder containing the videos (such as `cd ~/videos/`) then run example #2 (but omit `-c:v h264_nvenc -cq 18 -q:a 4`). – llogan Oct 25 '20 at 19:51
  • @llogan what do I do in the first portion of the script for `"$f"` and then also `"${f%.*}_trim.mkv"` ... Also does the `$` sign mean anything in regards to a variable or am I just typing it exactly as it appears? Thanks a lot – Jack Fitzpatrick Nov 04 '20 at 11:33
  • @JackFitzpatrick Just copy and paste the command. You don't need to change anything (except to omit those non-recommended encoding options I mentioned previously). The `$` just means it is a variable: you don't have to change that either. – llogan Nov 04 '20 at 18:46