In our Rails application, we come across a lot of cases where
we need to deal with images, videos, and audio.
With the support of
ActiveStorage which was released in Rails 5.2
it became easy for developers to upload media
to Rails application.
But after uploading the media, in few cases we also
have to analyze the media in terms of width, height, duration, etc.
With improvements in ActiveStorage,
Rails 6.2 added Image and Video Analyzer
where we could extract the metadata of the attached image or video.
But this support was missing for audio files.
Before
Let’s say,
we have a Movie model which has one title_track song attached to it.
If we want to fetch the metadata of the movie title_track, it would just return the following:
To extract audio metadata like duration, bit_rate, etc
we can use
tubbo/activestorage-audio
gem.
This gem requires
FFmpeg
to be installed as it uses
FFprobe
to extract the metadata from the audio.
This gem analyzes the audio and adds the following details to the metadata:
activestorage-audio uses the following command to extract to above details:
After
Rails 7 adds AudioAnalyzer
to ActiveStorage.
The audio is now analyzed and, the duration and bit_rate of the audio is
available as metadata without the need for any external gems.
This also needs
FFmpeg
to be installed as it uses a similar approach with the gem discussed above.
This implementation uses the following command:
Note:
This has a similar implementation of the above-mentioned gem,
but returns only duration and bit_rate.