Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Animated GIF conversion PowerShell function
function ConvertTo-AnimatedGif {
param (
[string]$VideoFilePath = $(Read-Host -Prompt 'Enter a video file path.'),
[int]$FramePerSeconds = 10,
[int]$Scale = 320,
[switch]$Verbose
)
$ffmpegCommand = (Get-Command -ErrorAction Ignore 'ffmpeg.exe')
if ($ffmpegCommand -eq $null) {
throw 'Cannot find ffmpeg.exe in current Path configuration.'
}
$inputFile = (Resolve-Path -ErrorAction Ignore "$VideoFilePath")
if ($inputFile -eq $null) {
throw "Cannot find the input file $VideoFilePath."
}
$outputFileDirectory = Split-Path -Path $inputFile.Path -Parent
$outputFilePath = Join-Path -Path $outputFileDirectory -ChildPath "output-$([DateTime]::Now.ToString("yyyy-MM-dd-HH-mm-ss")).gif"
if ($Verbose -eq $True) {
. $ffmpegCommand.Source -i $inputFile.Path -vf "fps=$($FramePerSeconds),scale=$($Scale):-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 $outputFilePath
} else {
. $ffmpegCommand.Source -i $inputFile.Path -vf "fps=$($FramePerSeconds),scale=$($Scale):-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 $outputFilePath 2>&1 | Out-Null
}
$outputFile = (Resolve-Path -ErrorAction Ignore "$outputFilePath")
if ($outputFile -eq $null) {
throw "Cannot find the generated output file $outputFile."
}
$outputFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment