PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/model/Transcoding.php

http://pumukit.googlecode.com/
PHP | 378 lines | 306 code | 30 blank | 42 comment | 9 complexity | 33627b26bdb0de2be82221d3f488b31d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Subclass for representing a row from the 'transcoding' table.
  4. *
  5. *
  6. * @author Ruben Gonzalez Gonzalez
  7. * @author rubenrua@uvigo.es
  8. * @copyright Copyright (c) Universidad de Vigo
  9. * @version 1
  10. *
  11. * @package lib.model
  12. */
  13. class Transcoding extends BaseTranscoding
  14. {
  15. /**
  16. *
  17. *
  18. *
  19. */
  20. public function getPathIniWin()
  21. {
  22. return str_replace('/' ,'\\', str_replace(sfConfig::get('app_transcoder_path_unix'), sfConfig::get('app_transcoder_path_win'), $this->getPathIni()));
  23. }
  24. /**
  25. *
  26. *
  27. *
  28. */
  29. public function deleteTempFiles()
  30. {
  31. $c = new Criteria();
  32. $c->add(TranscodingPeer::PATH_INI, $this->getPathIni());
  33. if ((TranscodingPeer::doCount($c) == 1)&&(false !== strpos($this->getPathIni(), sfConfig::get('app_transcoder_path_tmp')))){
  34. unlink($this->getPathIni());
  35. }
  36. }
  37. /**
  38. *
  39. *
  40. * Devuelve valor entre 0 y 1 indicando porcentaje de codificacion
  41. */
  42. public function getRatio()
  43. {
  44. if ($this->getFileSizeTemp() == 0) return 0;
  45. if (($this->getStatusId() == TranscodingPeer::STATUS_ERROR)||
  46. ($this->getStatusId() == TranscodingPeer::STATUS_PAUSADO)||
  47. ($this->getStatusId() == TranscodingPeer::STATUS_ERROR)||
  48. ($this->getStatusId() == TranscodingPeer::STATUS_ESPERANDO))
  49. return 0;
  50. if ($this->getStatusId() == TranscodingPeer::STATUS_FINALIZADO)
  51. return 1;
  52. if($this->getFileSizeTemp() == 0)
  53. return 0;
  54. if ($this->getStatusId() == TranscodingPeer::STATUS_EJECUTANDOSE){
  55. $aux = sprintf('%.2f' , $this->getFileSizeTemp()/$this->getDuration()/$this->getPerfil()->getRelDurationSize());
  56. return ($aux < 1)?$aux:0.99;
  57. }
  58. }
  59. /**
  60. *
  61. *
  62. * Devuelve tamano de archivo codificande
  63. */
  64. public function getFileSizeTemp()
  65. {
  66. return filesize($this->getPathend());
  67. }
  68. /**
  69. *
  70. *
  71. *
  72. */
  73. public function getStatusText()
  74. {
  75. $aux = $this->getRatio();
  76. $rel = ($aux == 0.0)?'?':(($aux*100).'%');
  77. $aux = array(
  78. -1 => 'Error :(',
  79. 0 => 'Pausado',
  80. 1 => 'Esperando...',
  81. 2 => 'Ejecutandose ('.$rel.')...',
  82. 3 => 'Finalizado :)',
  83. );
  84. return $aux[$this->getStatusId()];
  85. }
  86. public function setEmailOn()
  87. {
  88. $this->setEmail(str_replace('#', '@', $this->getEmail()));
  89. }
  90. public function setEmailOff()
  91. {
  92. $this->setEmail(str_replace('@', '#', $this->getEmail()));
  93. }
  94. public function hasEmailOn()
  95. {
  96. return eregi('^[\x20-\x2D\x2F-\x7E]+(\.[\x20-\x2D\x2F-\x7E]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+[a-z0-9]{2,6}$', $this->getEmail());
  97. }
  98. public function getBeforet()
  99. {
  100. $conexion = Propel::getConnection();
  101. $consulta = 'SELECT COUNT(%s) AS number FROM %s WHERE %s=%s AND (%s>%s OR %s<%s)';
  102. $consulta = sprintf($consulta, TrancodingPeer::ID, TrancodingPeer::TABLE_NAME,
  103. TrancodingPeer::STATUS_ID, TrancodingPeer::STATUS_ESPERANDO,
  104. TranscodingPeer::PRIORITY, $this->getPriority(),
  105. TranscodingPeer::ID, $this->getId());
  106. $sentencia = $conexion->prepareStatement($consulta);
  107. $resultset = $sentencia->executeQuery();
  108. $resultset->next();
  109. return $resultset->getInt('number');
  110. }
  111. public function getBeforetDuration()
  112. {
  113. $conexion = Propel::getConnection();
  114. $consulta = 'SELECT SUM(%s) AS duration FROM %s WHERE %s=%s AND (%s>%s OR %s<%s)';
  115. $consulta = sprintf($consulta, TrancodingPeer::DURATION, TrancodingPeer::TABLE_NAME,
  116. TrancodingPeer::STATUS_ID, TrancodingPeer::STATUS_ESPERANDO,
  117. TranscodingPeer::PRIORITY, $this->getPriority(),
  118. TranscodingPeer::ID, $this->getId());
  119. $sentencia = $conexion->prepareStatement($consulta);
  120. $resultset = $sentencia->executeQuery();
  121. $resultset->next();
  122. return $resultset->getInt('duration');
  123. }
  124. /**
  125. *
  126. *
  127. *
  128. */
  129. public function createFile()
  130. {
  131. $file = new File();
  132. $file->setMmId($this->getMmId());
  133. $file->setPerfilId($this->getPerfilId());
  134. $file->setLanguageId($this->getLanguageId());
  135. $file->setDisplay($this->getPerfil()->getDisplay());
  136. //ojo Error
  137. if (strlen($this->getPerfil()->getUrlOut()) !== 0)
  138. $file->setUrl(str_replace($this->getPerfil()->getDirOut(), $this->getPerfil()->getUrlOut(), $this->getUrl()));
  139. $file->setFile($this->getUrl());
  140. //OJO AL CREAR NUEVOS PERFILES.
  141. switch($this->getPerfilId()){
  142. case 1:
  143. $file->setFormatId(1);
  144. $file->setCodecId(6);
  145. $file->setMimeTypeId(2);
  146. $file->setResolutionId(3);
  147. break;
  148. case 2:
  149. $file->setFormatId(5);
  150. $file->setCodecId(7);
  151. $file->setMimeTypeId(2);
  152. $file->setResolutionId(4);
  153. break;
  154. case 3:
  155. $file->setFormatId(2);
  156. $file->setCodecId(8);
  157. $file->setMimeTypeId(2);
  158. $file->setResolutionId(4);
  159. break;
  160. case 4:
  161. $file->setFormatId(2);
  162. $file->setCodecId(6);
  163. $file->setMimeTypeId(2);
  164. $file->setResolutionId(4);
  165. break;
  166. case 5:
  167. $file->setFormatId(2);
  168. $file->setCodecId(6);
  169. $file->setMimeTypeId(2);
  170. $file->setResolutionId(3);
  171. break;
  172. case 6:
  173. $file->setFormatId(1);
  174. $file->setCodecId(4);
  175. $file->setMimeTypeId(1);
  176. $file->setResolutionId(3);
  177. break;
  178. case 7:
  179. $file->setFormatId(1);
  180. $file->setCodecId(4);
  181. $file->setMimeTypeId(1);
  182. $file->setResolutionId(4);
  183. break;
  184. case 8:
  185. $file->setFormatId(2);
  186. $file->setCodecId(6);
  187. $file->setMimeTypeId(2);
  188. $file->setResolutionId(4);
  189. break;
  190. default:
  191. $file->setFormatId(FormatPeer::getDefaultSelId());
  192. $file->setCodecId(CodecPeer::getDefaultSelId());
  193. $file->setMimeTypeId(MimeTypePeer::getDefaultSelId());
  194. $file->setResolutionId(ResolutionPeer::getDefaultSelId());
  195. }
  196. $file->setBitrate($this->getPerfil()->getBitrate());
  197. $file->setFramerate($this->getPerfil()->getFramerate());
  198. $file->setChannels($this->getPerfil()->getChannels());
  199. $file->setAudio($this->getPerfil()->getAudio());
  200. $movie = new ffmpeg_movie($file->getFile());
  201. if (!is_null($movie)){
  202. $file->setResolutionVer($movie->getFrameHeight());
  203. $file->setResolutionHor($movie->getFrameWidth());
  204. }
  205. $file->setDuration($this->getDuration());
  206. $file->setSize(filesize($this->getPathEnd()));
  207. $langs = sfConfig::get('app_lang_array', array('es'));
  208. foreach($langs as $lang){
  209. $file->setCulture($lang);
  210. $this->setCulture($lang);
  211. $file->setDescription($this->getDescription());
  212. }
  213. $file->save();
  214. if(count($file->getMm()->getPics()) == 0){
  215. $file->createPic();
  216. }
  217. if($this->getMm()->getStatusId() < -40 ){
  218. $this->getMm()->setStatusId($this->getMm()->getStatusId() + 50);
  219. $this->getMm()->save();
  220. }
  221. }
  222. /**
  223. * Devuelve los minutos de la duracion del archivo
  224. *
  225. * @access public
  226. * @return integer min
  227. */
  228. public function getDurationMin()
  229. {
  230. return floor($this->getDuration() / 60);
  231. }
  232. /**
  233. * Devuelve los segundos de la duracion del archivo
  234. *
  235. * @access public
  236. * @return integer seg
  237. */
  238. public function getDurationSeg()
  239. {
  240. $aux = $this->getDuration() %60;
  241. if ($aux < 10 ) $aux= '0' . $aux;
  242. return $aux;
  243. }
  244. /**
  245. * Devuelve un texto que representa la duracion del
  246. * archivo multimedia, del formato 1' 32''
  247. *
  248. * @access public
  249. * @return integer seg
  250. */
  251. public function getDurationString()
  252. {
  253. $min = $this->getDurationMin();
  254. if ($min == 0 ) $aux = $this->getDurationSeg() ."''";
  255. else $aux = $min . "' ". $this->getDurationSeg() ."''";
  256. return $aux;
  257. }
  258. /**
  259. * Completa la ruta y la extension final (pathEnd, extEnd)
  260. *
  261. * @access public
  262. * @return string PATH END
  263. */
  264. public function setPathsAuto($path)
  265. {
  266. $this->setPathIni($path);
  267. if((strlen($this->getPathIni()) == 0)||is_null($this->getMmId())||is_null($this->getPerfilId())){
  268. throw new sfException("Error en autoEnd");
  269. }
  270. $extension = substr($this->getPathIni(), -3, 3);
  271. $extension_final = (($this->getPerfil()->getExtension() == '???')?($extension):($this->getPerfil()->getExtension()));
  272. $dir_temp = $this->getPerfil()->getDirOut() . '/' . $this->getMm()->getSerialId();
  273. @mkdir($dir_temp, 0777, true);
  274. //ANTES OJO MIRAR SI EXISTE
  275. //$dir_salida = $dir_temp.'/'.$mm->getRecordDate('ymd').'_'.$this->elimina_acentos($mm->getTitle()).'_'.
  276. // $lang->getCod().'('.$profile->getName().')_'.$trans->getId().'.'.$extension_final;
  277. $aux = $dir_temp.'/'.$this->getId().'.'.$extension_final; //UNO
  278. $this->setPathEnd($aux);
  279. //FALTA MIRAR SI EXISTE PARA NO MACHACARLA (con id no problema)
  280. $this->setExtIni($extension);
  281. $this->setExtEnd($extension_final);
  282. return $aux;
  283. }
  284. /**
  285. * Genera la linea de ejecucion sustituyendo %1 %2 %3 por
  286. * los archivos de entrada salida y cfg.
  287. *
  288. * @return string linea de comandos
  289. */
  290. public function getBatAuto($file_in = null)
  291. {
  292. //CREO LINEA DE EJECUCION
  293. $linea_comandos = $this->getPerfil()->getBat();;
  294. $linea_comandos = str_replace('%1', (is_null($file_in)?$this->getPathIni():$file_in), $linea_comandos);
  295. $linea_comandos = str_replace('%2', $this->getPathEnd(), $linea_comandos);
  296. $perfil_path = sfConfig::get('app_transcoder_path_htdocs') . $this->getPerfil()->getFileCfg();
  297. $linea_comandos = str_replace('%3', $perfil_path, $linea_comandos);
  298. $path_tmp = "/mnt/vodhosting/tmp/" . substr(basename($this->getPathIni()), 0, -4) . "_(canopus).avi";
  299. $linea_comandos = str_replace('%4', $path_tmp, $linea_comandos);
  300. foreach(range(1, 9) as $identificador){
  301. do{
  302. $my_tmp_file = sfConfig::get('app_transcoder_path_tmp').'/'. rand() ;
  303. } while (file_exists($my_tmp_file));
  304. $linea_comandos = str_replace('%_' . $identificador, $my_tmp_file, $linea_comandos);
  305. //echo "cambio " , '%_' , $identificador, " por ", $my_tmp_file, "\n";
  306. }
  307. //:)
  308. if($this->getCpu()->getType() == "windows"){
  309. $linea_comandos = str_replace(sfConfig::get('app_transcoder_path_unix'), sfConfig::get('app_transcoder_path_win'), $linea_comandos);
  310. $linea_comandos = str_replace("\\/","----antes----",$linea_comandos);
  311. $linea_comandos = str_replace("/","\\",$linea_comandos);
  312. $linea_comandos = str_replace("----antes----", "/",$linea_comandos);
  313. $linea_comandos = str_replace(" \\i "," /i ",$linea_comandos); //CAMBIAR FORMA de hacerlo
  314. }
  315. $linea_comandos = urlencode($linea_comandos);
  316. $linea_comandos .= " \n";
  317. return $linea_comandos;
  318. }
  319. }