PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/spipmotion/trunk/inc/spipmotion_mediainfo.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 202 lines | 162 code | 7 blank | 33 comment | 51 complexity | a25c7363bcf14c8537d0bfbd260e478f MD5 | raw file
  1. <?php
  2. /**
  3. * SPIPmotion
  4. * Gestion de l'encodage et des métadonnées de vidéos directement dans spip
  5. *
  6. * Auteurs :
  7. * kent1 (http://www.kent1.info - kent1@arscenic.info)
  8. * 2008-2012 - Distribué sous licence GNU/GPL
  9. *
  10. */
  11. if (!defined('_ECRIRE_INC_VERSION')) return;
  12. /**
  13. * Récupération des métadonnées via MediaInfo
  14. *
  15. * @param string $chemin : le chemin du fichier à analyser
  16. * @return array $infos : un tableau des informations récupérées
  17. */
  18. function inc_spipmotion_mediainfo_dist($chemin){
  19. include_spip('inc/filtres');
  20. $infos = array();
  21. if(file_exists($chemin)){
  22. ob_start();
  23. passthru(escapeshellcmd("mediainfo -f --Output=XML $chemin"));
  24. $metadatas=ob_get_contents();
  25. ob_end_clean();
  26. include_spip('inc/xml');
  27. $arbre = spip_xml_parse($metadatas);
  28. spip_xml_match_nodes(",^track type,",$arbre, $tracks);
  29. foreach($tracks as $track => $info){
  30. $metas[$track] = $info;
  31. if($track == 'track type="General"'){
  32. spip_log($info[0],'test');
  33. $infos['titre'] = $info[0]['Title'][0] ? $info[0]['Title'][0] : ($info[0]['Movie_name'][0] ? $info[0]['Movie_name'][0] : $info[0]['Track_name '][0]);
  34. $infos['descriptif'] = $info[0]['Description'][0] ? $info[0]['Description'][0] : $info[0]['desc'][0];
  35. if($infos['descriptif'] == ''){
  36. if(isset($info[0]['Performer'][0]))
  37. $infos['descriptif'] .= utf8_encode($info[0]['Performer'][0])."\n";
  38. if(isset($info[0]['Album'][0]))
  39. $infos['descriptif'] .= utf8_encode($info[0]['Album'][0])."\n";
  40. if(isset($info[0]['Recorded_date'][0]))
  41. $infos['descriptif'] .= utf8_encode($info[0]['Recorded_date'][0])."\n";
  42. if(isset($info[0]['Genre'][0]))
  43. $infos['descriptif'] .= utf8_encode($info[0]['Genre'][0])."\n";
  44. if(isset($info[0]['Track_name_Position'][0]))
  45. $infos['descriptif'] .= $info[0]['Track_name_Position'][0].($info[0]['Track_name_Total'][0] ? '/'.$info[0]['Track_name_Total'][0]:'')."\n";
  46. if(isset($info[0]['Performer_Url'][0]))
  47. $infos['descriptif'] .= "\n".utf8_encode($info[0]['Performer_Url'][0])."\n";
  48. }
  49. $infos['credits'] .= $info[0]['Performer'][0]? $info[0]['Performer'][0].($info[0]['Copyright'][0] ? ' - '.$info[0]['Copyright'][0] : '') : $info[0]['Copyright'][0] ;
  50. $infos['duree'] = $info[0]['Duration'][0] / 1000;
  51. if(!$infos['duree'])
  52. $infos['duree'] = isset($info[0]['duration'][0]) ? (($info[0]['duration'][0] > 1000) ? ($info[0]['duration'][0]/1000) :$info[0]['duration'][0]) : '';
  53. $infos['bitrate'] = $info[0]['Overall_bit_rate'][0];
  54. $infos['encodeur'] = $info[0]['Writing_library'][0];
  55. if(!$infos['encodeur'])
  56. $infos['encodeur'] = $info[0]['Writing_application'][0];
  57. /**
  58. * Récupération de la cover
  59. */
  60. if($info[0]['Cover_Data'][0]){
  61. $mime = array_shift(explode(' ',$info[0]['Cover_MIME'][0]));
  62. switch ($mime) {
  63. case 'image/jpg':
  64. $ext = 'jpg';
  65. break;
  66. case 'image/png':
  67. $ext = 'png';
  68. break;
  69. case 'image/gif':
  70. $ext = 'gif';
  71. break;
  72. default:
  73. $ext = 'jpg';
  74. }
  75. $tmp_file = 'spipmotion-'.str_replace('.','_',str_replace(':','_',str_replace(' ','_',$infos['titre']))).'.'.$ext;
  76. $dest = sous_repertoire(_DIR_VAR, 'cache-spipmotion_logo');
  77. $dest = $dest.$tmp_file;
  78. if ($ok = ecrire_fichier($dest, base64_decode(array_shift(explode(' / ',$info[0]['Cover_Data'][0]))))){
  79. include_spip('inc/joindre_document');
  80. $ajouter_documents = charger_fonction('ajouter_documents', 'action');
  81. list($extension,$arg) = fixer_extension_document($dest);
  82. $cover_ajout = array(array('tmp_name'=>$dest,'name'=> basename($dest)));
  83. $ajoute = $ajouter_documents('new',$cover_ajout,'',0,'vignette');
  84. if (is_numeric(reset($ajoute))
  85. AND $id_vignette = reset($ajoute)){
  86. $infos['id_vignette'] = $id_vignette;
  87. }
  88. }
  89. /**
  90. * On tente de trouver une date correcte?
  91. *
  92. * Soit dans :
  93. * -* Original_Released_date
  94. * -* Recorded_date
  95. * -* Encoded_date
  96. */
  97. foreach(array($info[0]['Original_Released_date'][0],$info[0]['Encoded_date'][0]) as $date){
  98. $date = trim(str_replace('UTC','',$date));
  99. if(preg_match('#^[0-9]{4}-[0-9]{1,2}- [0-9]{1}$#',$date)){
  100. $date = preg_replace("#\.|/| #i",'0',$date,1);
  101. }
  102. $date = preg_replace("#\.|/| #i",'-',$date);
  103. if(preg_match('#^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$#',$date)){
  104. list($annee,$mois,$jour) = explode('-',$date);
  105. if (checkdate($mois, $jour, $annee)){
  106. // normaliser
  107. if ($date = recup_date($date)){
  108. if ($date = mktime($date[3], $date[4], 0, (int)$date[1], (int)$date[2], (int)$date[0])) {
  109. $date = date("Y-m-d H:i:s", $date);
  110. $date = vider_date($date); // enlever les valeurs considerees comme nulles (1 1 1970, etc...)
  111. if ($date) {
  112. $infos['date'] = $date;
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Si on a du contenu dans les messages de copyright,
  122. * on essaie de trouver la licence, si on a le plugin Licence
  123. *
  124. * Pour l'instant uniquement valable sur les CC
  125. */
  126. if(defined('_DIR_PLUGIN_LICENCE') && ((strlen($infos['descriptif']) > 0) OR strlen($infos['credits']) > 0)){
  127. include_spip('licence_fonctions');
  128. if(function_exists('licence_recuperer_texte')){
  129. foreach(array($infos['descriptif'],$infos['credits']) as $contenu){
  130. $infos['id_licence'] = licence_recuperer_texte($contenu);
  131. if(intval($infos['id_licence']))
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if($track == 'track type="Video"'){
  139. if(!$infos['titre'])
  140. $infos['titre'] = $info[0]['Title'][0] ? $info[0]['Title'][0] : '';
  141. $infos['videobitrate'] = $info[0]['Bit_rate'][0] ? $info[0]['Bit_rate'][0] : ($info[0]['Nominal_bit_rate'][0] ? $info[0]['Nominal_bit_rate'][0] : '');
  142. $infos['hauteur'] = $info[0]['Original_height'][0] ? $info[0]['Original_height'][0] : $info[0]['Height'][0];
  143. $infos['largeur'] = $info[0]['Original_width'][0] ? $info[0]['Original_width'][0] : $info[0]['Width'][0];
  144. $infos['videocodec'] = $info[0]['Format'][0];
  145. $infos['videocodecid'] = $info[0]['Codec_ID'][0] ? $info[0]['Codec_ID'][0] : strtolower($info[0]['Format'][0]);
  146. if($infos['videocodecid'] == 'avc1'){
  147. if(isset($info[0]['Format_profile'][0])){
  148. if(preg_match('/^Baseline.*/',$info[0]['Format_profile'][0]))
  149. $infos['videocodecid'] = 'avc1.42E01E';
  150. if(preg_match('/^Main.*/',$info[0]['Format_profile'][0]))
  151. $infos['videocodecid'] = 'avc1.4D401E';
  152. if(preg_match('/^High.*/',$info[0]['Format_profile'][0]))
  153. $infos['videocodecid'] = 'avc1.64001E';
  154. }
  155. }else if($infos['videocodec'] == 'Sorenson Spark'){
  156. $infos['videocodecid'] = 'h263';
  157. }
  158. $infos['aspect_ratio'] = $info[0]['Display_aspect_ratio'][0] ? $info[0]['Display_aspect_ratio'][0] : '';
  159. $infos['framerate'] = $info[0]['Frame_rate'][0];
  160. $infos['framecount'] = $info[0]['Frame_count'][0];
  161. $infos['rotation'] = intval($info[0]['Rotation'][0]);
  162. $infos['hasvideo'] = 'oui';
  163. }
  164. if($track == 'track type="Audio"'){
  165. $infos['hasaudio'] = 'oui';
  166. $infos['audiobitrate'] = $info[0]['Bit_rate'][0];
  167. $infos['audiochannels'] = $info[0]['Channel_s_'][0];
  168. $infos['audiochannels'] = $info[0]['Channel_s_'][0];
  169. $infos['audiosamplerate'] = $info[0]['Sampling_rate'][0];
  170. $infos['audiocodec'] = $info[0]['Codec'][0];
  171. $infos['audiobitratemode'] = strtolower($info[0]['Bit_rate_mode'][0]);
  172. if($infos['audiocodec'] == 'AAC LC'){
  173. $infos['audiocodecid'] = 'mp4a.40.2';
  174. }else if($infos['audiocodec'] == 'MPA1L3'){
  175. $infos['audiocodecid'] = 'mp3a';
  176. }else{
  177. $infos['audiocodecid'] = $info[0]['Codec_ID'][0] ? $info[0]['Codec_ID'][0] : strtolower($info[0]['Codec'][0]);
  178. }
  179. if(!$infos['audiobitrate'] && !$infos['audiochannels'] && !$infos['audiocodec'] && !$infos['audiobitratemode']){
  180. unset($infos['hasaudio']);
  181. }
  182. }
  183. }
  184. }
  185. if(!$infos['hasaudio']){
  186. $infos['hasaudio'] = 'non';
  187. }
  188. if(!$infos['hasvideo']){
  189. $infos['hasvideo'] = 'non';
  190. }
  191. $metas['Retrieved infos in database'] = $infos;
  192. $infos['metadatas'] = serialize($metas);
  193. return $infos;
  194. }
  195. ?>