Skip to content

Instantly share code, notes, and snippets.

@kjlape
Last active June 2, 2023 14:19
Show Gist options
  • Save kjlape/cbd3887bab710e935a88b2b440096152 to your computer and use it in GitHub Desktop.
Save kjlape/cbd3887bab710e935a88b2b440096152 to your computer and use it in GitHub Desktop.
thing.audio.open do |file|
input = file.path
output = "#{file.path}.mp3"
filename = "#{thing.audio.filename}.mp3"
system("ffmpeg", "-i", input, output, exception: true)
thing.audio_mp3.attach(io: File.open(output), filename:, content_type: "audio/mpeg")
ensure
File.delete(output) if File.exist?(output)
end
class TranscodeAudioJob < ApplicationJob
def perform(input_attachment, target, output_attribute, extension, content_type = Mime::Type.lookup_by_extension(extension).to_s)
output_attachment = target.send(output_attribute)
input_attachment.open do |file|
input = file.path
output = "#{file.path}.#{extension}"
filename = "#{input_attachment.filename}.#{extension}"
system("ffmpeg", "-i", input, output, exception: true)
output_attachment.attach(io: File.open(output), filename:, content_type:)
ensure
File.delete(output) if File.exist?(output)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment