Last active
January 3, 2023 20:53
-
-
Save iwalton3/c034ec5a942466206fbee859184b625d to your computer and use it in GitHub Desktop.
Introzapper - Remove intros from videos without a full re-encode. Note by default only 24 seconds can be removed. (License: MIT License)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#[file] [-d detectOnly] | |
alias ffmpeg="ffmpeg -nostdin" | |
function shortenVideo { | |
#[file] [shorten-by] [-d do nothing] | |
if [[ "$3" == "-d" ]] | |
then | |
return | |
fi | |
temp=$RANDOM$RANDOM | |
ffmpeg -v error -y -ss 24 -i "$1" -c copy "$temp-f2.ts" | |
f2len=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$temp-f2.ts") | |
olen=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1") | |
len=$(echo "$olen - $f2len" | bc) | |
ffmpeg -v error -y -i "$1" -ss "$2" -to "$len" -c:a copy -c:v libx264 -crf 22 -preset ultrafast "$temp-f1.ts" | |
ffmpeg -v error -y -i "concat:$temp-f1.ts|$temp-f2.ts" -c copy -movflags +faststart "$temp-f3.mp4" | |
rm "$temp-f1.ts" "$temp-f2.ts" | |
nlen=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$temp-f3.mp4") | |
if [[ "$(echo "$nlen - $olen + $2 + 5" | bc | sed 's/\..*//g')" -gt "0" ]] | |
then | |
mv "$temp-f3.mp4" "$1" | |
else | |
rm "$temp-f3.mp4" | |
echo "Encode for $1 failed!" | |
return 1 | |
fi | |
} | |
function detectImage { | |
#[image-file] [template-file] | |
temp=$RANDOM$RANDOM | |
cf=$(compare -verbose -metric MSE "$1" "$2" $temp-diff.png 2>&1 | grep 'all:' | sed 's/.*: \([0-9]*\).*/\1/g') | |
rm $temp-diff.png 2>/dev/null | |
if [[ "$cf" -lt 75 && "$cf" != "" ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
temp2=$RANDOM$RANDOM | |
ffmpeg -v error -ss 2 -i "$1" -vframes 1 -q:v 2 -y ss-$temp2.jpg | |
mogrify -resize 1280x720\! ss-$temp2.jpg | |
if detectImage ss-$temp2.jpg /home/vtemplates/example1.jpg | |
then | |
echo "Detected example1 Intro" | |
shortenVideo "$1" 6.5 "$2" | |
elif detectImage ss-$temp2.jpg /home/vtemplates/example2.jpg | |
then | |
echo "Detected example2 Intro" | |
shortenVideo "$1" 7 "$2" | |
fi | |
rm ss-$temp2.jpg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment