/package/app/app/infra/mediaInfoParser/KMediaInfoMediaParser.php

https://github.com/richhl/kalturaCE · PHP · 256 lines · 213 code · 17 blank · 26 comment · 31 complexity · f6a90796df18d87f24bddee20234e058 MD5 · raw file

  1. <?php
  2. /**
  3. * @package infra
  4. * @subpackage Media
  5. */
  6. class KMediaInfoMediaParser extends KBaseMediaParser
  7. {
  8. protected $cmdPath;
  9. /**
  10. * @param string $filePath
  11. * @param string $cmdPath
  12. */
  13. public function __construct($filePath, $cmdPath = 'mediainfo')
  14. {
  15. $this->cmdPath = $cmdPath;
  16. parent::__construct($filePath);
  17. }
  18. protected function getCommand()
  19. {
  20. return "{$this->cmdPath} \"{$this->filePath}\"";
  21. }
  22. protected function parseOutput($output)
  23. {
  24. $tokenizer = new KStringTokenizer ( $output, "\t\n" );
  25. $mediaInfo = new KalturaMediaInfo();
  26. $mediaInfo->rawData = $output;
  27. $fieldCnt = 0;
  28. $section = "general";
  29. $sectionID = 0;
  30. while ($tokenizer->hasMoreTokens())
  31. {
  32. $tok = strtolower(trim($tokenizer->nextToken()));
  33. if (strrpos($tok, ":") == false)
  34. {
  35. $sectionID = strchr($tok,"#");
  36. if($sectionID) {
  37. $sectionID = trim($sectionID,"#");
  38. }
  39. else
  40. $sectionID = 0;
  41. if(strstr($tok,"general")==true)
  42. $section = "general";
  43. else if(strstr($tok,"video")==true)
  44. $section = "video";
  45. else if(strstr($tok,"audio")==true)
  46. $section = "audio";
  47. // else if(strstr($tok,"image")==true)
  48. // $section = "image";
  49. else
  50. $section = $tok;
  51. }
  52. else if($sectionID<=1)
  53. {
  54. $key = trim(substr($tok, 0, strpos($tok, ":")));
  55. $val = trim(substr(strstr($tok, ":"), 1));
  56. switch ($section)
  57. {
  58. case "general" :
  59. $this->loadContainerSet($mediaInfo, $key, $val);
  60. break;
  61. case "video" :
  62. $this->loadVideoSet($mediaInfo, $key, $val);
  63. break;
  64. case "audio" :
  65. $this->loadAudioSet($mediaInfo, $key, $val);
  66. break;
  67. }
  68. $fieldCnt++;
  69. }
  70. }
  71. // On no-content return null
  72. if($fieldCnt<5)
  73. return null;
  74. else
  75. return $mediaInfo;
  76. }
  77. /**
  78. * @param $mediaInfo
  79. * @param string $key
  80. * @param string $val
  81. */
  82. private function loadAudioSet(KalturaMediaInfo $mediaInfo, $key, $val)
  83. {
  84. switch($key)
  85. {
  86. case "format":
  87. $mediaInfo->audioFormat = $val;
  88. break;
  89. case "codec id":
  90. $mediaInfo->audioCodecId = $val;
  91. break;
  92. case "duration":
  93. $mediaInfo->audioDuration = $this->convertDuration2msec($val);
  94. break;
  95. case "bit rate":
  96. $mediaInfo->audioBitRate = $this->convertValue2kbits($this->trima($val));
  97. break;
  98. case "bit rate mode":
  99. $mediaInfo->audioBitRateMode; // FIXME
  100. break;
  101. case "channel(s)":
  102. $mediaInfo->audioChannels = (int)$this->trima($val);
  103. break;
  104. case "sampling rate":
  105. $mediaInfo->audioSamplingRate = (float)$this->trima($val);
  106. if ($mediaInfo->audioSamplingRate < 1000)
  107. $mediaInfo->audioSamplingRate *= 1000;
  108. break;
  109. case "resolution":
  110. $mediaInfo->audioResolution = (int)$this->trima($val);
  111. break;
  112. }
  113. }
  114. /**
  115. * @param $mediaInfo
  116. * @param string $key
  117. * @param string $val
  118. */
  119. private function loadVideoSet(KalturaMediaInfo $mediaInfo, $key, $val)
  120. {
  121. switch($key)
  122. {
  123. case "format":
  124. $mediaInfo->videoFormat = $val;
  125. break;
  126. case "codec id":
  127. $mediaInfo->videoCodecId = $val;
  128. break;
  129. case "duration":
  130. $mediaInfo->videoDuration = $this->convertDuration2msec($val);
  131. break;
  132. case "bit rate":
  133. $mediaInfo->videoBitRate = $this->convertValue2kbits($this->trima($val));
  134. break;
  135. case "bit rate mode":
  136. $mediaInfo->videoBitRateMode; // FIXME
  137. break;
  138. case "width":
  139. $mediaInfo->videoWidth = (int)$this->trima($val);
  140. break;
  141. case "height":
  142. $mediaInfo->videoHeight = (int)$this->trima($val);
  143. break;
  144. case "frame rate":
  145. $mediaInfo->videoFrameRate = (float)$this->trima($val);
  146. break;
  147. case "display aspect ratio":
  148. $val = $this->trima($val);
  149. if(strstr($val, ":")==true){
  150. $darW = trim(substr($val, 0, strpos($val, ":")));
  151. $darH = trim(substr(strstr($val, ":"),1));
  152. if($darW>0)
  153. $mediaInfo->videoDar = $darW / $darH;
  154. else
  155. $mediaInfo->videoDar = null;
  156. }
  157. else if(strstr($val, "/")==true){
  158. $darW = trim(substr($val, 0, strpos($val, "/")));
  159. $darH = trim(substr(strstr($val, "/"),1));
  160. if($darW>0)
  161. $mediaInfo->videoDar = $darW / $darH;
  162. else
  163. $mediaInfo->videoDar = null;
  164. }
  165. else if($val) {
  166. $mediaInfo->videoDar = (float)$val;
  167. }
  168. break;
  169. case "rotation":
  170. $mediaInfo->videoRotation = (int)$this->trima($val);
  171. break;
  172. case "scan type":
  173. $scanType = $this->trima($val);
  174. if($scanType!="progressive") {
  175. $mediaInfo->scanType=1;
  176. }
  177. else {
  178. $mediaInfo->scanType=0;
  179. }
  180. break;
  181. }
  182. }
  183. /**
  184. * @param $mediaInfo
  185. * @param $key
  186. * @param $val
  187. */
  188. private function loadContainerSet(KalturaMediaInfo $mediaInfo, $key, $val)
  189. {
  190. switch($key)
  191. {
  192. case "file size":
  193. $mediaInfo->fileSize = $this->convertValue2kbits($this->trima($val));
  194. break;
  195. case "format":
  196. $mediaInfo->containerFormat = $val;
  197. break;
  198. case "codec id":
  199. $mediaInfo->containerId = $val;
  200. break;
  201. case "duration":
  202. $mediaInfo->containerDuration = $this->convertDuration2msec($val);
  203. break;
  204. case "overall bit rate":
  205. $mediaInfo->containerBitRate = $this->convertValue2kbits($this->trima($val));
  206. break;
  207. }
  208. }
  209. private function trima($str)
  210. {
  211. $str = str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $str);
  212. return $str;
  213. }
  214. private function convertDuration2msec($str)
  215. {
  216. preg_match_all("/(([0-9]*)h ?)?(([0-9]*)mn ?)?(([0-9]*)s ?)?(([0-9]*)ms ?)?/",
  217. $str, $res);
  218. $hour = @$res[2][0];
  219. $min = @$res[4][0];
  220. $sec = @$res[6][0];
  221. $msec = @$res[8][0];
  222. $rv = ($hour*3600 + $min*60 + $sec)*1000 + $msec;
  223. return (int)$rv;
  224. }
  225. private function convertValue2kbits($str)
  226. {
  227. preg_match_all("/(([0-9.]*)b ?)?(([0-9.]*)k ?)?(([0-9.]*)m ?)?(([0-9.]*)g ?)?/",
  228. $str, $res);
  229. if(@$res[2][0]!=="")
  230. $kbps=@$res[2][0]/1024;
  231. else if(@$res[4][0]!=="")
  232. $kbps=@$res[4][0];
  233. else if(@$res[6][0]!=="")
  234. $kbps=@$res[6][0]*1024;
  235. else if(@$res[8][0]!=="")
  236. $kbps=@$res[8][0]*1048576;
  237. return (float)$kbps;
  238. }
  239. }