class MultimediaParadise::ExtractAudio
Constants
- AUTOCONVERT_AAC_INTO_MP3
#¶ ↑
AUTOCONVERT_AAC_INTO_MP3
¶ ↑If the following constant is set to true then we will autoconvert from .aac into .mp3 format, whenever we extract from a .mp4 file.
#¶ ↑
- FFMPEG_COMMAND_TO_USE
#¶ ↑
FFMPEG_COMMAND_TO_USE
¶ ↑This constant specifies which command we will use in particular, for the extract-audio operation.
#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
- STORE_HERE
#¶ ↑
STORE_HERE
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
The first argument is an array of all audio files which we wish to extract the audio stream from.
The second argument tells us where to store them, by default the current working directory.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 32 def initialize( these_files = nil, store_where = STORE_HERE, run_already = true ) reset set_files_to_process(these_files) set_store_where(store_where) if block_given? yielded = yield case yielded # ===================================================================== # # === :always_overwrite_existing_files # ===================================================================== # when :always_overwrite_existing_files @always_overwrite_existing_files = true end end run if run_already end
Public Instance Methods
#¶ ↑
cleanup_empty_files
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 148 def cleanup_empty_files if @output_filename if File.exist?(@output_filename) and (File.size(@output_filename) == 0) if File.empty? @output_filename File.delete(@output_filename) end end end end
#¶ ↑
convert_aac_into_mp3
¶ ↑
This method will convert the .aac file into .mp3 file.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 213 def convert_aac_into_mp3(i) if i i.delete!('"') e 'Now converting '+simp(i)+' into .mp3 format:' output_file = i.gsub(/#{File.extname(i)}/, '') _ = 'ffmpeg -i '+i+' '+output_file+'.mp3' run_sys_this_cmd(_) else e 'Trying to convert .aac into .mp3 but the input '\ 'to this method was nil.' end end
#¶ ↑
identify_audio_codec_from_this_file
¶ ↑
We will use ffprobe to get more information about a multimedia file.
This method will return the audio codec in use.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 172 def identify_audio_codec_from_this_file(i) i = i.to_s.dup # ======================================================================= # # We must check whether the given input includes a "'" character. If # so we will pad the input via '"'. # ======================================================================= # if i.include? "'" i = '"'+i+'"' end cmd_to_run = 'ffprobe '+i+' 2>&1' _ = `#{cmd_to_run}` splitted = _.split(N) # ======================================================================= # # We need to keep in mind that the whole string may not # include any "Audio:" tag. # ======================================================================= # if _.include? 'Audio:' splitted.select! {|entry| entry.include? 'Audio:' } splitted.map!(&:strip) _ = splitted.first if _.include? ': aac' _ = 'aac' else _ = 'mp3' end return _ else opnn; e 'No "Audio:" string could be found. Are you sure that this file' opnn; e 'has any audio data?' e 'Debug:' e 'splitted variable was:' pp splitted return false end end
#¶ ↑
output_filename?¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 339 def output_filename? @output_filename end
#¶ ↑
process_files
¶ ↑
This method is the one that will be used to process all passed files.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 244 def process_files(i = files_to_process?) if i.is_a? Array i.each {|entry| process_files(entry) } else # ===================================================================== # # === Determine the audio codec in use # # Find out which audio codec we have in the file. # ===================================================================== # audio_codec = identify_audio_codec_from_this_file(i) output_name_to_use = ''.dup output_file = i.gsub(/.flv$/,'').gsub(/.avi$/,'') if output_file.include? '/' # Fetch last entry. output_file = output_file.split('/')[-1] end notify_user_where_we_will_store file_type = File.extname(i) file_type[0,1] = '' if file_type.start_with? '.' # ===================================================================== # # Here we decide what to use. # pp "DEBUG der file type von #{i} war #{file_type}" # ===================================================================== # case file_type # case tag # ===================================================================== # # Next we handle .mp4 files. We need to keep in mind that a .mp4 # file can also contain an .aac audio track. # ===================================================================== # when 'mp4' # mp4_to_mp3 conversion next. full_filename = '"'+store_where?+output_name_to_use+ output_file.gsub(/\.mp4$/,'') if audio_codec == 'aac' full_filename << '.aac' else full_filename << '.mp3' end full_filename << '"' #_ = FFMPEG_COMMAND_TO_USE+' -i "'+i+'" -acodec libmp3lame -ab 128k ' _ = FFMPEG_COMMAND_TO_USE+' -i "'+i+'" -acodec copy -vn ' _ << full_filename set_output_filename(full_filename) run_sys_command _ when 'flv' _ = FFMPEG_COMMAND_TO_USE+' -i "'+i+'" "'+ store_where?+output_name_to_use+output_file+'.mp3"' else # else use mplayer. _ = 'mplayer '.dup _ << i+' ' _ << '-vc null ' _ << '-vo null ' _ << '-benchmark ' _ << '-aid 128 ' if i.include? '.vob ' _ << '-ao pcm:file=' _ << store_where? # User Home dir is default. _ << i+'.wav ' # hier der name der neuen datei # =================================================================== # # Hmmm. I think I am going to use ffmpeg rather than mplayer. # Easier to consolidate on one. # =================================================================== # full_filename = store_where?+output_name_to_use+ output_file+'.mp3"' set_output_filename(full_filename) # Next pad the filename, but only if it includes a "'" character # and if it does NOT start and end with a '"' already. if full_filename.include?("'") and !full_filename.start_with?('"') and !full_filename.end_with?('"') full_filename = '"'+full_filename+'"' end _ = FFMPEG_COMMAND_TO_USE+' -i "'+i+'" "'+full_filename end if _ run_sys_command _ if audio_codec if audio_codec.delete('.') == 'aac' convert_aac_into_mp3(full_filename) end notify_user_where_we_will_store cleanup_empty_files else opnn; e 'The file ('+sfancy(_.to_s)+') does not appear to exist.' end end end end
#¶ ↑
reset¶ ↑
#¶ ↑
MultimediaParadise::AudioBase#reset
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 56 def reset super() # ======================================================================= # # === @files_to_process # ======================================================================= # @files_to_process = [] # ======================================================================= # # === @output_filename # ======================================================================= # @output_filename = nil # Where to store the output file. # ======================================================================= # # === @always_overwrite_existing_files # # This is currently not fully implemented. # ======================================================================= # @always_overwrite_existing_files = false end
#¶ ↑
run_sys_command
¶ ↑
Run a system() command, while also outputting the specific command we will use.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 232 def run_sys_command(i) i = i.strip e swarn(i) result = `#{i}` return result end
#¶ ↑
set_files_to_process
¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 84 def set_files_to_process(i = nil) _ = [] i = fetch_all_flv_files if i.nil? _ << i @files_to_process = _.flatten end
#¶ ↑
set_store_where
¶ ↑
This stores only the directory component.
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 96 def set_store_where(i = STORE_HERE) i = STORE_HERE if i == :default i = STORE_HERE if i.nil? # This is in effect since Jan 2012. i = i.dup i << '/' unless i.end_with? '/' # append / if not is last char already. @store_where = i end
#¶ ↑
store_where
?¶ ↑
#¶ ↑
# File lib/multimedia_paradise/audio/extract_audio/extract_audio.rb, line 161 def store_where? @store_where end