class MultimediaParadise::MplayerWrapper

Constants

ALSA_OPTIONS
#

ALSA_OPTIONS

#
APPEND_THESE_OPTIONS_TO_MPLAYER
#

APPEND_THESE_OPTIONS_TO_MPLAYER

#
NAMESPACE
#

NAMESPACE

#
STORE_WHERE
USE_THIS_MULTIMEDIA_PLAYER
#

USE_THIS_MULTIMEDIA_PLAYER

#

USE_THIS_MULTIMEDIA_PLAYER = ‘mpv’ # Can also be mplayer.

VO_X11
#

VO_X11

#

Public Class Methods

new( commandline_arguments = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 69
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Public Instance Methods

_(i) click to toggle source
#

_

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 477
def _(i)
  @_ << " #{i}"
  @_.squeeze!(' ')
end
Also aliased as: append
add(i) click to toggle source
#

add (add tag)

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 223
def add(i)
  @play_these_multimedia_files << [i].flatten.compact
  @play_these_multimedia_files.flatten!
  @play_these_multimedia_files.uniq! # ← Added in June 2020.
end
append(i)
Alias for: _
consider_to_notify_the_user_if_more_than_one_video_file() click to toggle source
#

consider_to_notify_the_user_if_more_than_one_video_file

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 301
def consider_to_notify_the_user_if_more_than_one_video_file
  if @play_these_multimedia_files.size > 1
    e "#{rev}More than one audio/video file was found. "\
      "We will output these now:"
    e
    @play_these_multimedia_files.each {|entry|
      e "#{sfancy('  - ')}#{entry}"
    }
    e
  end
end
cyan(i) click to toggle source
#

cyan

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 271
def cyan(i)
  "#{Colours::CYAN}#{i}#{Colours::GREEN}"
end
exit_program() click to toggle source
#

exit_program

Use this method when exiting from this class.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 287
def exit_program
  exit if @may_we_exit
end
main_string?() click to toggle source
#

main_string?

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 278
def main_string?
  @_
end
menu( i = @commandline_arguments ) click to toggle source
#

menu (menu tag)

#
notify_the_user_that_these_simpsons_episodes_will_be_played(i) click to toggle source
#

notify_the_user_that_these_simpsons_episodes_will_be_played

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 316
def notify_the_user_that_these_simpsons_episodes_will_be_played(i)
  e "#{rev}A total of #{i.size} different simpson episodes will be played."
  e "#{rev}The total duration of these files will be:"
  n_seconds = MultimediaParadise::FileDuration.total_time_of(i)
  e
  e sfancy('  '+n_seconds.to_s+' seconds')+
    ' ('+
    (n_seconds / 60.0).round(1).to_s+' minutes; '+
    slateblue(
      (n_seconds / 3600).round(1).to_s+' hours'
    )+
    ')'
  e
  set_use_these_files(i)
end
play_each_multimedia_file( i = @play_these_multimedia_files ) click to toggle source
#

play_each_multimedia_file

Here we finally iterate over the collection and play every file.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 425
def play_each_multimedia_file(
    i = @play_these_multimedia_files
  )
  i.each {|file|
    if File.directory? file
      opnn; e 'This is a directory. We will try to play every'
      opnn; e 'file in this directory next.'
      play_each_multimedia_file(
        Dir["#{file}/*"]
      )
    else
      register_file(file)
      reset_main_string
      _ "#{@use_this_multimedia_player} "
      _ APPEND_THESE_OPTIONS_TO_MPLAYER if APPEND_THESE_OPTIONS_TO_MPLAYER
      if @use_this_multimedia_player == 'mplayer'
        # The above check is necessary because mpv has no -zoom option.
        _ ' -fs -zoom ' if @play_zoomed
      end
      _ ' -vo null '  if @disable_video
      # =================================================================== #
      # Next add the -msgcolor flag. This works for mplayer, but may not
      # work for mpv, which insists on using -msg_color, so check for
      # this.
      # =================================================================== #
      if @use_mplayer_colours
        if main_string?.start_with? 'mpv '
          cmd = ' --msg-color '
          # ============================================================= #
          # Ok - this is mpv in this case:
          # ============================================================= #
          if does_the_video_player_support_this_commandline?
            _ cmd
          else
            _ ' -msgcolor '
          end
        else # else we assume mplayer is in use.
          _ ' -msgcolor '
        end
      end
      _ @append_this # Also append this string.
      # Starting from this point here, we will append the padded filename.
      e(@_+'"'+cyan(file)+'"') # We want the colours.
      _ '"'+file+'"'
      system(@_) # @_ is the String that holds our mplayer-command.
    end
  }
end
register_file( i = @file_name ) click to toggle source
#

register_file

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 232
def register_file(
    i = @file_name # register the file
  )
  number = 1
  if i
    if @hash.is_a? Hash
      _ = File.basename( i.delete('"') ) # Get rid of "
      if @hash.has_key? _
        @hash[_] = @hash[_] + number 
      else
        @hash[_] = number
      end
      save_what_into_this_file
    else
      e 'Something went wrong - @hash variable is not a hash.'
      e "Make sure to check the file #{sfile(@store_where)}."
      exit
    end
  end
end
register_sigint() click to toggle source
#

register_sigint

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 294
def register_sigint # Register at-exit handler.
  Signal.trap('SIGINT') { exit_program }
end
reset() click to toggle source
#

reset

#
Calls superclass method MultimediaParadise::Base#reset
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 84
def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @play_these_multimedia_files
  # ======================================================================= #
  @play_these_multimedia_files = [] # Is an array. In fact, it must be always an array.
  # ======================================================================= #
  # === @use_this_multimedia_player
  # ======================================================================= #
  @use_this_multimedia_player = USE_THIS_MULTIMEDIA_PLAYER
  # ======================================================================= #
  # === @store_where
  # ======================================================================= #
  @store_where = STORE_WHERE
  # ======================================================================= #
  # === @append_this
  # ======================================================================= #
  @append_this = ''
  # ======================================================================= #
  # === @play_zoomed
  # ======================================================================= #
  @play_zoomed = PLAY_ZOOMED
  # ======================================================================= #
  # === @disable_video
  # ======================================================================= #
  @disable_video = false
  # ======================================================================= #
  # === @use_mplayer_colours
  #
  # Whether to use -msgcolor or not, for mplayer.
  # ======================================================================= #
  @use_mplayer_colours = true
  # ======================================================================= #
  # === @hash
  #
  # Next, we setup the @hash variable.
  # ======================================================================= #
  @hash = {} # This hash tells us how often a song was played.
  @hash = YAML.load_file(@store_where) if File.exist? @store_where
  # Need to ensure that @hash is a hash indeed.
  @hash = {} if @hash == false
  # ======================================================================= #
  # === @may_we_exit
  # ======================================================================= #
  @may_we_exit = true
end
reset_main_string() click to toggle source
#

reset_main_string

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 138
def reset_main_string
  @_ = ''.dup # This is the sys-command to use, the mplayer string.
end
run() click to toggle source
#

run

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 485
def run
  menu
  consider_to_notify_the_user_if_more_than_one_video_file
  play_each_multimedia_file
end
sanitize_input(i) click to toggle source
#

sanitize_input

We need to be careful here.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 174
def sanitize_input(i)
  if i.is_a? Array # Replace all '+' with '*'.
    # ===================================================================== #
    # Added Jan 2014. Might fail for certain input though.
    # ===================================================================== #
    return i.map {|entry| entry.to_s.tr('+','*') }
  else
    return i
  end
end
save_what_into_this_file( what = @hash, where = STORE_WHERE ) click to toggle source
#

save_what_into_this_file

Use this to save the file played.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 258
def save_what_into_this_file(
    what = @hash, where = STORE_WHERE
  )
  begin
    File.open(where, 'w+') { |f| YAML.dump(what, f) }
  rescue Exception => error
    opnn; pp error
  end
end
set_play_these_multimedia_files( i ) click to toggle source
#

set_play_these_multimedia_files

This will always return an Array.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 206
def set_play_these_multimedia_files(
    i
  )
  verbose_exit if i.to_s.empty?
  i = i.dup if i.is_a? String
  # ======================================================================= #
  # Also includes nil.
  # ======================================================================= #
  @play_these_multimedia_files << sanitize_input(i)
  @play_these_multimedia_files.flatten!
  @play_these_multimedia_files.compact!
  try_to_ensure_that_the_files_exist
end
Also aliased as: set_use_these_files
set_use_these_files( i )
show_help_then_exit() click to toggle source
#

show_help_then_exit

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 154
def show_help_then_exit
  esystem "#{@use_this_multimedia_player} --help"
  exit
end
show_version_then_exit() click to toggle source
#

show_version_then_exit

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 162
def show_version_then_exit
  _ = `#{@use_this_multimedia_player} -version 2>&1`
  _ = _.split("\n")[-1]
  e(_)
  exit
end
try_to_ensure_that_the_files_exist() click to toggle source
#

try_to_ensure_that_the_files_exist

Here we try to make sure that the files exist, by applying a glob on it.

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 191
def try_to_ensure_that_the_files_exist
  @play_these_multimedia_files.map! {|file|
    unless File.exist? file # If the file does not exist, continue here.
      globbed = Dir[file+'*']
      file = globbed.first unless globbed.empty?
    end
    file
  }
end
verbose_exit() click to toggle source
#

verbose_exit

#
# File lib/multimedia_paradise/video/mplayer_wrapper.rb, line 145
def verbose_exit
  opnn; e 'You did not provide a multimedia file to play.'
  opnn; e 'Thus exiting now.'
  exit_program
end