commit 76d5925c7428320e0815535ffd447abfe1d3615e Author: Evgenij Titarenko Date: Wed Jul 5 23:28:10 2023 +0300 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/video2gif.sh b/video2gif.sh new file mode 100755 index 0000000..036141c --- /dev/null +++ b/video2gif.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +#video2gif.sh +#PARAMS: +# - filename [REQUIRED] +# - ffmpeg params [OPTIONAL] + +# BASED on: +# https://askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file +# https://stackoverflow.com/questions/32484504/using-random-to-generate-a-random-string-in-bash +# https://stackoverflow.com/questions/53566442/ffmpeg-gif-with-transparency-from-png-image-sequence + +filename=$1 +#keep only the ffmpeg params +shift +framerate=$(ffmpeg -i $filename 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p") # stolen from: https://askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file + +if [ -n "$filename" -a -f $filename ] && [ -n "$framerate" ] +then + +# temporary directory for garbage generated in the process +alphanum="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" #fuсkoff +RANDOM=$$ +tmp_dir="" +for i in {1..8} ; do + tmp_dir="$tmp_dir${alphanum:RANDOM%${#alphanum}:1}" +done +mkdir $tmp_dir + +# here we go +options=$* +# video to frames and maybe some processing +ffmpeg -i $filename $options $tmp_dir/out%d.png +# generate pallete with transparency support +ffmpeg -i $tmp_dir/out%d.png -vf palettegen=reserve_transparent=1 $tmp_dir/palette.png +# combine all frames into gif +ffmpeg -framerate $framerate -i $tmp_dir/out%d.png -i $tmp_dir/palette.png -lavfi paletteuse=alpha_threshold=128 -gifflags -offsetting "${filename%.*}_out.gif" +# i guess that's it +rm -rf $tmp_dir +else +echo "Invalid file" +fi +# 🍆 i wanna fuck