public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 314 315 316 317 318 319 320 321 322 323 324 325 326 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 411 412 413 414 415 416 417
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.