As previously promised, here are some more tips how to tackle with video processing. Streamio FFMPEG has a bunch of useful options. You can specify, for example frame rate, resolution, bitrate, codecs and many other by simply passing an options hash to transcoding. This time I wanted to change the previous code a little bit, add some options and include latest rails goodies as ActiveJob and Enumerable.
So let’s get started:
First, I have added an
enum field for processing status to have some information about the uploaded file. It was introduced in Rails 4.1. It's an attribute where database values are integers, but you can use them by name. For example:
You can learn more about enums here.
The updated post model will now look like this:
In the modified processor I have added some ffmpeg transcoding options, which modify how uploaded video will look like. I have also added job for ActiveJob library. This feature was included in Rails 4.2. Previously, when you were creating a job for background processing it may look different depending on which queueing backend you have used (sidekiq, delayedjob, sucker_punch etc.). Now on you can create a job and change the background processor in initializer without adding any changes to your job file.
More about ActiveJob can be found here.
In the job I have also added an option of taking a screenshot from uploaded movie and printing the progress of the current uploading. However, this is only for demonstration purposes because in the current shape it will only print progress in a background worker log, which may be only helpful with debugging on localhost.
And don’t forget to add an ActiveJob initializer (
config/initializers/active_job.rb) where you specify which background engine you are using. In my case it’s sidekiq.
Photo by: unsplash.com