Class: RFF::VideoHandler

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

Overview

This class provides an “All video to HTML5” conversion functionality. It takes every compatible with FFmpeg video format and converts it to the three HTML5 video formats - mp4, ogv and webm. If the input is already in one of these formats it is only converted to the two other formats and the original file is copied to the output directory, because it can be used as one of HTML5 sources.

Instance Method Summary collapse

Constructor Details

#initialize(input, output_path = nil, quality = "5000k", custom_args = nil, recommended_audio_quality = true, disable_subtitles_decoding = true) ⇒ VideoHandler

This constructor initializes the class with the following arguments:

  • input (required) - the full path to the input file

  • output_path - a path to place the output file in. Defaults to nil, which means that the input’ s directory path is used

  • quality - only affects video conversion. Sets the video conversion quality. Defaults to 5000k, which is tested value for good video conversion quality

  • custom_args - passes custom arguments to FFmpeg. Defaults to nil, which means no custom arguments are given

  • recommended_audio_quality - determines if recommended by FFmpeg community audio quality settings should be used. Defaults to true, which means audio conversion with good, recommended quality. Set to false if you are giving additional arguments that determine this quality.

  • disable_subtitles_decoding - in some formats subtitle decoding causes problems. This option disables this feature. Defaults to true to bypass problems by default.

All of the arguments are passed on to underlying Processor instances. This method also determines input type, initializes processing percentage and creates needed Processor instances.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/video_handler.rb', line 19

def initialize input, output_path=nil, quality="5000k", custom_args=nil, recommended_audio_quality=true, disable_subtitles_decoding=true
  @input = input
  @input_type = File.basename(@input).split(".")[1]
  @output_path = output_path
  @quality = quality
  @custom_args = custom_args
  @processing_percentage = 0
  @processors = []
  types = [:mp4, :ogv, :webm]
  if !@output_path.nil? && !File.exists?(@output_path)
      FileUtils.mkdir_p(@output_path)
  end
  if types.include?(@input_type.to_sym)
    types.delete(@input_type.to_sym)
    if !@output_path.nil?
        FileUtils.cp @input, @output_path
    end
  end
  types.each do |type|
    @processors << RFF::Processor.new(@input, type, @output_path, @quality, @custom_args, recommended_audio_quality, disable_subtitles_decoding)
  end
  @handler_status = :ready
end

Instance Method Details

#custom_argsObject

This method returns custom arguments passed to FFmpeg



202
203
204
# File 'lib/video_handler.rb', line 202

def custom_args
  @custom_args
end

#fire_allObject

This method fires all the Processor instances (conversion processes) in a separate thread at once. Then it counts the overall processing percentage from all the Processor instances as the process goes and sets it to 100% on finish



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/video_handler.rb', line 45

def fire_all
  @processing_thread = Thread.new do |th|
    begin
      @processors.each do |proc|
        proc.fire
        #sleep(5)
      end
      @handler_status = :processing
      status = :processing
      while status != :done
        donecount = 0
        @processors.each do |proc|
          #puts "Process status:" + proc.status.to_s
          if (!proc.command_exit_status.nil? && (proc.status == :completed || proc.status == :failed)) || proc.status == :aborted
            donecount = donecount + 1
          end
        end
        #puts "Done count: " + donecount.to_s
        if donecount == @processors.count
          status = :done
          break
        end
        processing_percentage = 0
        @processors.each do |proc|
          processing_percentage += proc.processing_percentage
        end
        @processing_percentage = (processing_percentage.to_f/@processors.count).to_i
      end
      @processors.each do |proc|
          if proc.status != :completed
              @handler_status = :failed
              break
          end
      end
      if @handler_status != :failed
          @handler_status = :completed
      end
      @processing_percentage = 100
    rescue => e
      puts "Caught exception: " + e.to_s
      puts "Backtrace:"
      puts e.backtrace
      status = :done
      @handler_status = :failed
    end
  end
end

#fire_sequentialObject

This method fires all the Processor instances (conversion processes) in a separate thread sequentially - next Processor in the row is fired only after the Processor before finishes. It also counts the overall processing percentage from all the Processor instances as the process goes and sets it to 100% on finish



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/video_handler.rb', line 95

def fire_sequential
  @processing_thread = Thread.new do |th|
    begin
      i = 0
      @handler_status = :processing
      @processors.each do |proc|
        proc.fire
        sleep(1)
        while proc.command_exit_status.nil? || proc.status == :processing && proc.status != :aborted
          if proc.processing_percentage != nil
            @processing_percentage = (i*(100/@processors.count))+(proc.processing_percentage.to_f/@processors.count).to_i
          end
        end
        i = i+1
        #sleep(5)
      end
      @processors.each do |proc|
          if proc.status != :completed
              @handler_status = :failed
              break
          end
      end
      if @handler_status != :failed
          @handler_status = :completed
      end
      @processing_percentage = 100
    rescue => e
      puts "Caught exception: " + e.to_s
      puts "Backtrace:"
      puts e.backtrace
      status = :done
      @handler_status = :failed
    end
  end
end

#format_processing_percentageObject

This method returns percentage of process completion formatted for output



214
215
216
# File 'lib/video_handler.rb', line 214

def format_processing_percentage
  @processing_percentage.nil? ? "0%" : @processing_percentage.to_s + "%"
end

#handler_statusObject



218
219
220
# File 'lib/video_handler.rb', line 218

def handler_status
    @handler_status
end

#inputObject

This method returns full input path



178
179
180
# File 'lib/video_handler.rb', line 178

def input
  @input
end

#killallObject

This method kills all the processes in Processor instances and its own processing thread



133
134
135
136
137
138
# File 'lib/video_handler.rb', line 133

def killall
  @processors.each do |proc|
    proc.kill
  end
  @processing_thread.kill
end

#mp4_processorObject

This method returns the “to MP4” Processor instance if it exists or nil otherwise



142
143
144
145
146
147
148
149
150
# File 'lib/video_handler.rb', line 142

def mp4_processor
  ret = nil
  @processors.each do |proc|
    if proc.output_type == :mp4
      ret = proc
    end
  end
  ret
end

#ogv_processorObject

This method returns the “to OGV” Processor instance if it exists or nil otherwise



154
155
156
157
158
159
160
161
162
# File 'lib/video_handler.rb', line 154

def ogv_processor
  ret = nil
  @processors.each do |proc|
    if proc.output_type == :ogv
      ret = proc
    end
  end
  ret
end

#output_nameObject

This method returns full output file name



184
185
186
# File 'lib/video_handler.rb', line 184

def output_name
  @output_name
end

#output_pathObject

This method returns the path in which output file is saved



190
191
192
# File 'lib/video_handler.rb', line 190

def output_path
  @output_path
end

#processing_percentageObject

This method returns percentage of process completion



208
209
210
# File 'lib/video_handler.rb', line 208

def processing_percentage
  @processing_percentage || 0
end

#qualityObject

This method returns used video quality



196
197
198
# File 'lib/video_handler.rb', line 196

def quality
  @quality
end

#webm_processorObject

This method returns the “to WEBM” Processor instance if it exists or nil otherwise



166
167
168
169
170
171
172
173
174
# File 'lib/video_handler.rb', line 166

def webm_processor
  ret = nil
  @processors.each do |proc|
    if proc.output_type == :webm
      ret = proc
    end
  end
  ret
end