Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/System/Executables/ruby -w
# =========================================================================== #
# require 'x/VID/VideoPlayer.rb'
# =========================================================================== #
# Description:
# This is a little ruby code wrapper over mplayer. We can use it to
# play a video file from a USB device for instance. We can also
# use it to play sub-groups of Videos. See the usage examples at
# the end.
# =========================================================================== #
# Invocation Examples:
# rivd branca l # play Branca Leone, and then continue to loop
# rvid branca 551 # play via -ss 551
# =========================================================================== #
require 'x/VID/find_video.rb'
require 'x/STD/std_video.rb'
require 'x/VID/mplayer.rb'
require 'ansi_colours' # bl $RUBY_COLOURS/ansi_col*.rb
class VideoPlayer
include StdVideo
include AnsiColours # For some colours.
N = "\n"
T = true
F = false
BE_VERBOSE = T
DEBUG = F
WHICH_VIDEO_PLAYER_TO_USE = 'mplayer' # DATA.read
DEFAULT_COLOUR = 'green'
FANCY_COLOUR = 'cyanb'
COLOUR_WARN = 'redb'
# ========================================================================= #
# === initialize
# ========================================================================= #
def initialize(play_mode = nil, run_already = true)
set_be_verbose(BE_VERBOSE)
register_shutdown
initialize_basic_variables
set_play_mode(play_mode)
set_runmode('standalone')
find_all_matches
register_sigint
check_play_mode(@play_mode) if run_already
end
# ========================================================================= #
# === initialize_basic_variables
# ========================================================================= #
def initialize_basic_variables
@video_player = WHICH_VIDEO_PLAYER_TO_USE
@extra_args = nil
@play_this = nil
@debug = DEBUG
@mount = ENV['MOUNT'] # HSS['MOUNT']
@shall_we_exit = F
@disable_video = F
@use_wrapper_over_mplayer = F
end; alias reset initialize_basic_variables
# ========================================================================= #
# === set_be_verbose
# ========================================================================= #
def set_be_verbose(i)
@be_verbose = i
end
# ========================================================================= #
# === set_play_mode
# ========================================================================= #
def set_play_mode(i = 'loop')
i = 'loop' if i.nil?
@play_mode = i
end
# ========================================================================= #
# === find_all_matches
# Fills up the variable @matches.
# ========================================================================= #
def find_all_matches(my_base_to_use = 'Realvid')
case my_base_to_use.to_s # for this menu, use only ONE entry per line.
when 'mma'
my_base_to_use = 'Mma'
when 'simpsons'
my_base_to_use = 'Cartoons/Simpsons'
when 'married'
my_base_to_use = 'MarriedWithChildren'
when 'breakdance'
my_base_to_use = 'Breakdance'
end
matches = Dir[ @mount+'/USB*/**' ].reject {|f| f !~ /Vid/}.first
if matches
e ' :: Retrieving video files next:'+N if @be_verbose
path = matches+'/'+my_base_to_use+'*/**'
_ = path
e _ if @debug
matches = Dir[_].reject {|f| ! is_video_file?(f) } # is_video_file? is in bl $RUBY_STD/std_video.rb
end
@matches = matches # should be an array of video files.
if @matches.nil?
warn_then_exit report_filename+'@matches is empty. Is there an USB device mounted?'
end
end
# ========================================================================= #
# === disable_video
# Use this method to disable video playback. You still get the audio output
# of the video file of course.
# ========================================================================= #
def disable_video
@disable_video = T
end
# ========================================================================= #
# === check_play_mode (case tag)
# Note that the argument `play_mode` must be a string. In case it includes
# a ' ' we will treat everything after the first ' ' as input.
# ========================================================================= #
def check_play_mode(play_mode = 'loop')
play_mode = play_mode.to_s.downcase
if play_mode.include? ' '
splitted = play_mode.split ' '
play_mode = splitted[0]
@extra_args = splitted[1..-1].to_s
end
if @extra_args =~ /\d+/ # If it contains only numbers.
@video_player << ' -ss '+@extra_args
@extra_args = nil
end
fancy "DER PLAY MODE WAR `#{play_mode}`." if @debug
case play_mode
when ''
# Purposely do nothing here.
when 'rand'
play_random_video_file
when 'help?','help','hel','h','h?','?'
feedback_help
when 'no_video','novideo','novid','nov','no'
disable_video
when 'listing','listing?'
feedback_listing
when 'loop','loo','lo','l','default',nil
play_video_files_in_loop
when 'xx','sex','sexy','s'
find_matches_then_play_video_in_loop(:sex, 'Sexy Clips')
when 'mma','m','combat'
find_matches_then_play_video_in_loop(:mma, 'Mixed Martial Arts')
when 'married','withchildren'
find_matches_then_play_video_in_loop(:married, 'Married with Children')
when 'simpsons','s','simp','simpson','cartoons','cartoon'
find_matches_then_play_video_in_loop(:simpsons, 'Simpsons')
when 'bdance','breakdance'
find_matches_then_play_video_in_loop(:breakdance, 'Breakdance')
else # else assume grep
# match = downcased.grep(/#{play_mode}/)
# @play_this = @array_video_files.grep(/#{$MATCH}/).first
results = find_video(play_mode)
# ================================================================ #
# If the user inputted a number, we go here.
# ================================================================ #
if results.is_a? Integer
results = @matches.grep(/#{results}/)
end
if results # May contain the number of the video file in question.
results.each { |r|
r = expand_to_proper_number(r)
if r.include? '/'
matched = r
else
matched = @matches.grep(/#{r}_/)
end
play_video_file(matched)
}
# ================================================================ #
# Now we handle extra arguments. If we passed l or loop then we
# will loop. Also rand.
# ================================================================ #
case @extra_args
when 'l','loop','rand','random'
play_video_files_in_loop
end
else
e 'Results is empty, we did not find any result. Exiting now.'
end
exit_program
end
end; alias run check_play_mode
# ========================================================================= #
# === feedback_listing
# ========================================================================= #
def feedback_listing
feedback_help('')
end
# ========================================================================= #
# === feedback_help (help tag)
# Called via the keywords:
# help? help hel h h? ?
# ========================================================================= #
def feedback_help(prepend_this = 'Help requested. ')
limit = 88
_ = prepend_this+'Available files are:'+N
e _
_ = @matches.map {|_| File.basename(_)}
_ = _.join(', ').wrap_at(limit)
_ = _.split(N).join(N+' ')
# Since Oct 2011, we colourize this a little bit.
_ = _.gsub(/(\d+)_/,GREEN+'\1_'+BOLD_BLUE)
e ' '+_
end
# ========================================================================= #
# === expand_to_proper_number
# ========================================================================= #
def expand_to_proper_number(i)
i = '%03d' % i.to_s if i.to_s.size < 4
i
end
# ========================================================================= #
# === find_video
# This will be called only from the method .check_play_mode
# ========================================================================= #
def find_video(i)
e 'Trying to find a video for the search term `'+i+'` now.' if @be_verbose
if i =~ /^\d+$/ # only numbers given.
results = i.to_i
else
_ = FindVideo.new(i) # bl
_.set_debug if @debug
_.find_video
results = _.results
end
e "The results were: `#{results}`" if @debug
results
end
# ========================================================================= #
# === report_filename
# Simply prepend the filename.
# ========================================================================= #
def report_filename
return File.basename(__FILE__)+': '
end
# ========================================================================= #
# === find_matches_then_play_video_in_loop
# A wrapper method.
# ========================================================================= #
def find_matches_then_play_video_in_loop(category, title = '')
e 'Playing Video Files from '+title+' now.'
find_all_matches(category)
play_video_files_in_loop
end
# ========================================================================= #
# === play_video_files_in_loop
# Play everything via a loop.
# ========================================================================= #
def play_video_files_in_loop
loop {
play_random_video_file
break if @shall_we_exit
}
end
# ========================================================================= #
# === play_random_video_file
# ========================================================================= #
def play_random_video_file
play_video_file(return_random_video)
end
# ========================================================================= #
# === play_video_file
# Use this method to play a video file. It assigns to the variable
# @play_this.
# ========================================================================= #
def play_video_file(i = nil)
i = return_random_video if i.nil?
if i.is_a? Array
i.each {|file| play_video_file(file) }
else
@play_this = i
if File.exists?(@play_this)
ef 'We will play this file next: '
fancy @play_this
save_last_video_file_played(@play_this)
my_system @play_this
else
warn 'But the file '+@play_this+' does not seem to exist.'
end
end
end
# ========================================================================= #
# === save_last_video_file_played
# This method saves which video file was last played. By default it stores
# into $HOME/last_video_file_players.
# ========================================================================= #
def save_last_video_file_played(i = @play_this)
store_where = ENV['HOME']+'/last_video_file_played'
e GREEN+'Storing at '+LIGHTBLUE+store_where
my_file = File.new(store_where, 'w+')
my_file.puts i
my_file.close
end
# ========================================================================= #
# === return_random_video
# Returns a random video.
# ========================================================================= #
def return_random_video
@matches[rand(@matches.size)]
end
# ========================================================================= #
# === register_shudown
# Will act upon SIGINT recieved, and shut down properly.
# ========================================================================= #
def register_shutdown
Signal.trap('INT') {
fancy 'Requested to exit from VideoPlayer.rb. Exiting now.'
exit_program
}
end
# ========================================================================= #
# Colour output and other output things.
# ========================================================================= #
# === warn_then_exit
# ========================================================================= #
def warn_then_exit(i)
e RED+i
exit_program
end
# ========================================================================= #
# === register_sigint
# Silently restore the prompt again here via reset.
# ========================================================================= #
def register_sigint
sigint_proc = proc {
system 'reset'
@shall_we_exit = T
exit_program
}
Signal.trap 'SIGINT', sigint_proc
end
# ========================================================================= #
# === set_runmode
# Can be standalone, or connected. 's' is an alias to standalone.
# ========================================================================= #
def set_runmode(i)
i = i.to_s
case i
when 's'
i = 'standalone'
end
@runmode = i
end
# ========================================================================= #
# === my_system
# Wrapper towards running system(). Will padd the output. You can modify
# the variable @video_player.
# ========================================================================= #
def my_system(i)
e 'In my_system() the argument was '+i if @debug
if @use_wrapper_over_mplayer
Mplayer.new(i)
else # else we use system directly.
i = '"'+i+'"'
i << ' -vo null ' if @disable_video
i = @video_player+' '+i
e ' '+i
`#{i}`
end
end
# ========================================================================= #
# === exit_program
# Use only this method when you want to exit from this program.
# ========================================================================= #
def exit_program
case @runmode
when 'standalone'
exit
else # else pass through.
end
end
# ========================================================================= #
# Coloured stuff.
# ========================================================================= #
def fancy(i, make_newline = T); e i, FANCY_COLOUR, make_newline; end
def fancyf(i, make_newline = F); e i, FANCY_COLOUR, make_newline; end
def ef(i, colour = DEFAULT_COLOUR); e i, colour, F; end
alias e cme
end
video_player = VideoPlayer.new(ARGV.join(' ')) if __FILE__ == $PROGRAM_NAME
# rvid
# rvid simpsons
# rvid mma
# __END__
# mplayer # vlc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.