Web Hosting Talk







View Full Version : Examples of PHP CLI scripting


apokalyptik
08-24-2002, 02:28 PM
This script takes all of the video files in a directory, and makes a xine (http://xine.sourceforge.net/) playlist out of them. Also optionaly gives some basic stats, randomizes the list, and actually runs xine with the playlist

useful because of xines lack of very-good playlist functionality


#!/usr/local/bin/php -f
<?php

## The verbose setting decides how much extra information the script will
## display. 0 runs the script silently, 1 displays some information about what
## was found, and 2 displays everythins that 1 does, PLUS the command used to
## start xine. 4 will _only_ display the command used to start xine (usefull
## for piping data around
$verbose=0;
## Runcmd specifies whether or not to actually RUN xine.
$runcmd=1;
## The path to your xine binary
$xine='/usr/bin/xine';
## The directory that your videos are stored in... this does not support recursion
## nor does it check to make sure all the files are valid video files
## so make sure this directory just contains movie files
$directory='/mnt/samba/videos/';
## By default this script will make a playlist which goes in the order in which
## the linux 'ls' command deems fit. If you are interested in a random order
## playlist, then set this to 1 instead of 0
$randomize=1;
## setting $validate to 1 will cause the script to make sure that the file

## being processed is a valid media file that xine should know how to play
$validate=1;
## media is a list of valid media extentions seperated by a space... this is
## only a useful option if $validate is set to 1
$media="mpg mpeg mp2 avi wmv asf vob m2v m2p";

### Probably not a good idea to edit past this point
#######################################################

$tmp=trim(`ls -1 $directory`);
$tmp=explode(chr(10), $tmp);

if ( $randomize == 1 ) {
srand ((float)microtime()*1000000);
shuffle($tmp);
}

$media=explode(' ', $media);
$cmd=$xine;
foreach ( $tmp as $idx => $val ) {
$ext=strrpos($val, '.') + 1;
$ext=substr($val, $ext);
if ( $validate == 1 && is_array($media) ) {
$valid=0;
foreach ( $media as $i => $v ) {
if ( strtolower(trim($v)) == strtolower(trim($ext)) ) {
$valid=1;
}
}
} else {
$valid=1;
}
if ( $valid == 1 ) {
$processed[$ext]++;
$cmd.=' \\'.chr(10).'"file:///mnt/samba/videos/'.str_replace('`','\\`',$val).'"';
}
}


if ( $verbose > 0 && $verbose != 4 && is_array($processed)) {
echo 'Found the following metrics:'.chr(10);
echo '----------------------------'.chr(10);
foreach ( $processed as $idx => $val ) {
echo $idx.' ('.$val.')'.chr(10);
}
echo '----------------------------'.chr(10);
}

if ( $verbose > 1 ) {
echo $cmd.chr(10);
}

if ( $runcmd == 1 ) {
`$cmd`;
}

?>


I wasn't entirely sure that this was an appropriate post, but it seems like something that people might be able to learn a bit from...