简体   繁体   中英

Play video file randomly from a folder recursively looking down into all directory structure tree

Some JavaScript, JQuery or PHP that will look into a folder recursively down into all directory structure tree, finds and loads a new video file into the html5 video tag source so it plays automatically each time when page is reloaded. A plus, when finish playing, jumps to another video file ramdomly & seamlessly.

This will work for one selected folder, but does not work down into all directory structure tree

<?php
$myVideoDir = '.';
$extension = 'mp4';
$videoFile = false;
$pseudoDir = scandir($myVideoDir);
$myitems = array();
$mycounter = 0;
foreach($pseudoDir as $item) {
    if ( $item != '..' && $item != '.' && !is_dir($item) ) {
        $ext = preg_replace('#^.*\.([a-zA-Z0-9]+)$#', '$1', $item);
        if ( $ext == $extension )
            $videoFile = $item;
            if ( $videoFile <> "" ) {
                $myitems[] = $videoFile;
                $mycounter = $mycounter + 1;
            }               
    }
}

$myrandom = rand(0,$mycounter-1);
if ( !!$videoFile ) {

    echo '<video id="dep" class="center" width="400" autoplay controls>        
            <source src="'.$myVideoDir.'/'.$myitems[$myrandom].'" type="video/mp4"> 
        </video>
    ';
}
?>

I would suggest the following code.

<?php
$myVideoDir = '.';
$extension = 'mp4';
$videoFile = false;
$pseudoDir = scandir($myVideoDir);
$myitems = array();
foreach($pseudoDir as $item) {
  if ( $item != '..' && $item != '.' && !is_dir($item) ) {
    $ext = preg_replace('#^.*\.([a-zA-Z0-9]+)$#', '$1', $item);
    if ( $ext == $extension ) {
      $videoFile = $item;
      if ( $videoFile <> "" ) {
        array_push($myitems, $videoFile);
      }               
    }
  }
}
$myrandom = rand(0,count($myitems)-1);
if ( !!$videoFile ) {
  echo '<video id="dep" class="center" width="400" autoplay controls><source src="'.$myVideoDir.'/'.$myitems[$myrandom].'" type="video/mp4"></video>';
}
?>

Update

Consider making a function.

<?php

function getFileList($dirPath, $ext){
  $list = scandir($dirPath);
  $fileList = array();
  foreach($list as $item) {
    if ($item != '..' && $item != '.' && !is_dir($item)) {
      $info = pathinfo($item);
      $videoFile = $item;
      if ($info['extension'] == $ext) {
        array_push($fileList, $item);
      }               
    }
  }
  return $fileList;
}

function pickRandVid($l){
  $r = rand(0, count($l) - 1);
  return $l[$r];
}

$myVideoDir = ".";
$dirList = scandir($myVideoDir);
$videoList = array()
foreach($dirList as $d){
  if(is_dir($d)){
    array_merge($videoList, getFileList($d, "mp4"));
  }
}

echo "<video id='dep' class='center' width='400' autoplay controls>\r\n";
echo "\t<source src='$myVideoDir/" . pickRandVid($videoList) . "' type='video/mp4' />\r\n";
echo "</video>\r\n";

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM