Module: CarrierWave::FFmpeg

Defined in:
lib/carrierwave/ffmpeg.rb,
lib/carrierwave/ffmpeg/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Instance Method Details

#codec(format) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/carrierwave/ffmpeg.rb', line 23

def codec format
  case format
  when :mp4
    { video_codec: 'libx264',   
      audio_codec: 'libfaac' }
  when :webm
    { video_codec: 'libvpx',    
      audio_codec: 'libvorbis' }
  when :ogv
    { video_codec: 'libtheora', 
      audio_codec: 'libvorbis' }
  else
    raise CarrierWave::ProcessingError.new("Unsupported video format. Error: #{e}")
  end
end

#encode(format, opts = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/carrierwave/ffmpeg.rb', line 16

def encode format, opts = {}
  tmp_path = File.join File.dirname(current_path), "tmp_file.#{format}"
  file = movie current_path
  file.transcode tmp_path, options(format, file, opts), transcoder_options
  File.rename tmp_path, current_path
end

#movie(path) ⇒ Object



51
52
53
# File 'lib/carrierwave/ffmpeg.rb', line 51

def movie path
  ::FFMPEG::Movie.new path
end

#options(format, file, opts = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/carrierwave/ffmpeg.rb', line 39

def options format, file, opts = {}
  opts[:resolution]    = file.resolution unless opts[:resolution]
  opts[:video_bitrate] = file.bitrate unless opts[:video_bitrate]
  opts[:video_bitrate_tolerance] = (opts[:video_bitrate].to_i / 10).to_i
  opts[:threads] = 2 unless opts[:threads]
  opts.merge!(codec(format))
end

#transcoder_optionsObject



47
48
49
# File 'lib/carrierwave/ffmpeg.rb', line 47

def transcoder_options 
  { preserve_aspect_ratio: :height }
end