Class: MultimediaParadise::FileDuration

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

Overview

MultimediaParadise::FileDuration

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
AUDIO_DIR =
#

AUDIO_DIR

#
SONG_DIR
DEFAULT_SONG =
#

DEFAULT_SONG

We can use this song here to test this class.

#
"#{AUDIO_DIR}AlexGaudino_DestinationUnkown.mp3"
FILE_NAME =
#

FILE_NAME

#
File.basename(__FILE__)+': '
DEFAULT_REPORT_ALREADY =
#

DEFAULT_REPORT_ALREADY

If this constant is set to true then we will automatically report information on the commandline to the user.

#
true
MAIN_REGEX =
#

MAIN_REGEX

#
/Duration: (\d\d:\d\d:\d\d.\d\d)/
USE_THIS_REGEX =
#

USE_THIS_REGEX

#
/Duration: (\d\d:\d\d:\d\d.\d\d),/

Constants inherited from Base

Base::ERROR, Base::ERROR_LINE, 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 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(commandline_arguments = ARGV, run_already = DEFAULT_REPORT_ALREADY, &block) ⇒ FileDuration

#

initialize

The second argument can be :do_not_report_already.

#


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
130
131
132
133
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 80

def initialize(
    commandline_arguments = ARGV,
    run_already           = DEFAULT_REPORT_ALREADY, # ← This is usually false by default.
    &block
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :do_not_exit
    # ===================================================================== #
    when :do_not_exit
      do_not_exit
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      set_be_quiet
    # ===================================================================== #
    # === :be_quiet_and_we_may_not_exit
    # ===================================================================== #
    when :be_quiet_and_we_may_not_exit
      set_be_quiet
      we_may_not_exit
    end
  end
  # ======================================================================= #
  # Test whether they are the same.
  # ======================================================================= #
  case run_already # case tag
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    run_already = false
  # ======================================================================= #
  # === :be_verbose
  # ======================================================================= #
  when :be_verbose
    set_be_verbose
  when :do_not_report_already,
       :do_not_exit
    do_not_exit
    run_already = DEFAULT_REPORT_ALREADY
  end
  run if run_already
end

Class Method Details

.[](i, run_already = :do_not_report_already, &block) ⇒ Object

#

MultimediaParadise::FileDuration[]

This is a class method - it works on class FileDuration.

Usage example:

MultimediaParadise::FileDuration["/home/x/songs/Veracocha_CarteBlancha.mp3"]
#


464
465
466
467
468
469
470
471
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 464

def self.[](
    i,
    run_already = :do_not_report_already,
    &block
  )
  _ = new(i, run_already, &block)
  return _.length?
end

.return_duration_of_this_file(i) ⇒ Object

#

MultimediaParadise::FileDuration.return_duration_of_this_file

This will simply return the duration, while being silent about it.

#


590
591
592
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 590

def self.return_duration_of_this_file(i)
  new(i) { :be_quiet }.duration?
end

.total_time_of(i) ⇒ Object

#

MultimediaParadise::FileDuration.total_time_of

This method can be used to quickly return the total duration of different multimedia files (audio files and video files), in n seconds. The method expects an Array as input.

If, for example, you provide three files as input, having the duration 1 second, 2 seconds and 3 seconds, then this method here will return a number, being 6 in this example - 6 standing for 6 seconds, obviously.

Invocation example:

array = %w( /home/x/songs/Zucchero_SenzaUnaDonna.mp3 /home/x/songs/ZombieNation_KernkraftRemix.mp3 )
MultimediaParadise::FileDuration.total_time_of(array)
#


491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 491

def self.total_time_of(i)
  if i.is_a? Array
    result = i.dup
    result.map! {|entry|
      duration = 0
      if entry and entry.include?("'")
        entry = '"'+entry+'"'
      end
      if entry and File.exist?(entry)
        # ================================================================= #
        # If the file exist then we can query the duration.
        # ================================================================= #
        duration = MultimediaParadise::FileDuration.return_duration_of_this_file(entry)
      end
      duration.to_f # return the duration here, as a float though.
    }
    return result.sum
  else
    e 'Please provide an Array as input to this method.'
  end
end

Instance Method Details

#check_if_this_file_exists(i) ⇒ Object

#

check_if_this_file_exists

#


516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 516

def check_if_this_file_exists(i)
  if i.is_a? Array
    i.each {|entry| check_if_this_file_exists(entry) }
  else
    unless File.exist? i
      e swarn('The file at `')+
        sfile(i)+
        swarn('` does not exist.')
      consider_exiting
    end
  end
end

#consider_exitingObject

#

consider_exiting

#


404
405
406
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 404

def consider_exiting # We exit only if we are allowed to exit.
  exit(2) if may_we_exit?
end

#determine_the_file_duration_via_ffmpeg(i, be_verbose = be_verbose? ) ⇒ Object Also known as: use_ffmpeg_to_parse_this_mp3_file

#

determine_the_file_duration_via_ffmpeg

This method should be able to handle all file extensions, thanks to ffmpeg - in particular ‘mp3’,‘mp4’,‘m4a’,‘m4v’,‘avi’,‘wav’,‘ogg’ and ‘flv’

We must scan for a String such as:

"Duration: 00:03:27.31, start: 0.000000, bitrate: 192 kb/s"
#


261
262
263
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
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 261

def determine_the_file_duration_via_ffmpeg(
    i,
    be_verbose = be_verbose?
  )
  # ======================================================================= #
  # Next check for ' ' characters, for the assumed file (stored in `i`).
  # If ' ' were found then '"' will be used as padding around these
  # characters.
  # ======================================================================= #
  if i.include? ' '
    i = i.to_s.dup
    i = '"'+i+'"'
  end
  # ======================================================================= #
  # The next clause handles cases such as: "Kaneda's_Theme"
  # ======================================================================= #
  if i.include? "'"  
    i = '"'+i+'"'
  end
  cmd = "ffmpeg -i #{i} #{ERROR_LINE}"
  if be_verbose and show_the_ffmpeg_command?
    e "#{rev}The following command will be run next:"
    e
    e ::Colours.slateblue("  #{cmd}")
    e
  end
  result = `#{cmd}`
  
  if result.include? 'command not found' # Check here whether we have installed ffmpeg or not.
    report_that_ffmpeg_was_not_found
    exit
  else
    if result.include? 'command not found' # Check here whether we have installed ffmpeg or not.
      report_that_ffmpeg_was_not_found
    else
      result =~ USE_THIS_REGEX
      _ = $1.to_s.dup
      if $1 and _ and !_.empty?
        # ===================================================================== #
        # Must convert the format given in _ into seconds next:
        # ===================================================================== #
        hours = HoursToSeconds.new(_, :be_silent)
        result = hours.n_seconds
        set_file_duration(result)
        return result
      else
        opne rev+'The duration could not be determined.'
        raise_error if file_duration? == 0 # since as of July 2011.
        return nil
      end
    end
  end
end

#determine_the_mpeg_layer(i) ⇒ Object

#

determine_the_mpeg_layer

#


332
333
334
335
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 332

def determine_the_mpeg_layer(i)
  result = ::MultimediaParadise.return_the_mpeg_layer_from_this_mp3_file(i)
  @internal_hash[:mpeg_layer] = result
end

#do_analyse_this_file(i) ⇒ Object

#

do_analyse_this_file

#


551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 551

def do_analyse_this_file(i)
  extname = File.extname(i)
  extname_without_dots = extname.delete('.')
  case extname_without_dots # case tag
  # ======================================================================= #
  # === mp3
  #
  # We determine the MPEG layer only for .mp3 files.
  # ======================================================================= #
  when 'mp3'
    determine_the_mpeg_layer(i)
  else
    case extname_without_dots
    when 'mp4',
         'm4v',
         'wav',
         'ogg',
         'flac',
         'aac',
         'mpg',
         'webm'
      # pass through in this case.
    else
      e 'Unhandled file type: '+
        steelblue(extname)
      return
    end
  end
  # ======================================================================= #
  # We will always try to let ffmpeg determine the file's duration.
  # ======================================================================= #
  determine_the_file_duration_via_ffmpeg(i)
end

#do_not_exitObject Also known as: we_may_not_exit

#

do_not_exit

#


208
209
210
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 208

def do_not_exit
  @internal_hash[:may_we_exit] = false
end

#do_show_the_ffmpeg_commandObject

#

do_show_the_ffmpeg_command

#


163
164
165
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 163

def do_show_the_ffmpeg_command
  @internal_hash[:show_the_ffmpeg_command] = true
end

#duration_as_string?Boolean Also known as: duration_as_string

#

duration_as_string?

This method will always return a String representation. If you need a Float, you can either use the method called .duration? or simply convert the String into a Float via .to_f.

#

Returns:

  • (Boolean)


450
451
452
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 450

def duration_as_string?
  duration?.to_s
end

#file_duration?Boolean Also known as: length?, seconds, n_seconds?, file_duration, duration?, duration

#

file_duration?

This method will return the duration of the multimedia file at hand. For instance, a local .mp3 file may have a duration of “180 seconds”, in which case this method should return the file_duration stored as a Float. The setter-method set_file_duration() ensures this.

#

Returns:

  • (Boolean)


434
435
436
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 434

def file_duration?
  @internal_hash[:file_duration]
end

#may_we_exit?Boolean

#

may_we_exit?

#

Returns:

  • (Boolean)


215
216
217
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 215

def may_we_exit?
  @internal_hash[:may_we_exit]
end

#mpeg_layer?Boolean

#

mpeg_layer?

#

Returns:

  • (Boolean)


245
246
247
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 245

def mpeg_layer?
  @internal_hash[:mpeg_layer]
end

#one_percent(file_duration = file_duration? ) ⇒ Object

#

one_percent

This method gives us back how many seconds one percent are.

#


413
414
415
416
417
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 413

def one_percent(
    file_duration = file_duration?
  )
  return ( file_duration.to_f / 100.0 )
end

#raise_errorObject

#

raise_error

#


318
319
320
321
322
323
324
325
326
327
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 318

def raise_error
  # raise '@file_duration for '+@file+' is nil.'
  ewarn 'From '+FILE_NAME+'file_duration for '
  ewarn '   -> '+files?.first
  ewarn ' is nil.'
  e 'Showing the caller() stack:'
  e
  pp caller()
  e
end

#report_duration(this_file = main_file?, , report_how = :verbose, be_verbose = be_verbose? ) ⇒ Object

#

report_duration (run tag)

We provide two basic ways to output the result in this method:

(1) The default way will show the amount of seconds on a new line
(2) The compact (terse) variant will show everything on one line

Option (2) was necessary because some other programs also report everything on the same line.

#


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 356

def report_duration(
    this_file  = main_file?,
    report_how = :verbose,
    be_verbose = be_verbose?
  ) # Can be :verbose or :compact
  length = length?
  if be_verbose
    case report_how
    when :verbose
      result = rev+
               'The duration of the file `'+
               sfile(this_file)+
               rev+'` is:'+N+
               '  '+simp(length)+
               rev+' seconds.'+rev
      if length.to_f > 60
        result = result.dup
        n_minutes = ( length.to_f / 60.0 ).round(1)
        result << "#{rev} (#{sfancy(n_minutes.to_s+' minutes')}#{rev})"
      end
      opne result
    else
      opne rev+
           'The duration of the file `'+sfile(this_file.first)+
           rev+'` is:  '+
           simp(length.to_s)+rev+' seconds.'+rev
    end
  end
end

#report_that_ffmpeg_was_not_foundObject

#

report_that_ffmpeg_was_not_found

#


340
341
342
343
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 340

def report_that_ffmpeg_was_not_found
  e 'It seems as if you have not installed ffmpeg. Please install'
  e 'it - otherwise this class can not work properly. Thank you.'
end

#report_the_total_duration(i = total_duration? ) ⇒ Object

#

report_the_total_duration

#


389
390
391
392
393
394
395
396
397
398
399
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 389

def report_the_total_duration(
    i = total_duration?
  )
  _ = commandline_arguments?
  if _.size > 1
    e
    e 'The total duration of these '+_.size.to_s+' files is '+
      steelblue(i.to_s).to_s+' seconds.'
    e
  end
end

#resetObject

#

reset (reset tag)

#


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 138

def reset
  super()
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  reset_the_internal_variables
  # ======================================================================= #
  # === @use_colours
  #
  # Since as of May 2020 this class will make use of colours again.
  # ======================================================================= #
  set_use_colours
  # ======================================================================= #
  # === :be_verbose
  # ======================================================================= #
  set_be_verbose
  # ======================================================================= #
  # === :show_the_ffmpeg_command
  # ======================================================================= #
  @internal_hash[:show_the_ffmpeg_command] = false
end

#reset_the_internal_variablesObject

#

reset_the_internal_variables

#


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 177

def reset_the_internal_variables
  # ======================================================================= #
  # === :work_on_this_file
  # ======================================================================= #
  @internal_hash[:work_on_this_file] = nil
  # ======================================================================= #
  # === :file_duration
  #
  # This variable will contain the duration of the .mp3 file at hand.
  # ======================================================================= #
  @internal_hash[:file_duration] = 0
  # ======================================================================= #
  # === :may_we_exit
  # ======================================================================= #
  @internal_hash[:may_we_exit] = true
  # ======================================================================= #
  # === :total_duration
  # ======================================================================= #
  @internal_hash[:total_duration] = 0
end

#reset_the_total_durationObject

#

reset_the_total_duration

#


422
423
424
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 422

def reset_the_total_duration
  @internal_hash[:total_duration] = 0
end

#runObject

#

run (run tag)

#


532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 532

def run
  _ = commandline_arguments?
  unless _.empty?
    reset_the_total_duration # Reset it, just in case.
    _.each {|this_file|
      check_if_this_file_exists(this_file)
      reset_the_internal_variables
      set_work_on_this_file(this_file) # Update the main "pointer" here.
      do_analyse_this_file(this_file)
      report_duration(this_file)
      @internal_hash[:total_duration] += duration? if duration?
    }
    report_the_total_duration
  end
end

#set_file_duration(i) ⇒ Object

#

set_file_duration

This must be stored as a float, hence why .to_f is used.

#


224
225
226
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 224

def set_file_duration(i)
  @internal_hash[:file_duration] = i.to_f
end

#set_work_on_this_file(this_file) ⇒ Object

#

set_work_on_this_file

#


231
232
233
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 231

def set_work_on_this_file(this_file)
  @internal_hash[:work_on_this_file] = this_file
end

#show_the_ffmpeg_command?Boolean

#

show_the_ffmpeg_command?

#

Returns:

  • (Boolean)


170
171
172
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 170

def show_the_ffmpeg_command?
  @internal_hash[:show_the_ffmpeg_command]
end

#total_duration?Boolean

#

total_duration?

#

Returns:

  • (Boolean)


201
202
203
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 201

def total_duration?
  @internal_hash[:total_duration]
end

#work_on_this_file?Boolean Also known as: main_file?

#

work_on_this_file?

#

Returns:

  • (Boolean)


238
239
240
# File 'lib/multimedia_paradise/audio/file_duration/file_duration.rb', line 238

def work_on_this_file?
  @internal_hash[:work_on_this_file]
end