Class: Converted::JPG

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

Constant Summary collapse

TMP_DIR =

Define the tmp directory path

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

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ JPG

Returns a new instance of JPG.



8
9
10
11
12
13
# File 'lib/converted/jpg.rb', line 8

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_avif(output_file = nil) ⇒ Object



32
33
34
35
# File 'lib/converted/jpg.rb', line 32

def convert_to_avif(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'avif'))
  run_ffmpeg_command("-c:v libaom-av1 -crf 30", output_file, "AVIF")
end

#convert_to_bmp(output_file = nil) ⇒ Object



37
38
39
40
# File 'lib/converted/jpg.rb', line 37

def convert_to_bmp(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'bmp'))
  run_ffmpeg_command("-vf 'format=bgr24'", output_file, "BMP")
end

#convert_to_gif(output_file = nil) ⇒ Object



15
16
17
18
# File 'lib/converted/jpg.rb', line 15

def convert_to_gif(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'gif'))
  run_ffmpeg_command("-vf 'palettegen,paletteuse'", output_file, "GIF")
end

#convert_to_ico(output_file = nil) ⇒ Object



42
43
44
45
# File 'lib/converted/jpg.rb', line 42

def convert_to_ico(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'ico'))
  run_ffmpeg_command("-vf \"scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:white\"", output_file, "ICO")
end

#convert_to_png(output_file = nil) ⇒ Object



20
21
22
23
# File 'lib/converted/jpg.rb', line 20

def convert_to_png(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'png'))
  run_ffmpeg_command("", output_file, "PNG")
end

#convert_to_webp(output_file = nil) ⇒ Object

For WEBP and AVIF, we could tweak compression settings (-crf, etc.) if higher quality or smaller file size is desired.



27
28
29
30
# File 'lib/converted/jpg.rb', line 27

def convert_to_webp(output_file = nil)
  output_file ||= tmp_file(change_extension(@input_file, 'webp'))
  run_ffmpeg_command("-c:v libwebp -lossless 0", output_file, "WEBP")
end