Class: MultimediaParadise::MplayerWrapper

Inherits:
Base
  • Object
show all
Defined in:
lib/multimedia_paradise/video/mplayer_wrapper.rb

Overview

MultimediaParadise::MplayerWrapper

Constant Summary collapse

ALSA_OPTIONS =
#

ALSA_OPTIONS

#
'-ao alsa '
VO_X11 =
#

VO_X11

#
'-vo x11'
APPEND_THESE_OPTIONS_TO_MPLAYER =
#

APPEND_THESE_OPTIONS_TO_MPLAYER

#
' '+VO_X11
USE_THIS_MULTIMEDIA_PLAYER =
#

USE_THIS_MULTIMEDIA_PLAYER

#

USE_THIS_MULTIMEDIA_PLAYER = ‘mpv’ # Can also be mplayer.

::MultimediaParadise.use_this_player
STORE_WHERE =
"#{HOME_DIR}mplayer_favourite_files.yml"

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

Instance Method Summary collapse

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, #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 = nil, run_already = true) ⇒ MplayerWrapper

#

initialize

#


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 64

def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Instance Method Details

#_(i) ⇒ Object Also known as: append

#

_

#


469
470
471
472
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 469

def _(i)
  @_ << " #{i}"
  @_.squeeze!(' ')
end

#add(i) ⇒ Object

#

add (add tag)

#


215
216
217
218
219
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 215

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

#consider_to_notify_the_user_if_more_than_one_video_fileObject

#

consider_to_notify_the_user_if_more_than_one_video_file

#


293
294
295
296
297
298
299
300
301
302
303
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 293

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) ⇒ Object

#

cyan

#


263
264
265
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 263

def cyan(i)
  "#{Colours::CYAN}#{i}#{Colours::GREEN}"
end

#exit_programObject

#

exit_program

Use this method when exiting from this class.

#


279
280
281
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 279

def exit_program
  exit if @may_we_exit
end

#main_string?Boolean

#

main_string?

#

Returns:

  • (Boolean)


270
271
272
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 270

def main_string?
  @_
end
#

menu (menu tag)

#


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 327

def menu(
    i = @commandline_arguments
  )
  if i and i.is_a?(Array)
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === mplayer_wrapper --simpsons=10
    # ===================================================================== #
    when /^-?-?simpsons=(\d+)$/i
      i = ::MultimediaParadise::Video.return_good_the_simpsons_episodes.shuffle
      n_files_to_play = $1.to_s.dup.to_i
      play_these_episodes = i[0 .. (n_files_to_play - 1)]
      notify_the_user_that_these_simpsons_episodes_will_be_played(play_these_episodes)
    # ===================================================================== #
    # === mplayer_wrapper --simpsons
    # ===================================================================== #
    when /^-?-?simpsons?\??$/i
      # =================================================================== #
      # Obtain all good simpsons episodes next.
      # =================================================================== #
      i = ::MultimediaParadise::Video.return_good_the_simpsons_episodes.shuffle # We play the default simpsons movies here.
      # =================================================================== #
      # Also add two random simpson videos, since as of 06.07.2019, if
      # we are on roebe.
      # =================================================================== #
      if is_on_roebe?
        i << MultimediaParadise.return_path_to_random_simpsons_video_file
        i << MultimediaParadise.return_path_to_random_simpsons_video_file
        i.uniq! # And make the list unique.
      end
      notify_the_user_that_these_simpsons_episodes_will_be_played(i)
    # ===================================================================== #
    # === --use-mplayer
    #
    # This entry point allows the user to specifically overrule
    # the multimedia-player in use.
    #
    # Invocation example:
    #
    #   mplaywrapper --use-mplayer --simpsons
    #
    # ===================================================================== #
    when /^-?-?use(-|_)?mplayer$/i
      @use_this_multimedia_player = 'mplayer'
    # ===================================================================== #
    # === :halloween
    #
    # This entry point specifically handles Symbols and Symbol-looking
    # Strings.
    # ===================================================================== #
    when :halloween, ':halloween',
         :predator,  ':predator'
      add map_symbol_to_locally_existing_file(i)
    # ===================================================================== #
    # === -nosound
    # ===================================================================== #
    when '-nosound',
         'silence',
         /^nosound$/i
      @append_this << '-nosound '
      i[1] = nil
    # ===================================================================== #
    # === mplayer_wrapper --version
    # ===================================================================== #
    when /^-?-?version\??$/i,
         '-v',
         '?'
      show_version_then_exit
    # ===================================================================== #
    # === --help
    # ===================================================================== #
    when /^-?-?help$/i
      show_help_then_exit
    else
      if File.exist? i
        add(i)
      else
        return i
      end
    end
  end
end

#notify_the_user_that_these_simpsons_episodes_will_be_played(i) ⇒ Object

#

notify_the_user_that_these_simpsons_episodes_will_be_played

#


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 308

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) ⇒ Object

#

play_each_multimedia_file

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

#


417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 417

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) ⇒ Object

#

register_file

#


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 224

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_sigintObject

#

register_sigint

#


286
287
288
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 286

def register_sigint # Register at-exit handler.
  Signal.trap('SIGINT') { exit_program }
end

#resetObject

#

reset

#


79
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
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 79

def reset
  super()
  infer_the_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 = false
  # ======================================================================= #
  # === @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_stringObject

#

reset_main_string

#


130
131
132
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 130

def reset_main_string
  @_ = ''.dup # This is the sys-command to use, the mplayer string.
end

#runObject

#

run

#


477
478
479
480
481
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 477

def run
  menu
  consider_to_notify_the_user_if_more_than_one_video_file
  play_each_multimedia_file
end

#sanitize_input(i) ⇒ Object

#

sanitize_input

We need to be careful here.

#


166
167
168
169
170
171
172
173
174
175
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 166

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) ⇒ Object

#

save_what_into_this_file

Use this to save the file played.

#


250
251
252
253
254
255
256
257
258
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 250

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) ⇒ Object Also known as: set_use_these_files

#

set_play_these_multimedia_files

This will always return an Array.

#


198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 198

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

#show_help_then_exitObject

#

show_help_then_exit

#


146
147
148
149
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 146

def show_help_then_exit
  esystem "#{@use_this_multimedia_player} --help"
  exit
end

#show_version_then_exitObject

#

show_version_then_exit

#


154
155
156
157
158
159
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 154

def show_version_then_exit
  _ = `#{@use_this_multimedia_player} -version 2>&1`
  _ = _.split("\n")[-1]
  e(_)
  exit
end

#try_to_ensure_that_the_files_existObject

#

try_to_ensure_that_the_files_exist

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

#


183
184
185
186
187
188
189
190
191
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 183

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_exitObject

#

verbose_exit

#


137
138
139
140
141
# File 'lib/multimedia_paradise/video/mplayer_wrapper.rb', line 137

def verbose_exit
  opnn; e 'You did not provide a multimedia file to play.'
  opnn; e 'Thus exiting now.'
  exit_program
end