Class: Abrizer::FfprobeInformer

Inherits:
Object
  • Object
show all
Includes:
FilepathHelpers
Defined in:
lib/abrizer/ffprobe_informer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FilepathHelpers

#adaptations_filepath, #all_media_paths, #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(filepath: nil, output_directory: nil) ⇒ FfprobeInformer

Returns a new instance of FfprobeInformer.



7
8
9
10
11
# File 'lib/abrizer/ffprobe_informer.rb', line 7

def initialize(filepath: nil, output_directory: nil)
  @filepath = filepath
  @output_directory = output_directory
  get_info
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/abrizer/ffprobe_informer.rb', line 6

def info
  @info
end

#json_resultObject (readonly)

Returns the value of attribute json_result.



6
7
8
# File 'lib/abrizer/ffprobe_informer.rb', line 6

def json_result
  @json_result
end

Instance Method Details

#audio_streamObject



84
85
86
87
88
# File 'lib/abrizer/ffprobe_informer.rb', line 84

def audio_stream
  @info['streams'].find do |stream|
    stream['codec_type'] == 'audio'
  end
end

#calculate_aspect_ratio_from_whObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/abrizer/ffprobe_informer.rb', line 59

def calculate_aspect_ratio_from_wh
  new_width = width
  new_height = height
  w = width
  h = height
  while h != 0
    rem = w % h
    w = h
    h = rem
  end
  new_height = new_height / w
  new_width = new_width / w
  "#{new_width}:#{new_height}"
end

#display_aspect_ratioObject

dar



49
50
51
52
53
54
55
56
57
# File 'lib/abrizer/ffprobe_informer.rb', line 49

def display_aspect_ratio #dar
  dar = video_stream['display_aspect_ratio']
  sar = video_stream['sample_aspect_ratio']
  if dar == "0:1" #&& sar == "0:1"
    calculate_aspect_ratio_from_wh
  else
    dar
  end
end

#durationObject



41
42
43
44
45
46
47
# File 'lib/abrizer/ffprobe_informer.rb', line 41

def duration
  if probe_format && probe_format['duration']
    probe_format['duration']
  elsif video_stream && video_stream['duration']
    video_stream['duration']
  end
end

#ffmpeg_info_cmdObject



90
91
92
# File 'lib/abrizer/ffprobe_informer.rb', line 90

def ffmpeg_info_cmd
  "ffprobe -v error -print_format json -show_format -show_streams #{@filepath}"
end

#get_infoObject



13
14
15
16
17
18
19
20
21
# File 'lib/abrizer/ffprobe_informer.rb', line 13

def get_info
  if @filepath && File.exist?(@filepath) && !File.directory?(@filepath)
    get_info_with_command
  elsif @output_directory && File.exist?(ffprobe_filepath)
    get_info_from_file
  else
    raise FfprobeError
  end
end

#get_info_from_fileObject



23
24
25
26
# File 'lib/abrizer/ffprobe_informer.rb', line 23

def get_info_from_file
  @json_result = File.read ffprobe_filepath
  @info = MultiJson.load @json_result
end

#get_info_with_commandObject



28
29
30
31
# File 'lib/abrizer/ffprobe_informer.rb', line 28

def get_info_with_command
  @json_result = `#{ffmpeg_info_cmd}`
  @info = MultiJson.load @json_result
end

#heightObject



37
38
39
# File 'lib/abrizer/ffprobe_informer.rb', line 37

def height
  video_stream['height'] if video_stream
end

#probe_formatObject



80
81
82
# File 'lib/abrizer/ffprobe_informer.rb', line 80

def probe_format
  @info['format']
end

#to_jsonObject



99
100
101
102
103
104
105
# File 'lib/abrizer/ffprobe_informer.rb', line 99

def to_json
  information = @info
  info_filename = information['format']['filename']
  truncated_filename = File.basename info_filename
  information['format']['filename'] = truncated_filename
  MultiJson.dump information
end

#to_sObject



94
95
96
97
# File 'lib/abrizer/ffprobe_informer.rb', line 94

def to_s
  ffmpeg_info_cmd + "\n" +
  "#{width}x#{height} DAR:#{display_aspect_ratio}"
end

#video_streamObject



74
75
76
77
78
# File 'lib/abrizer/ffprobe_informer.rb', line 74

def video_stream
  @info['streams'].find do |stream|
    stream['codec_type'] == 'video'
  end
end

#widthObject



33
34
35
# File 'lib/abrizer/ffprobe_informer.rb', line 33

def width
  video_stream['width'] if video_stream
end