Guest User

Xmoov flv streaming php script

a guest
Mar 5th, 2012
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //------------------------------------------------------------------------------------------
  3. // MEDIA PATH
  4. //------------------------------------------------------------------------------------------
  5. // you can configure these settings to point to video files outside the public html folder.
  6. //
  7. // points to server root
  8. define('XMOOV_PATH_ROOT', '');
  9. //
  10. // points to the folder containing the video files.
  11. define('XMOOV_PATH_FILES', './');
  12.  
  13. //------------------------------------------------------------------------------------------
  14. // BEHAVIOR
  15. //------------------------------------------------------------------------------------------
  16. //
  17. //set to TRUE to use bandwidth limiting.
  18. define('XMOOV_CONF_LIMIT_BANDWIDTH', FALSE);
  19. //
  20. //set to FALSE to prohibit caching of video files.
  21. define('XMOOV_CONF_ALLOW_FILE_CACHE', TRUE);
  22.  
  23. //------------------------------------------------------------------------------------------
  24. // BANDWIDTH SETTINGS
  25. //------------------------------------------------------------------------------------------
  26. // these settings are only needed when using bandwidth limiting.
  27. //
  28. // bandwidth is limited my sending a limited amount of video data(XMOOV_BW_PACKET_SIZE),
  29. // in specified time intervals(XMOOV_BW_PACKET_INTERVAL).
  30. // avoid time intervals over 1.5 seconds for best results.
  31. //
  32. // you can also control bandwidth limiting via http command using your video player.
  33. // the function getBandwidthLimit($part) holds three preconfigured presets(low, mid, high),
  34. // which can be changed to meet your needs
  35. //
  36. //set how many kilobytes will be sent per time interval
  37. define('XMOOV_BW_PACKET_SIZE', 90);
  38. //
  39. //set the time interval in which data packets will be sent in seconds.
  40. define('XMOOV_BW_PACKET_INTERVAL', 0.3);
  41. //
  42. //set to TRUE to control bandwidth externally via http.
  43. define('XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH', TRUE);
  44.  
  45. //------------------------------------------------------------------------------------------
  46. // INCOMING GET VARIABLES CONFIGURATION
  47. //------------------------------------------------------------------------------------------
  48. //
  49. // use these settings to configure how video files, seek position and bandwidth settings are
  50. // accessed by your player
  51. //
  52. define('XMOOV_GET_FILE', 'file');
  53. define('XMOOV_GET_POSITION', 'pos');
  54. define('XMOOV_GET_AUTHENTICATION', 'key');
  55. define('XMOOV_GET_BANDWIDTH', 'bw');
  56.  
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. // END SCRIPT CONFIGURATION - do not change anything beyond this point if you do not know what you are doing //
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60.  
  61. //------------------------------------------------------------------------------------------
  62. // PROCESS FILE REQUEST
  63. //------------------------------------------------------------------------------------------
  64.  
  65. if(isset($_GET[XMOOV_GET_FILE]))
  66. {
  67. // PROCESS VARIABLES
  68. // get seek position - JWMP doesn't send pos on the first request
  69. $seekPos = isset($_GET[XMOOV_GET_POSITION]) ? intval($_GET[XMOOV_GET_POSITION]) : 0;
  70. // get file name
  71. $fileName = htmlspecialchars($_GET[XMOOV_GET_FILE]);
  72. // assemble file path
  73. $file = XMOOV_PATH_ROOT . XMOOV_PATH_FILES . $fileName;
  74. // assemble packet interval
  75. $packet_interval = (XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH && isset($_GET[XMOOV_GET_BANDWIDTH])) ? getBandwidthLimit('interval') : XMOOV_BW_PACKET_INTERVAL;
  76. // assemble packet size
  77. $packet_size = ((XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH && isset($_GET[XMOOV_GET_BANDWIDTH])) ? getBandwidthLimit('size') : XMOOV_BW_PACKET_SIZE) * 1042;
  78.  
  79. // security improved by by TRUI www.trui.net
  80. if (!file_exists($file))
  81. {
  82. print('<b>ERROR:</b> xmoov-php could not find (' . $fileName . ') please check your settings.');
  83. exit();
  84. }
  85.  
  86. if(file_exists($file) && strrchr($fileName, '.') == '.flv' && strlen($fileName) > 2 && !eregi(basename($_SERVER['PHP_SELF']), $fileName) && ereg('^[^./][^/]*$', $fileName))
  87. {
  88. $fh = fopen($file, 'rb') or die ('<b>ERROR:</b> xmoov-php could not open (' . $fileName . ')');
  89.  
  90. $fileSize = filesize($file) - (($seekPos > 0) ? $seekPos + 1 : 0);
  91.  
  92. // SEND HEADERS
  93. if(!XMOOV_CONF_ALLOW_FILE_CACHE)
  94. {
  95. // prohibit caching (different methods for different clients)
  96. session_cache_limiter("nocache");
  97. header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
  98. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  99. header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  100. header("Pragma: no-cache");
  101. }
  102.  
  103. // content headers
  104. header("Content-Type: video/x-flv");
  105. // header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");
  106. header("Content-Length: " . $fileSize);
  107.  
  108. // FLV file format header
  109. if($seekPos != 0)
  110. {
  111. print('FLV');
  112. print(pack('C', 1));
  113. print(pack('C', 1));
  114. print(pack('N', 9));
  115. print(pack('N', 9));
  116. }
  117.  
  118. // seek to requested file position
  119. fseek($fh, $seekPos);
  120.  
  121. // output file
  122. while(!feof($fh))
  123. {
  124. // use bandwidth limiting - by Terry
  125. if(XMOOV_CONF_LIMIT_BANDWIDTH)
  126. {
  127. // get start time
  128. list($usec, $sec) = explode(' ', microtime());
  129. $time_start = ((float)$usec + (float)$sec);
  130. // output packet
  131. print(fread($fh, $packet_size));
  132. // get end time
  133. list($usec, $sec) = explode(' ', microtime());
  134. $time_stop = ((float)$usec + (float)$sec);
  135. // wait if output is slower than $packet_interval
  136. $time_difference = $time_stop - $time_start;
  137. if($time_difference < (float)$packet_interval)
  138. {
  139. usleep((float)$packet_interval * 1000000 - (float)$time_difference * 1000000);
  140. }
  141. }
  142. else
  143. {
  144. // output file without bandwidth limiting
  145. while (!feof($fh))
  146. {
  147. print(fread($fh, 16384));
  148. }
  149. }
  150. }
  151. }
  152. }
  153.  
  154. //------------------------------------------------------------------------------------------
  155. // DYNAMIC BANDWIDTH CONTROL
  156. //------------------------------------------------------------------------------------------
  157. //
  158. function getBandwidthLimit($part)
  159. {
  160. switch($part)
  161. {
  162. case 'interval' :
  163. switch($_GET[XMOOV_GET_BANDWIDTH])
  164. {
  165. case 'low' :
  166. return 1;
  167. break;
  168. case 'mid' :
  169. return 0.5;
  170. break;
  171. case 'high' :
  172. return 0.3;
  173. break;
  174. default :
  175. return XMOOV_BW_PACKET_INTERVAL;
  176. break;
  177. }
  178. break;
  179. case 'size' :
  180. switch($_GET[XMOOV_GET_BANDWIDTH])
  181. {
  182. case 'low' :
  183. return 10;
  184. break;
  185. case 'mid' :
  186. return 40;
  187. break;
  188. case 'high' :
  189. return 90;
  190. break;
  191. default :
  192. return XMOOV_BW_PACKET_SIZE;
  193. break;
  194. }
  195. break;
  196. }
  197. }
  198.  
  199. ?>
RAW Paste Data