api.cliff.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * ffmpeg version-specific CLI options wrapper
  4. */
  5. class CliFF {
  6. /**
  7. * Contains alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains binpaths.ini config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $binPaths = array();
  18. /**
  19. * Contains ffmpeg binary path
  20. *
  21. * @var string
  22. */
  23. protected $ffmpgPath = '';
  24. /**
  25. * Default chunk time in seconds
  26. *
  27. * @var int
  28. */
  29. protected $chunkTime = 60;
  30. /**
  31. * Contains motion detection threshold
  32. *
  33. * @var int
  34. */
  35. protected $moDetThreshold = 2;
  36. /**
  37. * Contains motion detection time scale
  38. *
  39. * @var int
  40. */
  41. protected $moDetTimeScale = 15;
  42. /**
  43. * Contains motion detection options string
  44. *
  45. * @var string
  46. */
  47. protected $moDetOpts = '';
  48. /**
  49. * Some CLI option templates
  50. */
  51. protected $transportTemplate = '';
  52. protected $recordOpts = '';
  53. protected $audioCapture = '';
  54. protected $liveOptsPrefix = '';
  55. protected $liveOptsSuffix = '';
  56. /**
  57. * Current instance ffmpeg version as three-digits integer
  58. *
  59. * @var int
  60. */
  61. protected $ffmpegVersion = 440;
  62. /**
  63. * Creates new CliFF instance
  64. */
  65. public function __construct() {
  66. $this->loadConfigs();
  67. $this->setVersion();
  68. $this->setTemplates();
  69. }
  70. /**
  71. * Loads some required configs
  72. *
  73. * @global $ubillingConfig
  74. *
  75. * @return void
  76. */
  77. protected function loadConfigs() {
  78. global $ubillingConfig;
  79. $this->binPaths = $ubillingConfig->getBinpaths();
  80. $this->altCfg = $ubillingConfig->getAlter();
  81. $this->ffmpgPath = $this->binPaths['FFMPG_PATH'];
  82. $this->chunkTime = $this->altCfg['RECORDER_CHUNK_TIME'];
  83. }
  84. /**
  85. * Detects current instance ffmpeg version and sets it into property
  86. *
  87. * @return void
  88. */
  89. protected function setVersion() {
  90. $command = $this->ffmpgPath . ' -version';
  91. $rawResult = shell_exec($command);
  92. if (!empty($rawResult)) {
  93. $rawResult = explodeRows($rawResult);
  94. if (isset($rawResult[0])) {
  95. $firstLine = $rawResult[0];
  96. if (!empty($firstLine) and ispos($firstLine, 'ffmpeg version')) {
  97. $rawVersion = substr($firstLine, 15, 5);
  98. if (!empty($rawVersion)) {
  99. $this->ffmpegVersion = ubRouting::filters($rawVersion, 'int');
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Sets some ffmpeg cli options templates
  107. *
  108. * @return void
  109. */
  110. protected function setTemplates() {
  111. //defaults which works with ffmpeg 4.4.2
  112. $this->transportTemplate = '-stimeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  113. $this->recordOpts = '-strict -2 -vcodec copy -f segment -segment_time ' . $this->chunkTime . ' -strftime 1 -segment_atclocktime 1 -segment_clocktime_offset 30 -reset_timestamps 1 -segment_format mp4';
  114. $this->audioCapture = '-acodec copy' . ' ';
  115. $this->liveOptsPrefix = '-stimeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  116. $this->liveOptsSuffix = '-strict -2 -vcodec copy -hls_wrap 10';
  117. // burn cpu burn! lol
  118. //$this->liveOptsSuffix = '-strict -2 -vcodec libx264 -preset ultrafast -hls_wrap 10';
  119. //some ffmpeg >=5.0 opts
  120. if ($this->ffmpegVersion >= 500) {
  121. //stimeout option deprecated and replaced with timeout
  122. $this->transportTemplate = '-timeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  123. $this->liveOptsPrefix = '-timeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  124. //hls_wrap option deprecated too
  125. $this->liveOptsSuffix = '-strict -2 -vcodec copy -hls_flags delete_segments -hls_list_size 10 -segment_wrap 10';
  126. }
  127. }
  128. /**
  129. * Returns RTSP transport template
  130. *
  131. * @return string
  132. */
  133. public function getTransportTemplate() {
  134. return ($this->transportTemplate);
  135. }
  136. /**
  137. * Returns recorder options template
  138. *
  139. * @return string
  140. */
  141. public function getRecordOpts() {
  142. return ($this->recordOpts);
  143. }
  144. /**
  145. * Returns optional audio capture template
  146. *
  147. * @return string
  148. */
  149. public function getAudioCapture() {
  150. return ($this->audioCapture);
  151. }
  152. /**
  153. * Returns live-preview hls stream prefix template
  154. *
  155. * @return string
  156. */
  157. public function getLiveOptsPrefix() {
  158. return ($this->liveOptsPrefix);
  159. }
  160. /**
  161. * Returns live-preview hls stream suffix template
  162. *
  163. * @return string
  164. */
  165. public function getLiveOptsSuffix() {
  166. return ($this->liveOptsSuffix);
  167. }
  168. /**
  169. * Returns ffmpeg binary path
  170. *
  171. * @return string
  172. */
  173. public function getFFmpegPath() {
  174. return ($this->ffmpgPath);
  175. }
  176. /**
  177. * Sets alternate motion detection parameters
  178. *
  179. * @param int $threshold motion detection sensitivity 1..100
  180. * @param int $timeScale Change the PTS of the input frames (x)
  181. * @return void
  182. */
  183. public function setMoDetParams($threshold=2,$timeScale=15) {
  184. $this->moDetThreshold=ubRouting::filters($threshold,'int');
  185. $this->moDetTimeScale=ubRouting::filters($timeScale,'int');
  186. }
  187. /**
  188. * Sets and returns motion detection options
  189. *
  190. * @return string
  191. */
  192. public function getMoDetOpts() {
  193. $this->moDetOpts = '-loglevel error -vf "select=gt(scene\,' . round($this->moDetThreshold / 100, 3) . '),setpts=N/(' . $this->moDetTimeScale . '*TB)" ';
  194. return ($this->moDetOpts);
  195. }
  196. }