Last active
June 2, 2023 14:19
-
-
Save kjlape/cbd3887bab710e935a88b2b440096152 to your computer and use it in GitHub Desktop.
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
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 |
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 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