Recently for a project I was working on, I needed to set up a video streaming backend for an iOS app. After digging around and seeing what the potential solutions for this were, I decided to use Amazon AWS as the backend. Next, in terms of the format that I should use, it seemed that http live streaming was the best option. Since it took a bit of time to organize and layout how this system worked, I figured it would be nice to write what I found.
Steps to Building your own Streaming Backend
- Save an original video file to disk that you want to stream. This will be typically be some quicktime format, mp4, etc.
- Log onto your Amazon AWS account (or make one) (http://aws.amazon.com/) and create 3 s3 buckets. One will be for the original video files, one is for a thumbnail preview of each video, and one will be for the actual streaming video files.
- Either through code or the amazon web interface, upload that file to the s3 bucket made to store all video files.
- Now, to convert these videos to a streaming format, we will need to do several things. First, I think it is probably a good idea to go over how http streaming actually works. so:
- HTTP Live Streaming
- Basically an extension of audio (mp3) streaming
- File format is M3U8.
- This looks something like:
#EXT-X-VERSION:3 #EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:1 # Old-style integer duration; avoid for newer clients. #EXTINF:10, http://media.example.com/segment0.ts # New-style floating-point duration; use for modern clients. #EXTINF:10.0, http://media.example.com/segment1.ts #EXTINF:9.5, http://media.example.com/segment2.ts #EXT-X-ENDLIST
- Essentially, it is a list of a ton of smaller .ts files. Apple recommends these to be about 10 seconds of video each. So, we need to convert our large video file into a ton of small .ts file clips, and then generate a M3U8 file that describes all the clips we need to make up our original video and therefore let the video actually stream.
- HTTP Live Streaming
- So, one bucket will hole these .m38u files (and all the corresponding .ts files) and then that file url is directly playable by various UI elements of iOS.
- To convert from .mov files or any original format to playable .m38u file, you will need to use the Amazon Elastic Transcoder.
- Open up the Elastic Transcoder on Amazon AWS console.
- Create a new Pipeline and hook up input and output buckets
- Create a new Transcoding Job.
- Input key: filename of your desire
- output key prefix: folder you want your output in i.e. ‘myStreamingVids/’
- I used a system preset of HLS 2m
- Use a segment duration of 10
- output key: whatever you want
- Set master playlist name to final video url stream name
- set playlist format to be hlsv3
- set ouputs in master playlist to be your output key.
- Create job.
- This will kickoff and run this job.
- Use Amazon AWS Lambda
- Set up notifications to be called whenever a new video is uploaded to s3 input videos bucket. (I have it called on all new uploads)
- This calls your Lambda function. (its a .js file)
- My JS file extracts file info and then creates an Elastic Transcoder Job and adds it to my pipeline.