Twitter: raymondcamden


Address: Lafayette, LA, USA

Video encoding with Zencoder

04-26-2011 5,514 views Development, ColdFusion 4 Comments

This is something I found a few months ago but I'm just now finally finding the time to write it up. If you've ever had to work with video encoding, you know it can be somewhat difficult, especially if you are looking for an automated solution. I've worked with ffmpeg before, and while it's pretty darn powerful, it can be a chore to set it up correctly and figure out the exact right API to get the results you need. If a client came to me asking for some form of video encoding solution I'd probably punt and say - just use Youtube. That's before I found Zencoder.

Zencoder is a commercial service that provides video encoding over the Internet. At first that seemed a bit crazy. Most videos are pretty large and I couldn't imagine how well such a service would work. Zencoder though can connect to your videos in multiple ways, including via Amazon S3. As long as Zencoder can suck down the file it can process it on it's own servers and spit out your videos with multiple results:

  • You can output to 6 different video formats and 4 for audio (see output formats)
  • You can resize the video
  • You can modify quality, bitrate, framerate, and other things that are way above me
  • You can generate thumbnails from the video
  • You can add a watermark
  • You can create a clip (ie, just a portion from the original)
  • and more...

Best of all - of this works via a simple to use API. If you read my blog, you know I love APIs and I especially like services that go out of their way to make things simple. The full docs for their API may be found here: http://app.zencoder.com/docs

For the most part, your interaction with Zencoder comes down to sending a request to them telling Zencoder where your video may be accessed. Your video is then put into a queue. Their API supports multiple ways of working with your own queue. You can ask it to ping you HTTP, email, or just use another API call to check the status. When Zencoder is done it can then copy the results back to you via FTP, SFTP, FTPS, or even better - S3.

This service is not free. You can see their price list here. To me - this looks pretty reasonable. They have a web based dashboard so you can do adhoc conversions and I could see myself using this for my blog. I know there are tools I could run locally, but for a few bucks I'd much rather let Zencoder handle it.

So by now you may be asking - is there a ColdFusion wrapper for this service? The answer is - of course - heck yes. Todd Schlomer has written a wrapper and it works really well. You can find it on Github here: https://github.com/sctm/zencoder-cf (And yes - he also created a RIAForge project so you will find it if you search there.)

I whipped up a quick ColdFusion script that shows a basic set of calls with his API. It just starts a job and doesn't actually check the result. But I wanted to show how easy this was, especially with ColdFusion 901's simple S3 integration. Here is template - and again - please consider this ugly test code.

view plain print about
1<cfset apikey = "myapikey">
2<cfset zen = new zencoder.Zencoder(apikey)>
3
4<cfset s3dir = "s3://s3.coldfusionjedi.com">
5
6<cfif not fileExists(s3dir & "/lynn.3gp")>
7    <cfset fileCopy(expandPath("./lynn.3gp"), s3dir & "/lynn.3gp")>
8</cfif>
9
10<cfset source = s3dir & "/lynn.3gp">
11
12<cfset outputs = new zencoder.ZencoderOutputArray()>
13<cfset thumbs = new zencoder.ZencoderThumbnails()>
14<cfset thumbs.addThumbnail({"number"=6,"size"="160x120","base_url"="s3://zen.coldfusionjedi.com",label="thumblabel"})>
15
16<cfset outputs.addOutput(
17    new zencoder.ZencoderOutput(base_url="s3://zen.coldfusionjedi.com",
18                                filename="foo2.mp4",
19                                label="Label_1",
20                                thumbnails=thumbs,
21                                public=1)
22)
>

23
24<cfset res = zen.createEncodingJob(source,outputs)>
25
26<cfdump var="#res#">
27<cfoutput>
28<cfif structkeyexists(res, "message")>message=#res.message.tostring()#</cfif>
29<p>
30Done.
31</cfoutput>

There isn't a lot going on here really. I begin by creating an instance of the Zencoder library with my API key. I then copy - if it isn't already there, a video I made locally to a S3 folder. Next I create a Zencoder output array. You can request multiple outputs when processing but for this template I'll just work with one. I also asked for 6 thumbnails. Finally I tell Zencoder where to store the file and then create my job.

And that's it! Obviously I'm leaving off the slightly more complex portion - handling the result. As I said, you can ping the API to check the status o jobs or have Zencoder run a HTTP call against your site. I'd imagine you could set up a simple Ajax based checker on your site in a few minutes.

All in all - this is a darn impressive service, and I'm looking forward to working with it in the future. I'd love to hear from anyone who has had a chance to use this in production yet.

4 Comments

  • Commented on 04-26-2011 at 7:51 AM
    I looked at them a while back because I have a client that wanted to be able to upload any format of video and have it converted into FLV for display on their site, but for their needs (a non-profit) it was a little cost prohibitive.

    It would be really cool to see a similar service written in CF though. :)
  • Commented on 04-26-2011 at 5:14 PM
    How well does Zencoder create adaptive streaming output for Flash and iOS? I'm starting to compare offerings against my own video encoding / hosting service at videoRx.com, but most online encoding services have simply "announced" forthcoming support for adaptive streaming presets.

    RE: non-profits @ Robert Zehnder, I've provided one non-profit with pro-bono hosting and encoding (site will launch soon) specially for adaptive streaming deployment. I'm still working on the public API for videoRx.com, but there will be AMF remoting support in addition to other data xfer formats.
  • Commented on 04-26-2011 at 5:28 PM
    Great article, and thanks for the writeup of Zencoder. We have a number of ColdFusion customers and are happy to support customers who use CF (or any language).

    @Robert R: not sure what adaptive streaming has to do with this post, but Zencoder has done adaptive streaming for iOS for quite a while, and have a number of customers using it. We create segmented TS with perfect keyframe placement and let you control the segment length. We can create m3u8 playlists for a single stream and for multiple streams (adaptive bitrate). You can also add an image (like album art) for an audio-only stream, or even automatically take one thumbnail for every segment of an audio-only stream.

    But Flash (HDS) is still forthcoming.
  • Commented on 04-26-2011 at 11:08 PM
    @Jon Dahl, you're right--I was a bit presumptuous. I mentioned adaptive streaming because Ray didn't say either way if zencoder.com supported adaptive streaming, and if so, for which players/browser/etc. It's my opinion that online encoding services should not only mimic desktop encoders, but provide services that desktop can't/doesn't provide (e.g. my work on my motion analysis procedures and predictive bitrates, as an example). I have a recent blog post outlining the case for why more developers, designers, integrators, etc, need to be more aware of adaptive streaming and its benefits, which requires the cooperation of proper encoding and proper deployment across all adaptive streaming types.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty