Skip to content

Instantly share code, notes, and snippets.

@royrscb
Last active April 16, 2019 23:58
Show Gist options
  • Save royrscb/ac9937e5d9172a9b01dd1be7abaab950 to your computer and use it in GitHub Desktop.
Save royrscb/ac9937e5d9172a9b01dd1be7abaab950 to your computer and use it in GitHub Desktop.
Video converter cut and accelerates
#!/bin/bash
#############################
#Author: Roy Ros Cobo #######
#############################
# VARS TO SET #####################################################
filesDir='../toConv'
# destDir is relative to filesDir
destDir='../converted'
tInicial='00:00:00'
tFinal='00:22:00'
# videoQuality (Best 1, worst 31)
videoQuality=9
# speed has to be decimal (examples: 1.4, 2.0, 1.0, 2.5)
speed=1.5
# speedInverse has to be 1/speed (maximum 3 decimals)
speedInverse=0.667
####################################################################
# EXECUTION ########################################################
echo -e "\n\t\e[1mVIDEO CONVERTER\e[0m"
echo "-----------------------------------"
echo -e "$filesDir -> $destDir\n"
echo -e "From: $tInicial to: $tFinal\n"
echo -e "Quality: $videoQuality\n"
echo -e "Speed: x$speed"
echo "-----------------------------------"
cd "$filesDir"
mkdir -p "$destDir"
shopt -s nullglob
i=0
for file in *
do
trap break SIGINT
tput civis -- invisible
SECONDS=0
#result name
name="$(echo "$file" | cut -f1 -d'[' | rev | cut -c 2- | rev)"
number=$(echo $name | head -c 3)
ext=$(echo "$file" | rev | cut -f1 -d. | rev)
if [ "$ext" != 'mp4' ] && [ "$ext" != 'avi' ]; then continue; fi
echo -ne "$number \e[31m\e[5m(tallant)\e[0m"
#first cut
ffmpeg -loglevel error \
-i "$file" \
-ss $tInicial -to $tFinal \
-q:v $videoQuality -q:a 1 \
"$destDir"/.tmp."$ext" -y && \
echo -ne "\r$number \e[31m\e[5m(accelerant)\e[0m" && \
#then accelerate
ffmpeg -loglevel error \
-i "$destDir"/.tmp."$ext" \
-filter:v "setpts=$speedInverse*PTS" -filter:a "atempo=$speed" \
-q:v $videoQuality -q:a 1 \
"$destDir"/.tmp2."$ext" -y
rm "$destDir"/.tmp."$ext" && \
mv "$destDir"/.tmp2."$ext" "$destDir"/"$name"."$ext" && \
gio trash "$file"
time_past="$(($SECONDS / 60))m $(($SECONDS % 60))s"
echo -e "\r$number \e[32mFet\e[95m ($time_past)\e[0m "
done
echo
shopt -u nullglob
tput cnorm -- normal
# -loglevel error >>> show only errors and above level of printings
# -ss 00:03:00 -t 00:15:30 >>> cut from -ss -t time of duration (-to option for cut to -to).
# -q:v 1 -q:a 1 >>> set the qualiti of v, video and a, audio. 1 best quality, 31 worst quality.
# -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" >>> set the speed of v, video and a, audio. \
# For video 0.5 is x2 and audio 2 is x2. (For same speed one has to be the inverse (1/X) of the other).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment