Class: Converted::MP4

Inherits:
Object
  • Object
show all
Defined in:
lib/converted/mp4.rb

Constant Summary collapse

MAX_GIF_SIZE_MB =
10
TMP_DIR =

Define the tmp directory path

File.join(Dir.pwd, 'tmp')

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ MP4

Returns a new instance of MP4.



10
11
12
13
14
15
# File 'lib/converted/mp4.rb', line 10

def initialize(input_file)
  @input_file = input_file
  unless File.exist?(@input_file)
    raise "Error: Input file #{@input_file} does not exist."
  end
end

Instance Method Details

#convert_to_avi(output_file = nil) ⇒ Object



43
44
45
46
# File 'lib/converted/mp4.rb', line 43

def convert_to_avi(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'avi'))
  run_ffmpeg_command("-c:v mpeg4 -vtag xvid -qscale:v 3", output_file, "AVI")
end

#convert_to_gif(output_file = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/converted/mp4.rb', line 22

def convert_to_gif(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'gif'))

  temp_gif = tmp_file("temp_#{File.basename(output_file)}")
  run_ffmpeg_command("-vf 'fps=10,scale=320:-1:flags=lanczos' -t 10", temp_gif, "GIF")
  
  gif_size_mb = File.size(temp_gif).to_f / (1024 * 1024)
  if gif_size_mb > MAX_GIF_SIZE_MB
    puts "GIF size (#{gif_size_mb.round(2)} MB) exceeds the maximum allowed size of #{MAX_GIF_SIZE_MB} MB."
    File.delete(temp_gif) if File.exist?(temp_gif)
  else
    FileUtils.mv(temp_gif, output_file)
    puts "GIF conversion successful: #{output_file}"
  end
end

#convert_to_mp3(output_file = nil) ⇒ Object



17
18
19
20
# File 'lib/converted/mp4.rb', line 17

def convert_to_mp3(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'mp3'))
  run_ffmpeg_command("-q:a 0 -map a", output_file, "MP3")
end

#convert_to_webm(output_file = nil) ⇒ Object



38
39
40
41
# File 'lib/converted/mp4.rb', line 38

def convert_to_webm(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'webm'))
  run_ffmpeg_command("-c:v libvpx-vp9 -b:v 1M -c:a libopus", output_file, "WEBM")
end