Bandwidth costs money, and streaming video from CloudFront is very expensive. You can reduce that cost by pre-encoding your videos with AWS Elastic Transcoder, which processes videos to reduce file size.
Bandwidth costs money
Video files are quite large as fair as the media goes, compared to pictures and audio. AWS charges you for storage space and bandwidth used. If you have a 100 MB video file that is viewed 1,000 times, it is almost 100 GB of bandwidth, or $ 8.50 in bandwidth costs to use CloudFront to serve the content. If your application hosts video, it can be a big cost factor.
Fortunately, the video does not have to be that big. Through a process called transcoding, you change the bit rate of the video. Each video is encoded at a certain data rate; for example, a recording can be encoded at 10Mb / s.
Take a look at the zoomed frame from some test materials of a jellyfish. The one on the left is coded with a reasonable 3Mb / s, and the one on the right is coded with a very high 100Mb / s. Can you see the difference?
It’s a bit distorted, and low bit rate video can definitely be blurry with a lot of motion, but for the most part, the 3Mb / s video looks perfectly acceptable.
However, you will notice the difference in size – the lower bit rate file takes up 11 MB for 30 seconds in length, and the higher bit rate file takes up 358 MB. Obviously, you would never use such a large file in production, but if you do not clean up the user input, you may encounter this. Even a small reduction in file size can lead to a lot of costs saved on files that are downloaded quite often. For big companies like Netflix, transcoding media is a huge business.
Transcoding is also used to generate videos of different sizes for different devices. Small mobile devices that watch a video in portrait mode probably do not need a 4K video, or even a 1080p. Users with slow connections may only be able to stream a 480p video. AWS Elastic Transcoder can generate multiple video files for each input.
This is exactly what YouTube does when you upload a video – your video is processed and encoded for delivery across multiple platforms (and if you are particularly early on a YouTube video, you may notice that the quality does not exceed 480p, as the 1080p transcode is not finished yet).
How to use AWS Elastic Transcoder
Switch to the Elastic Transcoder Console. You want to create a new pipeline, a queue that handles the transcoding jobs. Pipelines use S3 buckets for input and output, so you want to create two new buckets from the S3 Management Console.
Give your pipeline a name and then select your input bucket:
You want to make sure that your bucket and pipeline are in the same AWS region, or that you will be charged for processed data and the transcoding will be slower. Select the bucket and a bucket to be used for thumbnails. (This can be an outgoing bucket.)
Create your bucket and note Pipeline ID:
Using Elastic Transcoder is actually a strange manual process. You must create a new job from the console for each file and queue with the appropriate settings. Fortunately, you can automate the entire process with a Lambda feature that runs when a new video file is uploaded to S3.
Switch to the Lambda console and create a new function. Select NodeJS 10 as the runtime and paste it into this script, courtesy of Swapnil Pawar on Medium.
You want to edit the values for
bucket, place them in quotes so:
You also want to edit
PresetId to the preset you want to transcode, which you can find in AWS Docs. You can create your own from the transcoder console if the standard controls are not sufficient. To queue multiple transcodes for a single file, add more objects to this array:
When you have filled in everything, add a trigger so that your Lambda function can be run when an object is created in your input bucket:
Make sure the role has access to the Elastic Transcoder during the execution role.
You can use this sample of jellyfish to test your function. Download a medium speed bitrate file (30Mb / s or so) and upload it to your input bucket. If successful, you would see a new job queued in the “Jobs” tab of the Elastic Transcoder Console, and you should see a new “videos” folder in your output bin containing the output files. The preset “Generic 1080p” took a 112MB 30Mb / s video and encoded it to only 18MB (about 5Mb / s):
If your Lambda function failed, you can see the logs under the “Monitoring” tab. You can also create a test case to test the function without uploading anything to the S3, but the job sent to the transcoder will be bunk.
In particular, this script saves the output files with the exact same name as the input, but you can add a prefix if you want. The transcoder works fairly quickly, so you can access your video in the output bin shortly after transmission.