2
所以我試圖用Paperclip自定義處理器和ffmpeg將mp3文件轉換爲.flac。以下代碼運行ffmpeg命令並創建臨時flac文件。但是,它沒有保存?目前只有原始文件被保存。我在這裏錯過了什麼?如何使用回形針爲Ruby on Rails(5)創建音頻文件的自定義處理器
class AudioFile < ApplicationRecord
has_attached_file :raw_audio, processors: [:custom], styles: { original: {}}
定製處理器
module Paperclip
class Custom < Processor
def initialize(file, options = {}, attachment = nil)
super
@file = file
@basename = File.basename(@file.path)
@format = options[:format] || 'flac'
@params = options[:params] || '-y -i'
end
def make
source = @file
output = Tempfile.new([@basename, ".#{@format}"])
begin
parameters = [@params, ':source',':dest'].join(' ')
Paperclip.run('ffmpeg', parameters, :source => File.expand_path(source.path), :dest => File.expand_path(output.path), :sample_rate => @sample_rate, :bit_rate => @bit_rate)
end
output
end
end
end
'mp3 into .flac' - 試圖從有損到無損=不可能。不。這就像是拍攝一張100 x 100像素的圖像並將其大小調整爲1000 x 1000. –
它比擴展音頻的質量更重要。我正在使用僅接受flac或原始音頻的google語音api –
您的Paperclip配置中是否包含'log_command:true'?即使沒有,請檢查您的日誌是否有錯誤。 – Eric