Skip to content

Instantly share code, notes, and snippets.

@wolfy-j
Last active May 9, 2018 17:09
Show Gist options
  • Save wolfy-j/d4ece481eb8c9bd8a438967d77603ce7 to your computer and use it in GitHub Desktop.
Save wolfy-j/d4ece481eb8c9bd8a438967d77603ce7 to your computer and use it in GitHub Desktop.
Converts video file to GIF.
--@Description: Converts video file to GIF.
--@Version: 1.2 <May 9, 2018>
--@Source: https://gist.github.com/wolfy-j/d4ece481eb8c9bd8a438967d77603ce7
--@Author: Wolfy-J <wolfy.jd@gmail.com>
--@From: https://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
-- Input and output files
local input = ask("Input File", "exists")
local output = ask("Output File", "!empty")
-- Initial validations
if input == "" or output == "" then
print("\n<red>Script error: </reset><red+hb>not enough arguments</reset>\n")
return
end
-- Contains file information (see ffmpeg/probe.go for more details)
local info = require("ffmpeg").probe(input)
local defaultWidth = "320"
for k, stream in pairs(info.streams) do
if stream.codec_type == "video" then
defaultWidth = stream.width
end
end
-- Durations
local tStart = ask("Start Timestamp", null, "0")
local tEnd = ask("Duration", null, "10")
-- Image properties
local width = ask("Image Width", null, defaultWidth)
local fps = ask("FPS", null, "15")
local maxColors = ask("Max Colors", null, "128")
-- Conversion
local palette = require("tmp").file("png")
print("\nGenerating the palette...\n")
require("ffmpeg").run({
"-ss", tStart,
"-t", tEnd,
"-i", input,
"-vf", "fps=" .. fps .. ",scale=" .. width .. ":-1:flags=lanczos,palettegen=max_colors=" .. maxColors,
"-y", palette
}, "spinner")
print("<green+hb>Complete.</reset>\n\n")
print("Generating GIF...\n")
require("ffmpeg").run({
"-i", palette,
"-ss", tStart,
"-t", tEnd,
"-i", input,
"-filter_complex", "[1:v]fps=" .. fps .. ",scale=" .. width .. ":-1:flags=lanczos[x];[x][0:v]paletteuse",
"-y", output
}, "spinner")
print("<green+hb>GIF has been generated!</reset>\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment