Video encoding with Zencoder

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... </ul>

    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.

    <cfset apikey = "myapikey"> <cfset zen = new zencoder.Zencoder(apikey)> <cfset s3dir = "s3://s3.coldfusionjedi.com"> <cfif not fileExists(s3dir & "/lynn.3gp")> <cfset fileCopy(expandPath("./lynn.3gp"), s3dir & "/lynn.3gp")> </cfif> <cfset source = s3dir & "/lynn.3gp"> <cfset outputs = new zencoder.ZencoderOutputArray()> <cfset thumbs = new zencoder.ZencoderThumbnails()> <cfset thumbs.addThumbnail({"number"=6,"size"="160x120","base_url"="s3://zen.coldfusionjedi.com",label="thumblabel"})> <cfset outputs.addOutput( new zencoder.ZencoderOutput(base_url="s3://zen.coldfusionjedi.com", filename="foo2.mp4", label="Label_1", thumbnails=thumbs, public=1) )> <cfset res = zen.createEncodingJob(source,outputs)> <cfdump var="#res#"> <cfoutput> <cfif structkeyexists(res, "message")>message=#res.message.tostring()#</cfif> <p> Done. </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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments