Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created March 16, 2021 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonDanisch/ef25459b049a2a209304b6bdf529144f to your computer and use it in GitHub Desktop.
Save SimonDanisch/ef25459b049a2a209304b6bdf529144f to your computer and use it in GitHub Desktop.
using FFMPEG
function save(src::String, out::String;
framerate::Int = 24, compression = 20)
typ = splitext(out)[2]
mktempdir() do dir
if typ == ".mp4"
ffmpeg_exe(`-i $(src) -crf $compression -c:v libx264 -preset slow -r $framerate -pix_fmt yuv420p -c:a libvo_aacenc -b:a 128k -y $out`)
elseif typ == ".webm"
ffmpeg_exe(`-i $(src) -crf $compression -c:v libvpx-vp9 -threads 16 -b:v 2000k -c:a libvorbis -threads 16 -r $framerate -vf scale=iw:ih -y $out`)
elseif typ == ".gif"
filters = "fps=$framerate,scale=iw:ih:flags=lanczos"
palette_path = dirname(src)
pname = joinpath(palette_path, "palette.bmp")
isfile(pname) && rm(pname, force = true)
ffmpeg_exe(`-i $(src) -vf "$filters,palettegen" -y $pname`)
ffmpeg_exe(`-i $(src) -c:a aac -i $pname -lavfi "$filters [x]; [x][1:v] paletteuse" -y $out`)
rm(pname, force = true)
else
error("Video type $typ not known")
end
end
return out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment