this little script will use file random to get a video file and will use your player of choice to launch it. mediainfo.exe was used to get sleep count.

if you see anyway i can improve it please make a comment. i am trying to figure out how to push the filename to an array and do a check if it has played it in the last 100 videos/loops(that would help keep it from repeating if it does i think). i would also like to specify how many times to loop before exiting, that way it doesnt just loop forever and ever and the user can specify how many shows they want to watch.

i wasnt please with the way i had to get duration, but it worked and i was astounded, so if you have of a better was to get duration time, please let me know. the problem i was dealing with while getting duration with any tool was spaces in the path/filenames. Here is the code:
use strict; use warnings; use diagnostics; use File::Random qw(random_file); my $dir = $ARGV[0]; if ( not defined $dir ) { print "\nUsage: random.pl [folder]; exit(0); }else{ random($dir); } sub random{ my ($dir) = @_; while (1){ my $mpc = "C:/Program Files (x86)/K-Lite Codec Pack/Media +Player Classic/mpc-hc.exe"; my $rndm_file = random_file( -dir => $dir, #-check => qr/./, -recursive => 1 ); if ($rndm_file =~ /\.(ini|nfo|db)$/i){ print "$rndm_file\n"; random($dir); } print $rndm_file; #get duration my $t = ("MediaInfo.exe --Output=Video;%Duration% \"F:/TV/ +$rndm_file\""); system(1, $mpc, "F:/TV/$rndm_file"); my $time = qx($t); my $sleep_time = $time/1000; #in seconds because m +ediainfo.exe outputs milliseconds i think. print "\nDuration in seconds: $sleep_time\n"; sleep($sleep_time); random($dir); } }
this works great if you have a ton of media in a folder and want to randomly watch any given inside in that directory, and you do not have to worry about spaces in path/filenames. and this is also for windows but i think it could be used on any other platform as well with small changes

Replies are listed 'Best First'.
Re: Random video player
by toolic (Bishop) on Oct 29, 2014 at 18:21 UTC
    Your code doesn't compile. A double quote is missing:
    print "\nUsage: random.pl [folder]\n";

    Misc:

    • I get Can't exec "1": on your system line on linux.
    • pertidy
    • No need for diagnostics unless you are debugging.
    • Getopt::Long is a standard way to pass in options.
    • Add some documentation with POD and Pod::Usage
      i think in linux it uses a different option for system or exec to skip waiting. in windows it is passing "1" to system. but it does work good on windows 7 ultimate x64. i started this script last night and its still going fine :) I was unaware of getopt::long but i will def look into it. i was astounded i was even able to pass the arguments to mediainfo.exe like i did, but it did worked. i were also unaware of pod usage as well. thanks for pointing both of these out to me
Re: Random video player
by GotToBTru (Prior) on Oct 29, 2014 at 18:05 UTC

    If you don't have specific questions about something, such as why it works or why it doesn't work, but have working code that might benefit from examination, it would be more appropriately posted in Cool Uses for Perl.

    1 Peter 4:10
      oh ok, well i was hinting around as to a better or easier way to get duration, but thatnks, i will use that forum for stuff like this from here on out. thanks :)
      there is n oway that i know of to remove this thread, or i would go ahead and move this over to the forum your talking about.

        Yeah, that is something the janitors have to do. And, there's probably several people here would say it belongs where you posted it. As you said, something to keep in mind for future consideration.

        1 Peter 4:10