class MultimediaParadise::FileDuration

Constants

AUDIO_DIR
#

AUDIO_DIR

#
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.

#
DEFAULT_SONG
#

DEFAULT_SONG

We can use this song here to test this class.

#
ERROR
#

ERROR

#
FILE_NAME
#

FILE_NAME

#
MAIN_REGEX
#

MAIN_REGEX

#
NAMESPACE
#

NAMESPACE

#
USE_THIS_REGEX
#

USE_THIS_REGEX

#

Public Class Methods

[]( i, run_already = :do_not_report_already, &block ) click to toggle source
#

MultimediaParadise::FileDuration[]

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

Usage example:

MultimediaParadise::FileDuration["/home/x/songs/Veracocha_CarteBlancha.mp3"]
#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 409
def self.[](
    i,
    run_already = :do_not_report_already,
    &block
  )
  _ = new(i, run_already, &block)
  return _.length?
end
new( commandline_arguments = ARGV, run_already = DEFAULT_REPORT_ALREADY ) { || ... } click to toggle source
#

initialize

The The second argument can be :do_not_report_already.

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 46
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
return_duration_of_this_file(i) click to toggle source
#

MultimediaParadise::FileDuration.return_duration_of_this_file

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

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 534
def self.return_duration_of_this_file(i)
  new(i) { :be_quiet }.duration?
end
total_time_of(i) click to toggle source
#

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)
#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 436
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

Public Instance Methods

check_if_this_file_exists(i) click to toggle source
#

check_if_this_file_exists

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 461
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_exiting() click to toggle source
#

consider_exiting

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 349
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? ) click to toggle source
#

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"
#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 210
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
    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) click to toggle source
#

determine_the_mpeg_layer

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 281
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) click to toggle source
#

do_analyse_this_file

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 496
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'
      # 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_exit() click to toggle source
#

do_not_exit

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 150
def do_not_exit
  @internal_hash[:may_we_exit] = false
end
Also aliased as: we_may_not_exit
duration()
Alias for: file_duration?
duration?()
Alias for: file_duration?
duration_as_string()
Alias for: duration_as_string?
duration_as_string?() click to toggle source
#

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.

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 395
def duration_as_string?
  duration?.to_s
end
Also aliased as: duration_as_string
file_duration()
Alias for: file_duration?
file_duration?() click to toggle source
#

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.

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 379
def file_duration?
  @internal_hash[:file_duration]
end
length?()
Alias for: file_duration?
main_file?()
Alias for: work_on_this_file?
may_we_exit?() click to toggle source
#

may_we_exit?

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 157
def may_we_exit?
  @internal_hash[:may_we_exit]
end
mpeg_layer?() click to toggle source
#

mpeg_layer?

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 187
def mpeg_layer?
  @internal_hash[:mpeg_layer]
end
n_seconds?()
Alias for: file_duration?
one_percent( file_duration = file_duration? ) click to toggle source
#

one_percent

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

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 358
def one_percent(
    file_duration = file_duration?
  )
  return ( file_duration.to_f / 100.0 )
end
raise_error() click to toggle source
#

raise_error

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 267
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 ) click to toggle source
#

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.

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 305
def report_duration(
    this_file  = main_file?,
    report_how = :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 << ' ('+sfancy(n_minutes.to_s+' minutes')+')'
      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_found() click to toggle source
#

report_that_ffmpeg_was_not_found

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 289
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? ) click to toggle source
#

report_the_total_duration

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 334
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
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method MultimediaParadise::AudioBase#reset
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 104
def reset
  super()
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
  # ======================================================================= #
  # === :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
  # ======================================================================= #
  @total_duration = 0
  # ======================================================================= #
  # === @use_colours
  #
  # Since as of May 2020 this class will make use of colours again.
  # ======================================================================= #
  set_use_colours
  # ======================================================================= #
  # === :be_verbose
  # ======================================================================= #
  set_be_verbose
end
reset_the_internal_hash() click to toggle source
#

reset_the_internal_hash

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 194
def reset_the_internal_hash
  @internal_hash.clear
end
reset_the_total_duration() click to toggle source
#

reset_the_total_duration

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 367
def reset_the_total_duration
  @total_duration = 0
end
run() click to toggle source
#

run (run tag)

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 477
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_hash
      set_work_on_this_file(this_file) # Update the main "pointer" here.
      do_analyse_this_file(this_file)
      report_duration(this_file)
      @total_duration += duration? if duration?
    }
    report_the_total_duration
  end
end
seconds()
Alias for: file_duration?
set_file_duration(i) click to toggle source
#

set_file_duration

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

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 166
def set_file_duration(i)
  @internal_hash[:file_duration] = i.to_f
end
set_work_on_this_file(this_file) click to toggle source
#

set_work_on_this_file

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 173
def set_work_on_this_file(this_file)
  @internal_hash[:work_on_this_file] = this_file
end
total_duration?() click to toggle source
#

total_duration?

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 143
def total_duration?
  @total_duration
end
use_ffmpeg_to_parse_this_mp3_file( i, be_verbose = be_verbose? )
we_may_not_exit()
Alias for: do_not_exit
work_on_this_file?() click to toggle source
#

work_on_this_file?

#
# File lib/multimedia_paradise/audio/file_duration/file_duration.rb, line 180
def work_on_this_file?
  @internal_hash[:work_on_this_file]
end
Also aliased as: main_file?