Skip to content

Instantly share code, notes, and snippets.

@JarLowrey
Last active March 29, 2017 02:27
Show Gist options
  • Save JarLowrey/45a663dd98ca53c5b40552fade0d704c to your computer and use it in GitHub Desktop.
Save JarLowrey/45a663dd98ca53c5b40552fade0d704c to your computer and use it in GitHub Desktop.
convert most video files around, supports "gif". Can customize ffmpeg options. Use via "sh vid_convert.sh filename outputExtension"
#!/bin/sh
echo $0
#with help from QA - http://apple.stackexchange.com/questions/103473/convert-animated-gif-to-mp4
#https://ffmpeg.org/ffmpeg.html
#required dependencies
#sudo apt-get install imagemagick
#sudo apt-get install ffmpeg
#sudo apt install libimage-exiftool-perl
#use sh vid_convert.sh filename outputExtension
#Ex: sh vid_convert.sh birds.gif mp4
#Ex: sh vid_convert.sh birds.flv mp4
vidConversion () {
if [ "$2" = "webm" ]; then
ffmpeg -i $1 -vcodec libvpx -acodec libvorbis $3.$2
else
ffmpeg -i $1 -c:v libx264 -crf 19 -strict experimental $3.$2
fi;
}
gifToMp4(){
FRAME_TMP_FOLDER="____tmp_frames"
mkdir -p $FRAME_TMP_FOLDER
#create the gif frames
convert -coalesce $1 $FRAME_TMP_FOLDER/frames%04d.png
#determine the frame rate of output
GIF_DURATION=$(exiftool -Duration $1 | sed 's/[^0-9.]*//g')
NUM_FRAMES=$(cd $FRAME_TMP_FOLDER && ls -f | wc -l)
DESIRED_FPS=$(echo "$NUM_FRAMES / $GIF_DURATION"|bc)
ffmpeg -r $DESIRED_FPS -i $FRAME_TMP_FOLDER/frames%04d.png -vcodec h264 -y -pix_fmt yuv420p $3.$2
rm -rf $FRAME_TMP_FOLDER
}
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
if [ "$extension" = "gif" ]; then
gifToMp4 $1 $2 $filename
else
vidConversion $1 $2 $filename
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment