Skip to content

Instantly share code, notes, and snippets.

@mlitwiniuk
Created December 4, 2011 11:40
Show Gist options
  • Save mlitwiniuk/1429979 to your computer and use it in GitHub Desktop.
Save mlitwiniuk/1429979 to your computer and use it in GitHub Desktop.
CarrierWave - video encoding with Voyeur - lib/carrierwave_processing/
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
@Tarikus
Copy link

Tarikus commented Jun 23, 2012

how can I add it to carrierwave?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment