Skip to content

Instantly share code, notes, and snippets.

@blairanderson
Last active December 20, 2021 17:57
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save blairanderson/2fbd5ca13a536571816df2022389b8bf to your computer and use it in GitHub Desktop.
Save blairanderson/2fbd5ca13a536571816df2022389b8bf to your computer and use it in GitHub Desktop.
Optimize Videos for Web - Compress MP4 and remove Audio with FFMPEG. encodes as 264 with CRF 30, scales down to 1920x1080, strips audio
#! /bin/bash
# The Purpose of this Script is to batch convert and compress any video file to mp4 format
#
# WARNING: LOSSY COMPRESSION !!!
# Variable used:
# sourcedir is the directory where to be converted videos are. Converted video will be saved in the same folder
# usage:
#########################
# $ ls -al
# -> conv.sh
# -> /out
# -> /videos
# $ ./conv.sh videos/
#########################
# Source dir
sourcedir="$1"
if [[ $sourcedir ]]; then
echo -e "Using \033[1;34m$sourcedir\033[0m as Input Folder"
else
echo -e "\033[1;31mError: Check if you have set an input folder\033[0m"
exit
fi
################################################################
cd "$sourcedir"
for filelist in `ls`
do
if ffmpeg -i $filelist 2>&1 | grep 'Invalid data found' #check if it's video file
then
echo "ERROR File $filelist is NOT A VIDEO FILE can be converted!"
continue
fi
echo -e "ffmpeg -i $filelist -y -f mp4 -an -c:v libx264 -crf 30 -vf scale=1920:1080 -preset medium -map_metadata 0 out-"$filelist" < /dev/null"
ffmpeg -i $filelist -y -f mp4 -an -c:v libx264 -crf 30 -vf scale=1920:1080 -preset medium -map_metadata 0 "../out/$filelist" < /dev/null
done
@jacobeubanks
Copy link

Thanks for the script! It worked great for me. One small thing that caught me off-guard was:

https://gist.github.com/blairanderson/2fbd5ca13a536571816df2022389b8bf#file-conv-sh-L8

Converted videos appear to be saved in the out folder and if there is no out folder, it will produce a no file or directory error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment