Audio previews in Rails without a job queue
Suppose you have this model: class Recording < ApplicationRecord has_one_attached :audio end Users upload 40-minute WAV files to it. Your app has a couple of different requirements: The feed needs a 30-second preview, the player needs a waveform, downloads should be MP3, streaming should be Opus, everything should be loudness-normalised, because half of what people upload is recorded too quiet.…
Recording audio files in a Rails application often requires additional processing, such as creating previews, waveforms, and converting formats. Traditionally, this would involve creating a variant model, attaching each variant as a job that uses ffmpeg, and handling status columns and failure scenarios. However, audioproxy offers an alternative solution that streamlines the process.
Audioproxy works as a container between your storage and users, rendering variants on demand. Instead of pre-rendering variants and storing them, audioproxy generates them as needed, making the process much simpler and more efficient. To integrate audioproxy into a Rails application, you need to add the audioproxy-rails gem to your Gemfile and configure it with your proxy endpoint, credentials, and desired settings.
In your view, you can then use the audioproxy_audio_tag helper method to generate the audio tag with the desired format and bitrate. This tag will automatically sign the URL with the provided credentials, ensuring that the URL cannot be used to access arbitrary audio files. When a user first loads the page, the audio will be generated and cached, and subsequent loads will retrieve the audio from the cached variant store.
One of the key benefits of using audioproxy is the reduction in code complexity and the elimination of a variants table, job processing, and backfilling tasks. This not only simplifies your codebase but also reduces the chances of errors and failures. By leveraging audioproxy, you can focus on building your product without getting bogged down by the intricacies of audio processing.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.