Created
July 15, 2025 00:42
-
-
Save nanonanomachine/19ae30ce66a3aff340cffb241cde0c8a to your computer and use it in GitHub Desktop.
app/services/audio_uploader.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AudioUploader | |
# ctor takes a logged-in user (ActiveRecord) and path to a temp WAV | |
def initialize(user, tmpfile_path) | |
@user = user # e.g. <User id:1> | |
@tmpfile_path = tmpfile_path # e.g. "/tmp/input.wav" | |
@s3_client = Aws::S3::Client.new(region: "ap-northeast-1") | |
end | |
# -- master workflow (all side-effects inline) --------------------------- | |
def run | |
# 1) convert WAV → MP3 (128 kbps, CBR) | |
system("ffmpeg -y -i #{@tmpfile_path} " \ | |
"-codec:a libmp3lame -b:a 128k /tmp/out.mp3") | |
# 2) measure integrated loudness (LUFS) with ffprobe/loudnorm | |
loudness = `ffmpeg -i /tmp/out.mp3 -af loudnorm=I=-14:TP=-1.5:LRA=11 \ | |
-f null - 2>&1 | grep "I:" | tail -1 | awk '{print $2}'`.to_f | |
# 3) upload to S3 (blocking call) | |
bucket = ENV.fetch("AUDIO_BUCKET", "my-demo-bucket") | |
key = "tracks/#{SecureRandom.uuid}.mp3" | |
@s3_client.put_object(bucket: bucket, | |
key: key, | |
body: File.open("/tmp/out.mp3", "rb")) | |
# 4) persist track + send “ready” e-mail | |
track = @user.audio_tracks.create!( | |
s3_key: key, | |
status: "ready", | |
loudness_lufs: loudness | |
) | |
AudioMailer.with(user: @user, track: track).ready.deliver_now | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment