Reduce the Video’s Server Load with Transloadit

Wolox Engineering
Wolox
Published in
4 min readApr 8, 2015

In applications where users upload videos, it is a common need to extract at least a thumbnail from this kind of content or even compress and encode them into a particular format.

If you are using Rails to build this application, you are probably using Carrierwave or PaperClip to upload your images and videos to Amazon S3. When using these gems, it is most likely that you’ll struggle when trying to convert videos or extracting thumbnails from them.

Whether you are developing your application in Rails or in any other language, you’ll find that encoding the videos and extracting thumbnails from them means an extra load on your application server. To reduce this load (and the associated cost) on your application server, you might want to use an external service that processes and converts the videos.

In this post we will explain how to convert videos and extract thumbnails using Transloadit. Take notice that some of the steps will be specific to Rails, but they can be easily replaced by Java or any other language.

The approach you will find in the Transloadit documentation shows how to upload files from a web application. But, if you have a mobile app or any other approach where you have a REST API or backend service that handles videos, you will also find this post useful, so keep on reading!

The first approach when using Mobile App + Rails Rest API + Amazon S3

Transloadit approach when using Mobile App + Rails Rest API + Transloadit + AmazonS3

Create a Transloadit template

First, go to https://transloadit.com/ and sign up. From your Transloadit account, create a new template and state the steps for transforming your video.

Then, name the template; in our example, we have named it “demo”. Following up, edit the template JSON with the required steps.

You may read more about the Transloadit robots and their options in the Robot API documentation.

In our example, we are importing a video from S3 (providing only the url), extracting a thumbnail from the middle of the video, encoding the video as an mp4 file with 1024x768 resolution, and storing the thumbnail and the encoded video in S3.

You may use parameters with this syntax: “${fields.param_name}” .

Each step has an input and a result. When creating templates, you can chain steps so that the input of a step turns into the output of the previous one. To achieve this, specify the value of “use”.

The template we created has the following flow:

Testing your template

After you have finished editing the template, you can manually test it to make sure that it works properly.

You will see a form with as many input fields as parameters you have in the template.

Firstly, you should input the video path as import url, then the path within the S3 bucket where you want the video, and finally, the image to be stored.

Remember to put a real video file in the Amazon bucket before running the test.

Configure S3 permissions

As soon as you run the test, you will probably get an error because you don’t have the right permissions.

You will have to allow ListBucket, PutObject and PutObjectAcl in order to permit Transloadit to store files into your S3 bucket .

Your Amazon Policy should look similar to this:

Calling the Transloadit Template from your application

Once you have successfully tested your template, you are ready to execute it from your application.

All you have to do is create an HTTP request with your Transloadit key and your template id.

curl -F ‘params={“auth”: {“key”: “KEYDETRANSLOADIT”}, “template_id”: “TEMPLATEID” }’ “http://api2.transloadit.com/assemblies"

In Rails, you could do something like this:

Note on Carrierwave: You can still use it, but an uploader would no longer be needed. You can just store the video’s url.

Handling Transloadit response

When calling a Transloadit template, the JSON response will include an assembly_id corresponding to the current request. You will get to know that the job has succeeded if the field “ok” has the value “ASSEMBLY_COMPLETED”.

You will need to make subsequent calls to the assembly_ssl_url until the “ok” field has the value “ASSEMBLY_COMPLETED”. Meanwhile, the “ok” value will probably be “ASSEMBLY_EXECUTING”.

We hope this post is useful and that you find it interesting!

Posted by Lucila Stancato (lucila.stancato@wolox.com.ar)

www.wolox.com.ar

--

--