Skip to content

Instantly share code, notes, and snippets.

@alfg

alfg/README.md Secret

Last active May 28, 2020 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfg/1fb80cd89c70602c0291e5743c5df9cd to your computer and use it in GitHub Desktop.
Save alfg/1fb80cd89c70602c0291e5743c5df9cd to your computer and use it in GitHub Desktop.
FFmpeg for Instagram

FFmpeg for Instagram

A guide and collection of FFmpeg scripts for encoding video for Instagram.

Why?

Uploading high quality video to Instagram will result in low quality video on posts and stories due to aggressive re-encoding to save on bandwidth and storage for users. You will notice this especially if uploading videos from GoPro or other devices captured in 4K or 1440p with larger file sizes. Although the video preview on Instagram looks high quality, the resulting output will be degraded drastically.

This guide describes various FFmpeg encoding presets that can be used to help improve your video quality output when uploading to Instagram. These settings can also be applied to other encoders, such as Handbrake.

Recommended Encoding Settings

Instagram does not provide an official list of encoding requirements. These settings are gathered from various sources and through trial and error. However, Instagram does provide some encoding requirements for IGTV.

Set your encoder to use these settings:

  • MP4 Container format
  • H.264 Video Codec
  • AAC Audio
  • 3500kbps bitrate
  • 30 FPS
  • 60 seconds maximum in length
  • 1080p 16:9 max (vertical or horizontal)

Also, if you inspect a story video using a tool like mediainfo, you'll see something like the following:

First video stream:
1 367 kb/s 640*1136 (0.563) at 30.000 FPS, AVC (Main@L3)(CABAC 4 Ref Frames)
64.0 kb/s 44.1 kHz, 1 channel, AAC LC

Stories without audio, don't have an audio stream within the container at all.

Scripts

Example FFmpeg using the requirements above:

720p30 @ CRF 23

Using CRF only without targetting a bitrate.

ffmpeg -i INPUT.MOV -vf scale=-2:720 -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72 -c:a aac -preset slow -crf 23 -r 30 -sn -f mp4 OUTPUT.mp4

720p30 @ 3500k video bitrate + 256k audio bitrate

Variable bitrate with a max of 3500k.

ffmpeg -i INPUT.MOV -vf scale=-2:720 -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72:ref=4 -c:a aac -crf 23 -maxrate 3500k -bufsize 3500k -r 30 -ar 44100 -b:a 256k -sn -f mp4 OUTPUT.mp4

720p30 @ 3500k video bitrate + 256k audio bitrate - 2-pass encoding

Using 2-pass encoding with a targetted bitrate of 3500k. This doesn't necessarily improve quality, but helps target a filesize more accurately.

ffmpeg -i INPUT.MOV -vf scale=-2:720 -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72:ref=4 -c:a aac -b:v 3500k -maxrate 3500k -bufsize 3500k -r 30 -ar 44100 -b:a 256k -pass 1 -sn -f mp4 NUL && \ 
 ffmpeg -i INPUT.MOV -vf scale=-2:720 -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72:ref=4 -c:a aac -b:v 3500k -maxrate 3500k -bufsize 3500k -r 30 -ar 44100 -b:a 256k -pass 2 OUTPUT.mp4

Try adjusting a few settings to get the optimal quality and filesize before uploading. Maybe try 1080p for IGTV, per their specification.

What do those options mean?

  • -i INPUT.MOV - Input movie file.
  • -vf scale=-2:720 - Video filter to scale to 720p while keeping aspect ratio.
  • -c:v libx264 - Use x264 video codec.
  • -profile:v main -level:v 3.1 - Mostly for H.264 compatibility for older devices. This seems to be the profile Apple devices export to.
  • -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72:ref=4 - Some x264 codec options I use to improve video for streaming.
  • -c:a aac - Use AAC audio codec.
  • -preset slow - Slower presets will result in better quality. Feel free to also try slower and veryslow.
  • -crf 23 - Constant Rate Factor
  • -b:v 3500k - Targets video bitrate to 3500k.
  • -b:a 256k - Targets audio bitrate to 256k.
  • -ar 44100 - Sets audio sample rate to 44.1 kHz.
  • -r 30 - Sets the framerate to 30.
  • -f mp4 - Sets the MP4 container format. This is optional if you have .mp4 in the output filename.
  • OUTPUT.mp4 - The encoded output movie file.

Uploading via Web

By default, you can't upload from web, but a simple user agent trick will unveil the upload options for web (meant for mobile web). In Firefox or Chrome, just open developer tools (F12 or CTRL+Shift+I), then go to Inspector and enable the Responsive Design Mode by clicking the mobile icon. Once enabled, you can select any mobile user-agent profile, reload the page and you'll see the [+] upload option on the bottom toolbar to upload your image or video directly! Convenient, instead of transferring the files to the device first.

Resources

License

CC0

To the extent possible under law, alfg has waived all copyright and related or neighboring rights to this work.

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