Advertisement
Guest User

video streamer

a guest
Jan 13th, 2015
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. $digits                         = 4;
  4. $path                           = "/tmp/capture/";
  5. $prefix                         = "";
  6. $extension                      = "ts";
  7. $log_file                       = "/tmp/ffmpeg.log";
  8. $buffer_size                    = 5;
  9.  
  10. function getChunkName($id, $path = ""){
  11.  
  12.     global $digits;
  13.     global $prefix;
  14.     global $extension;
  15.     global $path;
  16.     return $path.$prefix.str_pad($id+ 1, $digits, "0", STR_PAD_LEFT).".".$extension;
  17.  
  18. }
  19.  
  20. function writeLog( $data ){
  21.     global $log_file;
  22.     file_put_contents( $log_file, $data."\n", FILE_APPEND);
  23. }
  24.  
  25. function displayChunk( $file_path ){
  26.     writeLog($file_path);
  27.     if( is_file ( $file_path ) ){
  28.         readfile( $file_path );
  29.     }else{
  30.         writeLog( "ERROR ! Invalid path ".$file_path );
  31.     }
  32. }
  33.  
  34. $fileList                       = array();
  35. $current                        = 0;
  36. $d                              = opendir($path);
  37. if ( ! $d) {
  38.     die( "Failed to load source directory $path content" );
  39. }
  40. while (($c                      = readdir($d))!==false) {
  41.         if (is_file($path."/".$c)) {
  42.                 $c              = intval($c);
  43.                 $fileList[]     = $c;
  44.         }
  45. }
  46. closedir($d);
  47. sort($fileList);
  48. $current                        = 0;
  49. if( count($fileList) > 1 ){
  50.     $current                    = $fileList[count($fileList)-1];
  51. }
  52. if( count($fileList) <  $buffer_size ){
  53.     $buffer_size                = count( $fileList);
  54. }
  55. $fileList                       = array_slice($fileList,count($fileList)-$buffer_size,$buffer_size-1);
  56. // Now we read the X latest files (fill the buffer in ...)
  57. writeLog("Filling the buffer");
  58. foreach($fileList as $k) {
  59.     displayChunk( getChunkName($k), $path );
  60. }
  61. $timeout                        = 60;
  62. $noactivity                     = 0;
  63. // Endless stream loop
  64. while ( true ) {
  65.     $file_path                  = getChunkName($current);
  66.         if (file_exists($file_path)) {
  67.  
  68.             displayChunk( $file_path);
  69.                 // Update log record
  70.                 $current++;
  71.                 $noactivity             = 0;
  72.         } else {
  73.                 sleep(1);
  74.                 $noactivity++;
  75.                 writeLog( $file_path.":".$noactivity);
  76.                 // go home live, you're finished
  77.                 if ($noactivity >= $timeout) {
  78.                         die();
  79.                 }
  80.         }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement