Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NetOpWibby/cd52e6b58de6410cc9f62f18dbe3b006 to your computer and use it in GitHub Desktop.
Save NetOpWibby/cd52e6b58de6410cc9f62f18dbe3b006 to your computer and use it in GitHub Desktop.

Pixel Perfect GIF to MP4 Conversion

problem

Sharing pixel art as animated gifs on social media sucks. Uploading animated gifs will typically get automatically converted to a video format with blurry results. We can manually do the conversion ourselves to get much nicer results.

solution

ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf scale=1920:1080:flags=neighbor output.mp4

ffmpeg

ffmpeg is the command line tool we are using to do the conversion. It can be downloaded from https://www.ffmpeg.org or acquired from various package managers (apt, brew, winget, etc). To perform the conversion we need to provide ffmpeg several command line parameters.

parameters

  • -i input.gif Specifies which file to use as input. Here input.gif is the file we wish to convert.
  • -movflags faststart Enables fast start for online playback.
  • -pix_fmt yuv420p Specifies output pixel format. The yuv420p format is widely compatible with most media players.
  • -vf scale=1920:1080:flags=neighbor Applies a filter to video. Here the filter is scale=1920:1080:flags=neighbor. Which scales the output resolution to 1920x1080. The magic bit is flags=neighbor which indicates to use nearest neighbor scaling. This is exactly what we want for pixel perfect upscaling.
  • output.mp4 Specifies name of output file. Here output.mp4 is the file we want to get.

example

blurry.mp4

Blurry upscaling

pixel_perfect.mp4

Pixel perfect upscaling

one last thing...

ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf scale=iw*3:ih*3:flags=neighbor output.mp4

You can use a little bit of math inside the filter to calculate the output resolution for you. Here scale=iw*3:ih*3:flags=neighbor will scale the original image up by 3x. Where iw is the input image width and ih is the input image height.

The scale filter has quite a few options. You can read about them at FFmpeg Filters Documentation.

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