How to Get Video or Audio File Time Duration Uploaded to Amazon S3 in Rails

How to Get Video or Audio File Time Duration Uploaded to Amazon S3 in Rails
In this blog post, we’ll explore how to retrieve the duration of a video or audio file that’s been uploaded to Amazon S3 using Ruby on Rails. This can come in handy when you need to display the duration of a media file on your application, or for processing media files in your Rails application.
Overview
Before we dive into the code, let’s take a brief look at the tools we’ll be using:
- Ruby on Rails: A popular web application framework written in Ruby.
- Amazon S3: Amazon Simple Storage Service (S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance.
- FFmpeg: A free and open-source software project consisting of a vast suite of libraries and programs for handling video, audio, and other multimedia files and streams.
- Active Storage: An in-built Rails library for handling file uploads to various cloud storage services.
Prerequisites
Make sure you have the following installed on your system:
- Ruby 2.7.0 or higher
- Rails 6.0.0 or higher
- FFmpeg
You should also have an Amazon S3 bucket where your media files are stored.
Getting the Duration
Here’s the step-by-step process to get the duration of a video or audio file.
1. Setup Active Storage with Amazon S3
Firstly, setup Active Storage by running rails active_storage:install
. This generates a migration that you’ll need to migrate using rails db:migrate
.
Then, in your config/storage.yml
, setup Amazon S3:
amazon:
service: S3
access_key_id: YOUR_ACCESS_KEY
secret_access_key: YOUR_SECRET_ACCESS_KEY
region: YOUR_REGION
bucket: YOUR_BUCKET
In your config/environments/production.rb
(or development.rb), tell Rails to use Amazon as the storage service:
config.active_storage.service = :amazon
2. Attach File to a Model
Next, in your model (let’s say you have a model Video
), attach the file:
class Video < ApplicationRecord
has_one_attached :file
end
3. Process the File to Get Duration
To get the duration, we’ll use FFmpeg. Create a service app/services/video_duration_service.rb
:
class VideoDurationService
def initialize(blob)
@blob = blob
end
def call
download_blob_to_tmp_file
fetch_duration
ensure
delete_tmp_file
end
private
def download_blob_to_tmp_file
@blob.open do |file|
@tmp_file = file.path
end
end
def fetch_duration
movie = FFMPEG::Movie.new(@tmp_file)
movie.duration
end
def delete_tmp_file
File.delete(@tmp_file)
end
end
This service downloads the file, fetches the duration using FFmpeg, then deletes the downloaded file.
To call this service, use VideoDurationService.new(video.file.blob).call
.
Conclusion
We’ve learned how to get the duration of a video or audio file uploaded to Amazon S3 in a Rails application. This process involves setting up Active Storage, attaching the file to a model, and using FFmpeg to fetch the duration. With these steps, you can retrieve and use media file durations in your Rails application.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.