Skip to content

Instantly share code, notes, and snippets.

@Still34
Created December 16, 2019 10:11
Show Gist options
  • Save Still34/c395333f0371ee63f275163a49eda8a6 to your computer and use it in GitHub Desktop.
Save Still34/c395333f0371ee63f275163a49eda8a6 to your computer and use it in GitHub Desktop.
Short PowerShell script to generate GIF from video
function ConvertTo-Gif {
[CmdletBinding()]
param (
[ValidateScript( { test-path -LiteralPath $_ })]
[Parameter(Mandatory = $true)]
$File
)
begin {
# Check if ffmpeg is installed
$ffmpeg = Get-Command "ffmpeg.exe" -ErrorAction SilentlyContinue
if ($null -eq $ffmpeg) {
throw [System.IO.FileNotFoundException]::new("ffmpeg cannot be found. Please install ffmpeg then run this script again.")
}
$fileToBeProcessed = Get-Item -LiteralPath $File
}
process {
$duration = ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 `"$fileToBeProcessed`"
$rngSeek = get-random -Minimum 0.4 -Maximum 0.8
$targetPoint = [math]::floor([decimal]::Multiply($duration, $rngSeek))
$targetDuration = 5
$targetFilename = "$($fileToBeProcessed.basename)-$targetPoint-$targetDuration.gif"
ffmpeg -ss $targetPoint -t $targetDuration -i `"$fileToBeProcessed`" -vf "fps=18,scale=600:-1,split [o1] [o2];[o1] palettegen=stats_mode=diff [p]; [o2] fifo [o3];[o3] [p] paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" `"$targetFilename`" -y
}
end {
# Start-Process $targetFilename
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment