Class: RFF::AudioHandler
- Inherits:
-
Object
- Object
- RFF::AudioHandler
- Defined in:
- lib/audio_handler.rb
Overview
This class provides an “All audio to HTML5” conversion functionality. It takes every compatible with FFmpeg audio format and converts it to the three HTML5 audio formats - mp3, ogg and wav. 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
-
#custom_args ⇒ Object
This method returns custom arguments passed to FFmpeg.
-
#fire_all ⇒ Object
This method fires all the Processor instances (conversion processes) in a separate thread at once.
-
#fire_sequential ⇒ Object
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.
-
#format_processing_percentage ⇒ Object
This method returns percentage of process completion formatted for output.
- #handler_status ⇒ Object
-
#initialize(input, output_path = nil, custom_args = nil, recommended_audio_quality = true, disable_subtitles_decoding = true) ⇒ AudioHandler
constructor
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.
-
#input ⇒ Object
This method returns full input path.
-
#killall ⇒ Object
This method kills all the processes in Processor instances and its own processing thread.
-
#mp3_processor ⇒ Object
This method returns the “to MP3” Processor instance if it exists or nil otherwise.
-
#ogg_processor ⇒ Object
This method returns the “to OGG” Processor instance if it exists or nil otherwise.
-
#output_name ⇒ Object
This method returns full output file name.
-
#output_path ⇒ Object
This method returns the path in which output file is saved.
-
#processing_percentage ⇒ Object
This method returns percentage of process completion.
-
#wav_processor ⇒ Object
This method returns the “to WAV” Processor instance if it exists or nil otherwise.
Constructor Details
#initialize(input, output_path = nil, custom_args = nil, recommended_audio_quality = true, disable_subtitles_decoding = true) ⇒ AudioHandler
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
-
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/audio_handler.rb', line 22 def initialize input, output_path=nil, custom_args=nil, recommended_audio_quality=true, disable_subtitles_decoding=true @input = input @input_type = File.basename(@input).split(".")[1] @output_path = output_path @custom_args = custom_args @processing_percentage = 0 @processors = [] types = [:mp3, :ogg, :wav] 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, nil, @custom_args, recommended_audio_quality, disable_subtitles_decoding) end @handler_status = :ready end |
Instance Method Details
#custom_args ⇒ Object
This method returns custom arguments passed to FFmpeg
199 200 201 |
# File 'lib/audio_handler.rb', line 199 def custom_args @custom_args end |
#fire_all ⇒ Object
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
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 92 93 |
# File 'lib/audio_handler.rb', line 47 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_sequential ⇒ Object
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
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 130 131 132 |
# File 'lib/audio_handler.rb', line 98 def fire_sequential @processing_thread = Thread.new do |th| begin @handler_status = :processing i = 0 @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_percentage ⇒ Object
This method returns percentage of process completion formatted for output
211 212 213 |
# File 'lib/audio_handler.rb', line 211 def format_processing_percentage @processing_percentage.nil? ? "0%" : @processing_percentage.to_s + "%" end |
#handler_status ⇒ Object
215 216 217 |
# File 'lib/audio_handler.rb', line 215 def handler_status @handler_status end |
#input ⇒ Object
This method returns full input path
181 182 183 |
# File 'lib/audio_handler.rb', line 181 def input @input end |
#killall ⇒ Object
This method kills all the processes in Processor instances and its own processing thread
136 137 138 139 140 141 |
# File 'lib/audio_handler.rb', line 136 def killall @processors.each do |proc| proc.kill end @processing_thread.kill end |
#mp3_processor ⇒ Object
This method returns the “to MP3” Processor instance if it exists or nil otherwise
145 146 147 148 149 150 151 152 153 |
# File 'lib/audio_handler.rb', line 145 def mp3_processor ret = nil @processors.each do |proc| if proc.output_type == :mp3 ret = proc end end ret end |
#ogg_processor ⇒ Object
This method returns the “to OGG” Processor instance if it exists or nil otherwise
157 158 159 160 161 162 163 164 165 |
# File 'lib/audio_handler.rb', line 157 def ogg_processor ret = nil @processors.each do |proc| if proc.output_type == :ogg ret = proc end end ret end |
#output_name ⇒ Object
This method returns full output file name
187 188 189 |
# File 'lib/audio_handler.rb', line 187 def output_name @output_name end |
#output_path ⇒ Object
This method returns the path in which output file is saved
193 194 195 |
# File 'lib/audio_handler.rb', line 193 def output_path @output_path end |
#processing_percentage ⇒ Object
This method returns percentage of process completion
205 206 207 |
# File 'lib/audio_handler.rb', line 205 def processing_percentage @processing_percentage || 0 end |
#wav_processor ⇒ Object
This method returns the “to WAV” Processor instance if it exists or nil otherwise
169 170 171 172 173 174 175 176 177 |
# File 'lib/audio_handler.rb', line 169 def wav_processor ret = nil @processors.each do |proc| if proc.output_type == :wav ret = proc end end ret end |