I have an audiofile which gets uploaded via carrierwave. I wanna rename the current_file before it gets processed.
When I do processing a version, normally I rewrite the file extension via
def full_filename(for_file=file)
super.chomp(File.extname(super)) + '.mp3'
end
but this will be executed after the version creation process.
How can I make a version and renmame it before it gets saved.
To be more concret:
I am converting an WAV file to a MP3 by using ffmpeg.
FFMPEG needs an inputfile (-i inputfile.wav) and and outputfilename which needs the mp3 fileextension to process an mp3. (output.mp3 in my case)
How can I rename the extension before it get's saved?
ffmpeg -i inputfile.wav -acodec libmp3lame -f mp3 watermarked.mp3
HOW CAN I RENAME THE EXTENSTION BEFORE IT GET SAVED? ^^^
The above snip (-f forcing the codec and format) does NOT it's job and
def full_filename(for_file=file)
super.chomp(File.extname(super)) + '.mp3'
end
is happening too late (done after processing)
How can I rename the temporary Carrierfile name?
You can work around this issue by using a temp file (with an mp3 extension), and then moving it into place where it can be handled by full_filename
as expected:
version :mp3 do
process :convert_to_mp3
def convert_to_mp3
temp_path = ... # generate good temp path, ending in '.mp3'
`ffmpeg -i #{ current_path.shellescape } -acodec libmp3lame -f mp3 #{ temp_path.shellescape }`
File.unlink(current_path)
FileUtils.mv(temp_path, current_path)
end
def full_filename(for_file)
super.chomp(File.extname(super)) + '.mp3'
end
end
Some options for generating your temp_path, for you to test and decide from:
current_path.chomp(File.extname(current_path)) + '.mp3'
Tempfile.new([File.basename(current_path), '.mp3']).path
Rails.root.join('tmp', 'mp3', Dir::Tmpname.make_tmpname([original_filename,'.mp3'], nil))