Advertisement
Guest User

raspistream::streamer

a guest
Jan 16th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This script takes files in a folder and reads them as they arrive
  5.  *
  6.  * It is used essentially to assemble MPEGTS in order to pipe
  7.  * the output in FFMPEG and then convert this input to OGG for example
  8.  *
  9.  * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3
  10.  * @package raspistream
  11.  * @see http://www.tmplab.org/wiki/index.php/Streaming_Video_With_RaspberryPi
  12.  * @author Alban Crommer
  13.  * @copyright (c) 2015
  14.  * @version 1.0
  15.  */
  16.  
  17. // --------------------------------------------------------------
  18. // User Tweakable parameters
  19. // --------------------------------------------------------------
  20.  
  21. // Defines how many digits your FFMPEG segments have
  22. // Ex: "/tmp/capture/out-0001.ts" => 4
  23. $digits                         = 10;
  24. // Defines the path to your segments to merge
  25. // Ex: "/tmp/capture/out-0001.ts" => "/tmp/capture"
  26. $path                           = "/tmp/capture/";
  27. // Defines a prefix for your segments ex "out-0001.ts"
  28. // Ex: "/tmp/capture/out-0001.ts" => "out-"
  29. $prefix                         = "";
  30. // Defines the extension of your segments ex "0001.ts"
  31. // Ex: "/tmp/capture/out-0001.ts" => "ts"
  32. $extension                      = "ts";
  33. // Defines a log file
  34. $log_file                       = "/tmp/ffmpeg.log";
  35. // Defines a number of files to send immediately if available for buffer
  36. $buffer_size                    = 5;
  37.  
  38. /**
  39.  *
  40.  * @global int $digits
  41.  * @global string $prefix
  42.  * @global string $extension
  43.  * @global string $path
  44.  * @param type $id
  45.  * @param string $path
  46.  * @return type
  47.  */
  48. function getChunkName($id, $path = ""){
  49.  
  50.     global $digits;
  51.     global $prefix;
  52.     global $extension;
  53.     global $path;
  54.     return $path.$prefix.str_pad($id+ 1, $digits, "0", STR_PAD_LEFT).".".$extension;
  55.  
  56. }
  57. /**
  58.  *
  59.  * @global string $log_file
  60.  * @param type $data
  61.  */
  62. function writeLog( $data ){
  63.     global $log_file;
  64.     file_put_contents( $log_file, $data."\n", FILE_APPEND);
  65. }
  66. /**
  67.  *
  68.  * @param type $file_path
  69.  */
  70. function displayChunk( $file_path ){
  71.     writeLog($file_path);
  72.     if( is_file ( $file_path ) ){
  73.         readfile( $file_path );
  74.     }else{
  75.         writeLog( "ERROR ! Invalid path ".$file_path );
  76.     }
  77. }
  78.  
  79. // Script start
  80.  
  81. // Attempt to read the directory content
  82. $fileList                       = array();
  83. $current                        = 0;
  84. $d                              = opendir($path);
  85. if ( ! $d) {
  86.     die( "Failed to load source directory $path content" );
  87. }
  88. while (($c                      = readdir($d))!==false) {
  89.         if (is_file($path."/".$c)) {
  90.                 $c              = intval($c);
  91.                 $fileList[]     = $c;
  92.         }
  93. }
  94. closedir($d);
  95.  
  96.  
  97. // Attempt to parse the file list and determine the current chunk
  98. sort($fileList);
  99. $current                        = 0;
  100. if( count($fileList) > 1 ){
  101.     $current                    = $fileList[count($fileList)-1];
  102. }
  103. if( count($fileList) <  $buffer_size ){
  104.     $buffer_size                = count( $fileList);
  105. }
  106. $fileList                       = array_slice($fileList,count($fileList)-$buffer_size,$buffer_size-1);
  107.  
  108. // Read the X latest files (fill the buffer in ...)
  109. writeLog("Filling the buffer");
  110. foreach($fileList as $k) {
  111.     displayChunk( getChunkName($k), $path );
  112. }
  113. $timeout                        = 60;
  114. $noactivity                     = 0;
  115.  
  116. // Start an endless stream loop
  117. while ( true ) {
  118.     $file_path                  = getChunkName($current);
  119.         if (file_exists($file_path)) {
  120.  
  121.             displayChunk( $file_path);
  122.                 // Update log record
  123.                 $current++;
  124.                 $noactivity             = 0;
  125.         } else {
  126.                 sleep(1);
  127.                 $noactivity++;
  128.                 writeLog( $file_path.":".$noactivity);
  129.                 // go home live, you're finished
  130.                 if ($noactivity >= $timeout) {
  131.                         die();
  132.                 }
  133.         }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement