Class: MultimediaParadise::Audio::ExtractAudio

Inherits:
MultimediaParadise::AudioBase show all
Defined in:
lib/multimedia_paradise/audio/extract_audio/extract_audio.rb

Overview

MultimediaParadise::Audio::ExtractAudio

Constant Summary collapse

STORE_HERE =
#

STORE_HERE

#
(Dir.pwd+'/').squeeze '/'
FFMPEG_COMMAND_TO_USE =
#

FFMPEG_COMMAND_TO_USE

This constant specifies which command we will use in particular, for the extract-audio operation.

#
'ffmpeg -y'
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.

#
true

Constants inherited from Base

Base::ERROR, Base::ERROR_LINE, Base::NAMESPACE, Base::USE_THIS_NAMESPACE_FOR_THE_COLOURS, Base::USE_THIS_NAMESPACE_FOR_THE_CORE_COLOURS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MultimediaParadise::AudioBase

#directory_to_the_local_songs?, #return_random_audio_file

Methods inherited from Base

#actions, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #cartoons_directory?, #clear_the_internal_hash, #cliner, #cliner_with_time_stamp, #colourized_comment, #copy_file, #crimson, #dataset_from_file_video_collection, #dd_mm_yyyy, #debug?, #default_readlines, #directory_to_realvids?, #do_not_use_opn, #dodgerblue, #does_the_video_player_support_this_commandline?, #e, #ecomment, #ecrimson, #efancy, #enable_debug, #ensure_main_encoding, #ensure_that_the_output_directory_exists, #eparse, #erev, #esystem, #ewarn, #file_video_collection?, #filter_for_audio_files, #filter_for_video_files, #forestgreen, #gold, #grey, #hh_mm_ss, #home_x_video?, #indianred, #infer_the_namespace, #internal_hash?, #is_audio_file?, #is_mkv?, #is_mp3?, #is_mp4?, #is_multimedia_file?, #is_on_roebe?, #is_video_file?, #konsole_colour_peru, #lightblue, #lightgreen, #load_yaml, #local_audio_directory?, #log_dir?, #map_symbol_to_locally_existing_file, #mediumorchid, #mediumpurple, #mediumslateblue, #mkdir, #move_file, #namespace?, #no_file_exists, #no_file_exists_at, #olive, #olivedrab, #opne, #opnecomment, #opnn, #orange, #palegoldenrod, #palegreen, #powderblue, #project_base_directory?, #rds, #register_sigint, #remove_file, #report_pwd, #reset_the_internal_hash, #return_all_video_files, #return_pwd, #return_random_video, #rev, #royalblue, #save_what_into, #sdir, #seagreen, #seconds_to_time_format, #select_only_video_files_from, #set_be_verbose, #set_use_colours, #sfancy, #sfile, #simp, #skyblue, #slateblue, #springgreen, #steelblue, #swarn, #teal, #time_right_now, #to_hh_mm_ss, #tomato, #true_rev, #try_to_rename_kde_konsole_tab, #ucliner, #use_colours?, #use_opn?, #use_which_video_player?, #verbose_truth, #video_collection?, #violet, #yaml_directory?, #yel

Methods included from CommandlineArgumentsModule

#all_input_starts_with_a_number?, #commandline_arguments?, #commandline_arguments_as_a_string, #first_argument?, #first_non_hyphened_commandline_argument?, #set_commandline_arguments

Constructor Details

#initialize(these_files = nil, store_where = STORE_HERE, run_already = true) ⇒ ExtractAudio

#

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.

#


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 55

def initialize(
    these_files = nil,
    store_where = STORE_HERE,
    run_already = true
  )
  reset
  set_files_to_process(these_files)
  set_store_where(store_where)
  # ======================================================================= #
  # === Handle blocks given to this method next
  # ======================================================================= #
  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

Class Method Details

.[](i = ARGV) ⇒ Object

#

MultimediaParadise::Audio::ExtractAudio[]

#


382
383
384
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 382

def self.[](i = ARGV)
  new(i)
end

Instance Method Details

#cleanup_empty_files(output_filename = @output_filename) ⇒ Object

#

cleanup_empty_files

#


168
169
170
171
172
173
174
175
176
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 168

def cleanup_empty_files(output_filename = @output_filename)
  if output_filename
    if File.exist?(output_filename) and (File.size(@output_filename) == 0)
      if File.empty? output_filename
        remove_this_file(output_filename)
      end
    end
  end
end

#convert_aac_into_mp3(i) ⇒ Object

#

convert_aac_into_mp3

This method will convert the .aac file into .mp3 file.

#


233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 233

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

#exit_programObject

#

exit_program

#


150
151
152
153
154
155
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 150

def exit_program
  e 'You tried to run the program, however you have not assigned'
  e 'a filename to use yet. Please give a filename as argument'
  e 'before running this program.'
  exit
end

#fetch_all_flv_filesObject

#

fetch_all_flv_files

#


136
137
138
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 136

def fetch_all_flv_files
  return Dir['*.flv']
end

#file?Boolean

#

file?

Which file to process.

#

Returns:

  • (Boolean)


129
130
131
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 129

def file?
  files_to_process?.first
end

#files_to_process?Boolean

#

files_to_process?

#

Returns:

  • (Boolean)


143
144
145
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 143

def files_to_process?
  @internal_hash[:files_to_process]
end

#identify_audio_codec_from_this_file(i) ⇒ Object

#

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.

#


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 192

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
    opne rev+'No "Audio:" string could be found. Are you sure that this file'
    opne 'has any audio data?'
    e 'Debug:'
    e 'splitted variable was:'
    pp splitted
    return false
  end
end

#notify_user_where_we_will_storeObject

#

notify_user_where_we_will_store

#


160
161
162
163
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 160

def notify_user_where_we_will_store
  e "#{rev}(Note: Will store in the directory "\
    "#{sdir(store_where?)})#{N}"
end

#output_filename?Boolean Also known as: file_location?

#

output_filename?

#

Returns:

  • (Boolean)


360
361
362
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 360

def output_filename?
  @output_filename
end

#process_files(i = files_to_process?) ) ⇒ Object

#

process_files

This method is the one that will be used to process all passed files.

#


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 264

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
        opne rev+'The file ('+sfancy(_.to_s)+rev+
             ') does not appear to exist.'
      end
    end
  end
end

#resetObject

#

reset

#


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 82

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === :files_to_process
  # ======================================================================= #
  @internal_hash[: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

#runObject

#

run (run tag)

#


374
375
376
377
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 374

def run
  exit_program if files_to_process?.empty?
  process_files
end

#run_sys_command(i) ⇒ Object Also known as: run_sys_this_cmd

#

run_sys_command

Run a system() command, while also outputting the specific command we will use.

#


252
253
254
255
256
257
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 252

def run_sys_command(i)
  i = i.strip
  e swarn(i)
  result = `#{i}`
  return result
end

#set_files_to_process(i = nil) ⇒ Object Also known as: files=

#

set_files_to_process

#


104
105
106
107
108
109
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 104

def set_files_to_process(i = nil)
  _ = []
  i = fetch_all_flv_files if i.nil?
  _ << i
  @internal_hash[:files_to_process] = _.flatten
end

#set_output_filename(i = nil) ⇒ Object

#

set_output_filename

#


367
368
369
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 367

def set_output_filename(i = nil)
  @output_filename = i.to_s.delete('"')
end

#set_store_where(i = STORE_HERE) ⇒ Object Also known as: store_in_default_directory

#

set_store_where

This stores only the directory component.

#


116
117
118
119
120
121
122
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 116

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_whereBoolean

#

store_where?

#

store_where

Returns:

  • (Boolean)


183
184
185
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 183

def store_where?
  @store_where
end

#store_where?Boolean Also known as: path?

#

store_where?

#

Returns:

  • (Boolean)


181
182
183
# File 'lib/multimedia_paradise/audio/extract_audio/extract_audio.rb', line 181

def store_where?
  @store_where
end