Class: Abrizer::Adaptation

Inherits:
Object
  • Object
show all
Includes:
DebugSettings, FilepathHelpers
Defined in:
lib/abrizer/adaptation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DebugSettings

#debug_settings

Methods included from FilepathHelpers

#adaptations_filepath, #audio_filepath, #audio_filepath_fragmented, #basename, #canvas_filepath, #canvas_partial_filepath, #captions_filepath, #data_filepath, #data_partial_filepath, #ffprobe_filepath, #filename_directory, #first_image_filepath, #hlsts_aac_filepath, #hlsts_aac_partial_filepath, #hlsts_filepath, #hlsts_partial_filepath, #mp3_filepath, #mp3_partial_filepath, #mp4_filepath, #mp4_partial_filepath, #mpd_filepath, #mpd_partial_filepath, #output_directory, #output_directory_basename, #poster_filepath, #poster_image_filepath, #poster_partial_filepath, #sprites_filepath, #sprites_partial_filepath, #vp9_filepath, #vp9_partial_filepath, #webvtt_input_filepath

Constructor Details

#initialize(width:, height:, bitrate:) ⇒ Adaptation

Returns a new instance of Adaptation.



9
10
11
12
13
# File 'lib/abrizer/adaptation.rb', line 9

def initialize(width:, height:, bitrate:)
  @width = width
  @height = height
  @bitrate = bitrate
end

Instance Attribute Details

#bitrateObject (readonly)

Returns the value of attribute bitrate.



7
8
9
# File 'lib/abrizer/adaptation.rb', line 7

def bitrate
  @bitrate
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/abrizer/adaptation.rb', line 7

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/abrizer/adaptation.rb', line 7

def width
  @width
end

Instance Method Details

#constrained_bitrateObject

TODO: make the constrained bitrate (maxrate) value configurable



30
31
32
# File 'lib/abrizer/adaptation.rb', line 30

def constrained_bitrate
  @bitrate * 1.1
end

#ffmpeg_cmd(input, output_directory, pass) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/abrizer/adaptation.rb', line 15

def ffmpeg_cmd(input, output_directory, pass)
  cmd = %Q|ffmpeg -y #{debug_settings} \
      -i #{input} -vf \
      yadif,scale='#{width}:trunc(#{width}/dar/2)*2',setsar=1 \
      -an -c:v libx264 -x264opts 'keyint=48:min-keyint=48:no-scenecut' \
      -b:v #{bitrate}k -preset faster -pix_fmt yuv420p |
  if pass == 2
    cmd += %Q| -maxrate #{constrained_bitrate}k -bufsize #{bitrate}k -pass 2 #{filepath(input, output_directory)} |
  else
    cmd += " -pass 1 -f mp4 /dev/null "
  end
  cmd
end

#filepath(input, output_directory) ⇒ Object



40
41
42
43
# File 'lib/abrizer/adaptation.rb', line 40

def filepath(input, output_directory)
  name = "#{outfile_basename(input)}.mp4"
  File.join output_directory, name
end

#filepath_fragmented(input, output_directory) ⇒ Object



45
46
47
48
# File 'lib/abrizer/adaptation.rb', line 45

def filepath_fragmented(input, output_directory)
  name = "#{outfile_basename(input)}-frag.mp4"
  File.join output_directory, name
end

#outfile_basename(input) ⇒ Object



34
35
36
37
38
# File 'lib/abrizer/adaptation.rb', line 34

def outfile_basename(input)
  extname = File.extname input
  basename = File.basename input, extname
  "#{basename}-#{width}x#{height}-#{bitrate}"
end

#to_hashObject



58
59
60
# File 'lib/abrizer/adaptation.rb', line 58

def to_hash
  {width: @width, height: @height, bitrate: @bitrate}
end

#to_jsonObject



54
55
56
# File 'lib/abrizer/adaptation.rb', line 54

def to_json
  MultiJson.dump(to_hash)
end

#to_sObject



50
51
52
# File 'lib/abrizer/adaptation.rb', line 50

def to_s
  "Width: #{@width}, Height: #{@height}, Bitrate: #{@bitrate}"
end