Skip to content

Instantly share code, notes, and snippets.

@JaimeStill
Last active May 4, 2018 03:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaimeStill/8adb695ee46425f1ee9830357cd9c6bb to your computer and use it in GitHub Desktop.
Save JaimeStill/8adb695ee46425f1ee9830357cd9c6bb to your computer and use it in GitHub Desktop.
Globally install ffmpeg and convert video to gif

Helpful Links

ffmpeg - documentation
ffmpeg - downloads
High Quality GIF with ffmpeg

Install

  1. Download ffmpeg and unzip wherever you want it to reside.
  2. From an admin command prompt, open the environment variables editor: rundll32 sysdm.cpl,EditEnvironmentVariables
  3. Select Path from the System Variables section at the bottom, then click Edit below
  4. Add the path to the \bin folder in the extracted directory
  5. Be sure to click Ok to close out of the Path editor and the Environment Variable editor

Opening Environment Variables
environment-variables

Editing Path
editing-path

Adding Variable
add-variable

Executing Globally
execute-global

PowerShell Script

I've converted the bash script outlined in High Quality GIF with ffmpeg into a simple parameterized PowerShell script.

Create-Gif.ps1

[CmdletBinding()]
Param(
    [Parameter()]
    [string]$origin,
    [Parameter()]
    [string]$destination,
    [Parameter()]
    [string]$options = $null,
    [Parameter()]
    [string]$log = $null
)

$palette = "${env:TEMP}\palette.png"
$filters = if ($options.Length -gt 0) { $options } else { "scale=512:-1:flags=bicubic" }
$logLevel = if ($log.Length -gt 0) { $log } else { "panic" }

ffmpeg -v $logLevel -i $origin -vf "$filters,palettegen" -y $palette
ffmpeg -v $logLevel -i $origin -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $destination

Syntax

{directory-where-script-is-saved}>.\Create-Gif.ps1 {origin-video} {destination-gif} -log {optional logging level} -options {optional filter options}

Examples

# Basic Usage
D:\Desktop>.\Create-Gif.ps1 D:\Captures\demo.mp4 D:\Captures\demo.gif  

# With Options
D:\Desktop>.\Create-Gif.ps1 D:\Captures\demo.mp4 D:\Captures\demo.gif -options "trim=start=5:end=10,scale=320:-1:flags=lanczos"

# With Log
D:\Desktop>.\Create-Gif.ps1 D:\Captures\demo.mp4 D:\Captures\demo.gif -log debug

The options relate to the options set for the palettegen filter. The default is scale=720:-1:flags=lanczos, which specifies an aspect ratio-locked width of 720px and uses the lanczos scaling algorithm.

In the example above, the options limit the gif to 15 frames per second and an aspect ratio-locked width of 320px.

For detailed information on filters, see the following resources:
ffmpeg - Filters Table of Contents
ffmpeg - Video Filters
ffmpeg - FPS
ffmpeg - Scale
ffmpeg - Scale Algorithm Flags

Demonstrations

Source video that will be used to make .gif files:

NASA - Time-Lapse Assembly of NASA Supersonic Model

Default

ffmpeg-default

nasa-default

Options - Smaller Width

ffmpeg-small

nasa-small

Options - Smaller Width and Reduced FPS

ffmpeg-small-jittery

nasa-small-jittery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment