Ting.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Ting extends CI_Controller {
  4. function __construct(){
  5. parent::__construct();
  6. }
  7. public function index(){
  8. $this->load->view('ting.html');
  9. }
  10. //执行转码
  11. public function transcode(){
  12. if(substr(Video_Path,0,2) == './'){
  13. $Video_Path = FCPATH.substr(Video_Path,2);
  14. }else{
  15. $Video_Path = Video_Path;
  16. }
  17. $id = 0;
  18. $xt = php_uname();
  19. if(strpos($xt, 'Windows') !== false){
  20. $jc = shell_exec('tasklist');
  21. }else{
  22. $jc = shell_exec('ps aux | less');
  23. }
  24. //获取数据库未转码
  25. $this->load->database();
  26. $row = $this->db->query("select * from ".CS_SqlPrefix."queue order by id asc limit 1")->row();
  27. if($row){
  28. $id = $row->vid;
  29. $vod = $this->db->query("select id,name,m3u8,addtime from ".CS_SqlPrefix."video where id=".$id)->row();
  30. //判断转码是否完毕
  31. if(substr(Mu_Path,0,2) == './'){
  32. $m3u8_path = FCPATH.substr(Mu_Path,2).$vod->m3u8;
  33. }else{
  34. $m3u8_path = Mu_Path.$vod->m3u8;
  35. }
  36. if(Mu_Type == 1){
  37. $ext = end(explode('/', $m3u8_path));
  38. $m3u8_path2 = str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8_path);
  39. $is_m3u8 = (file_exists($m3u8_path) && file_exists($m3u8_path2));
  40. }else{
  41. $is_m3u8 = file_exists($m3u8_path);
  42. }
  43. if($is_m3u8){
  44. //正在转码,终止执行
  45. if(strpos($jc, 'ffmpeg') !== false){
  46. exit('0');
  47. }
  48. $m3u8_neir = file_get_contents($m3u8_path);
  49. if(strpos($m3u8_neir,'#EXT-X-ENDLIST') !== false){
  50. //转换完毕
  51. $this->db->update('video',array('sid'=>1),array('id'=>$id));
  52. //发送成功API消息
  53. get_api('api',array('vid'=>$vod->id,'name'=>$vod->name,'code'=>1));
  54. }else{
  55. //转码失败
  56. $this->db->update('video',array('sid'=>2),array('id'=>$id));
  57. //发送失败API消息
  58. get_api('api',array('vid'=>$vod->id,'name'=>$vod->name,'code'=>2));
  59. }
  60. $this->db->delete('queue',array('id'=>$row->id));//删除队列
  61. $id = 0;
  62. }else{
  63. //失败了,进程还在,需要杀掉进程继续下一个转码
  64. if(strpos($jc, 'ffmpeg') !== false && $row->addtime > 0 && $row->addtime+120 < time()){
  65. //杀掉进程
  66. if(strpos($xt, 'Windows') !== false){
  67. exec('taskkill /im ffmpeg.exe /f');
  68. }else{
  69. exec('ps aux | grep "ffmpeg" | cut -c 9-15 | xargs kill -9');
  70. }
  71. $this->db->update('video',array('sid'=>2),array('id'=>$id));
  72. $this->db->delete('queue',array('id'=>$row->id));//删除队列
  73. //发送失败API消息
  74. get_api('api',array('vid'=>$vod->id,'name'=>$vod->name,'code'=>2));
  75. }
  76. }
  77. }
  78. if($id == 0){
  79. //进入待转码目录
  80. if(Mu_Dir != '' && is_dir(Mu_Dir)){
  81. $dirinfo = dir_file(Mu_Dir);
  82. if(!empty($dirinfo['file'])){
  83. $filename = $dirinfo['file'];
  84. $Mu_Dir = $dirinfo['dir'];
  85. if(substr($Mu_Dir,-1) != '/') $Mu_Dir.='/';
  86. //文件后缀
  87. $file_ext = strtolower(trim(substr(strrchr($filename, '.'), 1)));
  88. //入库名字
  89. $video_name = safe_replace(str_replace('.'.$file_ext, '', iconv('gbk', 'utf-8',$filename)));
  90. //定义允许上传的文件扩展名
  91. $ext_arr = array_filter(explode('|', Up_Ext));
  92. //判断文件后缀
  93. if(in_array($file_ext, $ext_arr) == false) {
  94. unlink($Mu_Dir.$filename);
  95. }else{
  96. $uploadStr = date('Y').'/'.date('m').'/'.date('d').'/';
  97. creatdir($Video_Path.$uploadStr); //创建目录
  98. $newname = date('YmdHis').rand(1111,9999).'.'.$file_ext;
  99. //video
  100. $data['name'] = $video_name;
  101. $data['path'] = $uploadStr.$newname;
  102. $data['addtime'] = time();
  103. //移动目录
  104. $res = rename($Mu_Dir.$filename,$Video_Path.$uploadStr.$newname);
  105. if($res){
  106. $this->load->library('xyz');
  107. $format = $this->xyz->format($Video_Path.$uploadStr.$newname);
  108. if(empty($format['size']) || empty($format['duration'])){
  109. rename($Video_Path.$uploadStr.$newname,$Mu_Dir.$filename);
  110. exit('ffmpeg error');
  111. }
  112. $data['duration'] = $format['duration'];
  113. $data['size'] = $format['size'];
  114. $data['md5'] = md5_file($Video_Path.$uploadStr.$newname);
  115. //判断是否存在
  116. $res = $this->db->query("select id from ".CS_SqlPrefix."video where name='".$video_name."'")->row();
  117. if(!$res){
  118. //判断分类是否存在
  119. $carr = explode('/',str_replace(Mu_Dir,'',$Mu_Dir));
  120. $cname = empty($carr[0]) ? $carr[1] : $carr[0];
  121. $cname = safe_replace(iconv('gbk','utf-8',$cname));
  122. if(!empty($cname)){
  123. $rec = $this->db->query("select id from ".CS_SqlPrefix."class where name='".$cname."'")->row();
  124. if($rec){
  125. $data['cid'] = $rec->id;
  126. }else{
  127. $arr2['name'] = $cname;
  128. $arr2['xid'] = 10;
  129. $this->db->insert("class",$arr2);
  130. $data['cid'] = $this->db->insert_id();
  131. }
  132. }
  133. //新增视频入库
  134. $this->db->insert("video",$data);
  135. $id = $this->db->insert_id();
  136. //M3U8保存目录
  137. $m3u8_dir = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  138. //路径
  139. $time = time();
  140. $m3u8path = m3u8_dir($id,$time);
  141. $jpgpath = m3u8_dir($id,$time,'jpg');
  142. $gifpath = m3u8_dir($id,$time,'gif');
  143. $this->db->update('video',array('m3u8'=>$m3u8path,'pic'=>$jpgpath),array('id'=>$id));
  144. //queue
  145. $queue['xu'] = 1;
  146. $queue['vid'] = $id;
  147. $queue['path'] = $data['path'];
  148. $queue['jpg'] = $m3u8_dir.$jpgpath;
  149. $queue['gif'] = $m3u8_dir.$gifpath;
  150. $queue['m3u8'] = $m3u8_dir.$m3u8path;
  151. $queue['duration'] = $format['duration'];
  152. $this->db->insert('queue',$queue);
  153. //多码率
  154. if(Mu_Type == 1){
  155. $ext = end(explode('/', $m3u8path));
  156. $queue['xu'] = 2;
  157. $queue['m3u8'] = $m3u8_dir.str_replace($ext, Mu_Kbps2.'/'.$ext, $m3u8path);
  158. $this->db->insert('queue',$queue);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. echo $id;
  167. }
  168. }