Skip to main content

5. Implementing a video resizing solution yourself

Prerequisites

Before reading this page, read the previous page on implementing image resizing solutions yourself (as much of the same process is required).

This page assumes you already know the steps involved in that, and focuses on specific differences you need to account for when resizing videos instead of images.

Video encoding and resizing

The main difference, predictably, is in actually resizing and re-encoding the videos themselves. The main differences you need to consider are encoding time, the library to use, video formats, and an understanding of video encoding and parameters.

Video encoding libraries

I highly recommend using FFmpeg for resizing and re-encoding videos. CompilingYou can either compile FFmpeg yourself is possible but not advised for thisor use caseJohn -Van it'Sickle's awkwardpre-built tobinaries make it self-contained and run properly in an isolated environment like this.here.

Notes on compilingCompiling FFmpeg on AWS Lightsail servers

First of all, to compile FFmpeg from scratch isn't simple in the slightest - I highly recommend you use https://github.com/zimbatm/ffmpeg-static to do it, as it removes many of the stumbling blocks you will otherwise encounter and makes the process much, much easier. 

You can use the normal LAMP offering in Lightsail. The Bitnami LAMP stack that Lightsail uses runs on Debian, so it's compatible with the guide linkedscripts above.

You will need to use at least the $10 server tier - a $3.5 server will not suffice here. Lightsail servers use a burstable CPU system; they accrue CPU credits over time they can use to run at higher than normal capacity, but when operating above that capacity, they consume the credits. The baseline "performance" counted as capacity changes on which price of server you chose; the $3.5 server considers 5% CPU usage the baseline for instance, so doing anything heavy will eat your CPU credits like crazy (and you can't accumulate credits forever, they have a limit for each server).

When you start up a server, there's a small grace period where you get some extra credits that aren't displayed in the metrics screens, to stop smaller servers becoming sluggish when e.g. installation scripts are run on them. Compiling some of the FFmpeg librariestakes isat least several minutes, so you're going to exceed this, so we need to use a server with ahalf decent baseline.server for this.

You can see in the article from Amazon here exactly how this works. Here's a screenshot of the baselines of each server type from the article:

image.png

The $20/month Linux servers don't have any benefit in this case over the $10 one, and the $40 one is likely to be overkill, so I recommend using the $10 option for this purpose. You won't actually be billed much, as you don't need to keep this server around after you finish compiling and whatnot; Lightsail charges in hourly increments, so probably a few cents. If you have trouble with your server getting throttled due to a lack of CPU credits, try again from scratch with a higher price instance.

Notes on the ffmpeg-static GitHub repository

Having set up your Lightsail server, copy the files from that repository into a new folder. Make sure to change the permissions on the items within, so the scripts are executable, and then run the build-ubuntu script inside it.

chmod 755 yourfolder/
cd yourfolder
./build-ubuntu.sh

If something goes wrong, it usually happens fairly fast. Otherwise, let it run - go get some coffee or something - and when it's finished the "bin" directory inside your folder will contain the compiled executables. You only need ffmpeg for this use case, the other executables are not necessary here.

Download the ffmpeg executable you compiled, put it in a folder named "bin" on its own, and zip that folder, so you have a zip file containing a "bin" folder, and the ffmpeg executable inside that folder. You can then upload this to Lambda as a layer, for processing videos.