api.cliff.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * Some CLI option templates
  32. */
  33. protected $transportTemplate = '';
  34. protected $recordOpts = '';
  35. protected $audioCapture = '';
  36. protected $liveOptsPrefix = '';
  37. protected $liveOptsSuffix = '';
  38. /**
  39. * Current instance ffmpeg version as three-digits integer
  40. *
  41. * @var int
  42. */
  43. protected $ffmpegVersion = 440;
  44. /**
  45. * Creates new CliFF instance
  46. */
  47. public function __construct() {
  48. $this->loadConfigs();
  49. $this->setVersion();
  50. $this->setTemplates();
  51. }
  52. /**
  53. * Loads some required configs
  54. *
  55. * @global $ubillingConfig
  56. *
  57. * @return void
  58. */
  59. protected function loadConfigs() {
  60. global $ubillingConfig;
  61. $this->binPaths = $ubillingConfig->getBinpaths();
  62. $this->altCfg = $ubillingConfig->getAlter();
  63. $this->ffmpgPath = $this->binPaths['FFMPG_PATH'];
  64. $this->chunkTime = $this->altCfg['RECORDER_CHUNK_TIME'];
  65. }
  66. /**
  67. * Detects current instance ffmpeg version and sets it into property
  68. *
  69. * @return void
  70. */
  71. protected function setVersion() {
  72. $command = $this->ffmpgPath . ' -version';
  73. $rawResult = shell_exec($command);
  74. if (!empty($rawResult)) {
  75. $rawResult = explodeRows($rawResult);
  76. if (isset($rawResult[0])) {
  77. $firstLine = $rawResult[0];
  78. if (!empty($firstLine) AND ispos($firstLine, 'ffmpeg version')) {
  79. $rawVersion = substr($firstLine, 15, 5);
  80. if (!empty($rawVersion)) {
  81. $this->ffmpegVersion = ubRouting::filters($rawVersion, 'int');
  82. }
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * Sets some ffmpeg cli options templates
  89. *
  90. * @return void
  91. */
  92. protected function setTemplates() {
  93. //defaults which works with ffmpeg 4.4.2
  94. $this->transportTemplate = '-stimeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  95. $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';
  96. $this->audioCapture = '-acodec copy' . ' ';
  97. $this->liveOptsPrefix = '-stimeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  98. $this->liveOptsSuffix = '-strict -2 -vcodec copy -hls_wrap 10';
  99. // burn cpu burn! lol
  100. //$this->liveOptsSuffix = '-strict -2 -vcodec libx264 -preset ultrafast -hls_wrap 10';
  101. //some ffmpeg >=5.0 opts
  102. if ($this->ffmpegVersion >= 500) {
  103. //stimeout option deprecated and replaced with timeout
  104. $this->transportTemplate = '-timeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  105. $this->liveOptsPrefix = '-timeout 5000000 -loglevel error -rtsp_transport tcp -f rtsp -i';
  106. //hls_wrap option deprecated too
  107. $this->liveOptsSuffix = '-strict -2 -vcodec copy -hls_flags delete_segments -hls_list_size 10 -segment_wrap 10';
  108. }
  109. }
  110. /**
  111. * Returns RTSP transport template
  112. *
  113. * @return string
  114. */
  115. public function getTransportTemplate() {
  116. return($this->transportTemplate);
  117. }
  118. /**
  119. * Returns recorder options template
  120. *
  121. * @return string
  122. */
  123. public function getRecordOpts() {
  124. return($this->recordOpts);
  125. }
  126. /**
  127. * Returns optional audio capture template
  128. *
  129. * @return string
  130. */
  131. public function getAudioCapture() {
  132. return ($this->audioCapture);
  133. }
  134. /**
  135. * Returns live-preview hls stream prefix template
  136. *
  137. * @return string
  138. */
  139. public function getLiveOptsPrefix() {
  140. return ($this->liveOptsPrefix);
  141. }
  142. /**
  143. * Returns live-preview hls stream suffix template
  144. *
  145. * @return string
  146. */
  147. public function getLiveOptsSuffix() {
  148. return ($this->liveOptsSuffix);
  149. }
  150. }