Cloud Computing?
Lately everybody is talking about it. So at ADAM's PlayDay last week, I decided to have a look what these
"cloud services" could mean within an ADAM context.
Why consider the "cloud"?
The biggest advantage of cloud computing is that it provides us a bunch of services and resources that are accessible with minimal configuration.
In this blog post, I will show you how to implement a Media Engine that uses a cloud based video software solution, Zencoder,
to generate movie previews.
About Zencoder
Zencoder is cloud-based video and audio encoding software as a service (SaaS). It is hosted on Amazon Web Services.
Zencoder provides many, many encoding features, but for this post we'll stick to the basics, i.e. creating a preview and some thumbnails of a movie file.
Advantages
The main advantages of using a cloud based encoding solutions like Zencoder are:
Computing power: video encoding is a very intensive operation that requires a lot of CPU/GPU power. Instead of having to buy one or more high-end servers,
you can benefit from the immense and very scalable computing power available in the cloud.Pricing model: instead of having to buy very expensive licenses, cloud based solutions often offer different plans accustomed to your usage of the services.
Zencoder for instance offers a "pay-as-you-go" formula (for lightweight usage) up to a "growth" license suitable for the most demanding customers. For testing purposes,
there's even a completely free test license available (off course with some encoding limitations).No third party tool installation and configurationNo codecs need to be installedWriting a cloud based Media Engine
First thing I did, was signup for a Test license on the Zencoder website. You'll receive an API key which is passed with each Zencoder request.
Next up is choosing your way of communicating with the Zencoder API. This API is REST based, so it's possible to only use plain old HTTP GET/POST/PUT requests and some JSON.
However, there are already a couple of managed code libraries available which make it even simpler to send our encoding requests. In this post, I've used the excellent open source
(MIT license) C# Zencoder library of Chad Burggraf.
Setup
Before going further with the actual implementation, let's discuss how Zencoder processes video encoding requests.
When you want to encode a video, evidently it needs to be accessible to Zencoder. Therefore we need to put in on a place where Zencoder can upload and process it.
There are four options available:
- HTTP/HTTPS
- FTP/SFTP
- Amazon S3
- Rackspace Cloud Files
Once the encoding job has completed, Zencoder can upload the generated previews to any of the last three of these options.
If none of these options are provided to the request, Zencoder will host the outputs on its own storage for 24 hours, from where they can be downloaded.
For this demo, let's simply put the movie files into the public folder of a dropbox account. This folder can be accessed by anyone through an HTTP address
(e.g. http://dl.dropbox.com/u/<account_number>/plane.avi).
Needless to say, this is NOT the way to go for production processes :-)
Afterwards, we will download the generated outputs ourselves from the Zencoder storage.
Implementation
So let's look at the implementation now. Once you've made your movie available through any of the described options, you're ready to send the encoding request to Zencoder.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
|
Zencoder zencoder = new Zencoder("your-api-key-goes-here");
CreateJobResponse response = zencoder.CreateJob(action.Path,
new[]
{
new Output
{
Label = "converted",
FileName = Guid.NewGuid()+".mp4", // output file name
Thumbnails = new []{new Thumbnails{ Label = "Thumbs", Number = 3}}, // generated 3 thumbnails
// Note: check zencoder documentation for many more encoding options.
}
});
|
Notice the response object we get on line 2. This will tell us if the encoding job was correctly created and provides us with the details of the created job (e.g. job id).
Since the Zencoder API is completely asynchronous, you'll need this information to get the progress status of your job.
| C# |
1
2
3
4
5
|
JobProgressResponse progress = zencoder.JobProgress(response.Outputs.First().Id);
while (progress.State != OutputState.Finished)
{
...
}
|
Note: Zencoder also has an extensive notification framework that you can use, to automatically get notified when a job has completed or failed (through HTTP, email or custom handlers).
Once the job has completed we can get the location of the generated files and download them.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Job details = zencoder.JobDetails(response.Id).Job;
using (WebClient webClient = new WebClient())
{
// download the movie preview
Uri url = new Uri(details.OutputMediaFiles.First().Url);
string previewFileName = App.GetTemporaryFile("mp4");
webClient.DownloadFile(url, previewFileName);
action.MoviePreviews.Add(new MoviePreview(previewFileName));
// download the image previews
foreach (Thumbnail thumbnail in details.Thumbnails)
{
url = new Uri(thumbnail.Url);
previewFileName = App.GetTemporaryFile("png");
webClient.DownloadFile(url, previewFileName);
action.ImagePreviews.Add(new Preview(previewFileName, -1));
}
}
|
Conclusion
Cloud Computing!
I hope this post has given you a small taster on how you can easily benefit from cloud services within ADAM.
I've added a simple sample media engine that uses Zencoder to this post. To play with it, just create a free test account at Zencoder and put in your API key.
Sample Code
The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now