Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oskar456
Created July 2, 2020 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskar456/06543c231a5e29da8239f6215a3b8e51 to your computer and use it in GitHub Desktop.
Save oskar456/06543c231a5e29da8239f6215a3b8e51 to your computer and use it in GitHub Desktop.
Cut a video out of a long recording with fade in and out and volume normalization
#!/bin/bash
INFILE="input_video.mp4"
FPS=30
get_seconds() {
local seconds=0
IFS=":" read -ra T <<< "$1"
for x in "${T[@]}"; do
seconds=$(echo "$seconds * 60 + $x" | bc)
done
echo $seconds
}
if [[ "$#" -ne 3 ]]; then
echo "Usage: $0 <start time> <stop time> <out file name>"
exit 1
fi
start="$1"
stop="$2"
outfname="$3"
framestart="$( echo "$(get_seconds $start) * $FPS / 1" | bc)"
framestop="$( echo "($(get_seconds $stop) - 1) * $FPS / 1" | bc)"
ffmpeg -i "$INFILE" -ss $start -to $stop \
-vf 'fade=in:'${framestart}:$FPS', fade=out:'${framestop}:$FPS \
-af 'afade=in:st='$(get_seconds $start)':d=1, afade=out:st='$( echo "$(get_seconds $stop) - 1" | bc)':d=1, loudnorm=dual_mono=true:print_format=summary:I=-18' \
-c:v libx264 -ar 48000 -c:a aac \
${outfname}.mkv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment