Created
December 4, 2011 11:40
-
-
Save mlitwiniuk/1429979 to your computer and use it in GitHub Desktop.
CarrierWave - video encoding with Voyeur - lib/carrierwave_processing/
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
module CarrierWave | |
module VideoConverter | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def encode_video(target_format) | |
process :encode_video => [target_format] | |
end | |
end | |
def encode_video(format='mp4') | |
# move upload to local cache | |
cache_stored_file! if !cached? | |
directory = File.dirname( current_path ) | |
# move upload to tmp file - encoding result will be saved to | |
# original file name | |
tmp_path = File.join( directory, "tmpfile" ) | |
File.rename current_path, tmp_path | |
# encode | |
Voyeur::Video.new( filename: tmp_path ).convert( to: format.to_sym, output_filename: current_path ) | |
# because encoding video will change file extension, change it | |
# to old one | |
fixed_name = File.basename(current_path, '.*') + "." + format.to_s | |
File.rename File.join( directory, fixed_name ), current_path | |
# delete tmp file | |
File.delete tmp_path | |
end | |
private | |
def prepare! | |
cache_stored_file! if !cached? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can I add it to carrierwave?