private function get_transcode_config($arr)
 {
     $trans = new transcode($arr);
     $ret = $trans->get_transcode_config();
     $ret = json_decode($ret, 1);
     return $ret;
 }
 public function run()
 {
     /**************************************查询出数据库里面正在转码的视频******************************************/
     $sql = "SELECT * FROM " . DB_PREFIX . "vodinfo WHERE status = 0  AND vod_leixing != 4 ";
     $q = $this->db->query($sql);
     $videos = array();
     while ($r = $this->db->fetch_array($q)) {
         $videos[] = $r['id'];
     }
     if (!$videos) {
         $this->errorOutput(NO_VIDEO_IS_TRANSCODING);
     }
     /**************************************查询出当前正在开启的服务器********************************************/
     $sql = " SELECT * FROM " . DB_PREFIX . "transcode_center WHERE is_open = 1 ";
     $q = $this->db->query($sql);
     $servers = array();
     while ($r = $this->db->fetch_array($q)) {
         $servers[] = $r;
     }
     if (!$servers) {
         $this->errorOutput(NO_TRANSERVER_CAN_USE);
     }
     /**************************************查询出所有正在使用的转码服务器中正在转码的视频*****************************/
     $ids = array();
     //存储正在转码中视频id
     foreach ($servers as $k => $v) {
         $trans = new transcode(array('host' => $v['trans_host'], 'port' => $v['trans_port']));
         $task = json_decode($trans->get_transcode_status(), 1);
         if ($task['return'] == 'success' && $task['running']) {
             if ($task['waiting']) {
                 $task['running'] = array_merge($task['running'], $task['waiting']);
             }
             foreach ($task['running'] as $_k => $_v) {
                 if (!in_array($_v['id'], $ids)) {
                     $ids[] = $_v['id'];
                 }
             }
         }
     }
     /***************算出页面中正在转码的视频与实际服务器中转码视频以及等待的视频的差集,算出来视频id就是遗漏的视频**************/
     $diffIds = array_diff($videos, $ids);
     if (!$diffIds) {
         $this->errorOutput(NO_VIDEO_LEAVE_OUT);
     }
     //将找出遗漏的视频提交重新转码
     $curl = new curl($this->settings['App_mediaserver']['host'], $this->settings['App_mediaserver']['dir'] . 'admin/');
     $curl->setSubmitType('get');
     foreach ($diffIds as $k => $v) {
         $curl->initPostData();
         $curl->addRequestData('id', $v);
         $curl->addRequestData('force_recodec', 1);
         $curl->request('retranscode.php');
     }
     $this->addItem('遗漏的视频:' . implode(',', $diffIds) . '已经提交重新转码');
     $this->output();
 }
 public function run()
 {
     /**************************************查询出数据库里面经过多码流但未成功的视频**********************************/
     $sql = "SELECT * FROM " . DB_PREFIX . "vodinfo WHERE is_morebitrate = 0 AND clarity != '' ";
     $q = $this->db->query($sql);
     $videos = array();
     while ($r = $this->db->fetch_array($q)) {
         $videos[] = $r['id'] . '_more';
         //多码流的任务格式
     }
     if (!$videos) {
         $this->errorOutput(NO_FAILED_VIDEOS_FROM_MORE);
     }
     /**************************************查询出当前正在开启的服务器********************************************/
     $sql = " SELECT * FROM " . DB_PREFIX . "transcode_center WHERE is_open = 1 ";
     $q = $this->db->query($sql);
     $servers = array();
     while ($r = $this->db->fetch_array($q)) {
         $servers[] = $r;
     }
     if (!$servers) {
         $this->errorOutput(NO_TRANSERVER_CAN_USE);
     }
     /**************************************查询出所有正在使用的转码服务器中正在转码的视频*****************************/
     $ids = array();
     //存储正在转码中视频id
     foreach ($servers as $k => $v) {
         $trans = new transcode(array('host' => $v['trans_host'], 'port' => $v['trans_port']));
         $task = json_decode($trans->get_transcode_status(), 1);
         if ($task['return'] == 'success' && $task['running']) {
             if ($task['waiting']) {
                 $task['running'] = array_merge($task['running'], $task['waiting']);
             }
             foreach ($task['running'] as $_k => $_v) {
                 if (!in_array($_v['id'], $ids)) {
                     $ids[] = $_v['id'];
                 }
             }
         }
     }
     /***************算出失败的视频与实际服务器中转码视频以及等待的视频的差集,算出来视频id就是正真多码流失败的视频**************/
     $diffIds = array_diff($videos, $ids);
     if (!$diffIds) {
         $this->errorOutput(NO_VIDEO_LEAVE_OUT);
     }
     $real_failed = array();
     foreach ($diffIds as $v) {
         $real_failed[] = str_replace('_more', '', $v);
     }
     //更新数据库
     $sql = "UPDATE " . DB_PREFIX . "vodinfo SET clarity = '' WHERE id IN (" . implode(',', $real_failed) . ")";
     $this->db->query($sql);
     $this->addItem('多码流失败的视频:' . implode(',', $diffIds) . '已经提交处理');
     $this->output();
 }
Exemple #4
0
 public function delete()
 {
     if (!$this->input['id']) {
         $this->errorOutput(NOID);
     }
     $sql = " SELECT * FROM " . DB_PREFIX . "vodinfo WHERE id IN (" . $this->input['id'] . ")";
     $q = $this->db->query($sql);
     $video = array();
     while ($r = $this->db->fetch_array($q)) {
         if (file_exists($r['video_base_path'] . $r['video_path'] . $r['video_filename'])) {
             $_pathinfo = pathinfo($r['video_path']);
             if ($_pathinfo['extension'] == 'ssm') {
                 $video_tmp = explode('.', $r['video_filename']);
                 $video[] = $r['video_base_path'] . $r['video_path'] . $video_tmp[0];
             }
         }
     }
     if ($video) {
         foreach ($video as $v) {
             if (!defined('NOT_CREATE_ISMV') || !NOT_CREATE_ISMV) {
                 @unlink($v . '.ism');
                 @unlink($v . '.ismv');
             }
             //重命名.ssm目录
             $_dir = pathinfo($v);
             if ($_dir['dirname'] && is_dir($_dir['dirname'])) {
                 rename($_dir['dirname'], $_dir['dirname'] . '_removed');
             }
         }
     }
     /*********************判断删除的视频中有没有正在转码的,如果有就停止掉***************/
     $ids = explode(',', $this->input['id']);
     foreach ($ids as $id) {
         if ($trans_servers = checkStatusFromAllServers($id)) {
             $transcode = new transcode($trans_servers);
             $transcode->stop_transcode_task("{$id}");
         }
     }
     /**************************************************************************/
     $this->addItem('success');
     $this->output();
 }
 static function GetEncodedTime($logfile)
 {
     if (!file_exists($logfile)) {
         transcode::log("can't open FFMPEG-Log '" . $logfile . "'");
         return false;
     } else {
         $FFMPEGLog = @file_get_contents($logfile);
         $dPos = strpos($FFMPEGLog, " Duration: ");
         self::$duration = self::durationToSecs(substr($FFMPEGLog, $dPos + 11, 11));
         $FFMPEGLog = str_replace("\r", "\n", $FFMPEGLog);
         $lines = explode("\n", $FFMPEGLog);
         $line = $lines[count($lines) - 2];
         if ($tpos = strpos($line, "time=")) {
             $bpos = strpos($line, " bitrate=");
             $time = substr($line, $tpos + 5, $bpos - ($tpos + 5));
             return $time;
         } else {
             return false;
         }
     }
 }
 static function item_created($item)
 {
     if ($item->is_movie()) {
         transcode::log("Item created - is a movie. Let's create a transcode task or 2..");
         $ffmpegPath = module::get_var("transcode", "ffmpeg_path");
         $fileObj = self::_getVideoInfo($item->file_path());
         transcode::log($fileObj);
         // Save our needed variables
         $srcFile = $item->file_path();
         $srcWidth = transcode::makeMultipleTwo($fileObj->video->width);
         $srcHeight = transcode::makeMultipleTwo($fileObj->video->height);
         $aspect = $srcWidth / $srcHeight;
         $srcFPS = $fileObj->video->fps;
         $srcAR = $fileObj->audio->samplerate;
         if ($srcAR > 44100) {
             $srcAR = 44100;
         }
         $accepted_sample_rates = array(11025, 22050, 44100);
         if (!in_array($srcAR, $accepted_sample_rates)) {
             // if the input sample rate isn't an accepted rate, find the next lowest rate that is
             $below = true;
             $rate = 0;
             if ($srcAR < 11025) {
                 $rate = 11025;
             } else {
                 foreach ($accepted_sample_rates as $r) {
                     transcode::log("testing audio rate '" . $r . "' against input rate '" . $srcAR . "'");
                     if ($r < $srcAR) {
                         $rate = $r;
                     }
                 }
             }
             $srcAR = $rate;
         }
         $srcACodec = module::get_var("transcode", "audio_codec");
         $heights = array();
         if (module::get_var("transcode", "resolution_240p")) {
             array_push($heights, 240);
         }
         if (module::get_var("transcode", "resolution_360p")) {
             array_push($heights, 360);
         }
         if (module::get_var("transcode", "resolution_480p")) {
             array_push($heights, 480);
         }
         if (module::get_var("transcode", "resolution_576p")) {
             array_push($heights, 576);
         }
         if (module::get_var("transcode", "resolution_720p")) {
             array_push($heights, 720);
         }
         if (module::get_var("transcode", "resolution_1080p")) {
             array_push($heights, 1080);
         }
         if (!is_dir(VARPATH . "modules/transcode/flv/" . $item->id)) {
             @mkdir(VARPATH . "modules/transcode/flv/" . $item->id);
         }
         $xtraFlags = module::get_var("transcode", "ffmpeg_flags", "");
         foreach ($heights as $destHeight) {
             transcode::log("srcHeight: " . $srcHeight . ", destheight: " . $destHeight);
             // don't bother upscaling, there's no advantage to it...
             if ($destHeight > $srcHeight) {
                 continue;
             }
             $destFormat = module::get_var("transcode", "format", "flv");
             $destWidth = floor($destHeight * $aspect);
             if ($destWidth % 2) {
                 $destWidth = ceil($destHeight * $aspect);
             }
             transcode::log("destination resolution: " . $destWidth . "x" . $destHeight);
             $destFile = VARPATH . "modules/transcode/flv/" . $item->id . "/" . $destWidth . "x" . $destHeight . ".flv";
             switch ($destHeight) {
                 case 240:
                     $destVB = 128;
                     $srcAB = 16 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
                 case 360:
                     $destVB = 384;
                     $srcAB = 16 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
                 case 480:
                     $destVB = 1024;
                     $srcAB = 32 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
                 case 576:
                     $destVB = 2048;
                     $srcAB = 32 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
                 case 720:
                     $destVB = 4096;
                     $srcAB = 64 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
                 case 1080:
                     $destVB = 8192;
                     $srcAB = 64 * ($fileObj->audio->channels ? $fileObj->audio->channels : 2);
                     break;
             }
             $destVB *= 1024;
             $srcAB *= 1024;
             $cmd = $ffmpegPath . " " . "-i \"" . $srcFile . "\" ";
             if ($fileObj->audio->has) {
                 $cmd .= "-y -acodec " . $srcACodec . " " . "-ar " . $srcAR . " " . "-ab " . $srcAB . " ";
             } else {
                 $cmd .= "-an ";
             }
             $cmd .= "-vb " . $destVB . " " . "-f " . $destFormat . " " . "-s " . $destWidth . "x" . $destHeight . " " . $xtraFlags . " " . "\"" . $destFile . "\"";
             transcode::log($cmd);
             $task_def = Task_Definition::factory()->callback("transcode_task::transcode")->name("Video Transcode to " . $destWidth . "x" . $destHeight)->severity(log::SUCCESS);
             $task = task::create($task_def, array("ffmpeg_cmd" => $cmd, "width" => $destWidth, "height" => $destHeight, "item_id" => $item->id));
             task::run($task->id);
         }
     }
 }
Exemple #7
0
 public function retranscode()
 {
     if (!$this->input['id']) {
         $this->errorOutput(NOID);
     }
     $id = intval($this->input['id']);
     $sql = " SELECT * FROM " . DB_PREFIX . "vodinfo WHERE id = {$id} ";
     $video = $this->db->query_first($sql);
     $output_tmp = explode('.', $video['video_filename']);
     //重新转码只针对源视频
     if ($video['source_path']) {
         $source_path = rtrim($video['source_path'], '/') . '/' . $video['source_filename'];
     } else {
         $source_path = '';
     }
     if (!is_file($video['source_base_path'] . $source_path) || !$source_path) {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$id}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $id);
         //将转码失败的信息记录下来
         $this->errorOutput(NOFILE);
     }
     //判断当前该视频是不是正在转码,如果正在转码就删除该任务
     if ($t_server = checkStatusFromAllServers($id)) {
         $s_tran = new transcode($t_server);
         $s_tran->stop_transcode_task($id);
     }
     $vod_config = transcode_config();
     //查询出视频转码的配置信息
     if ($this->input['audit_auto']) {
         $this->settings['App_mediaserver']['extends'] = $this->input['audit_auto'];
     }
     //构建提交的转码配置
     if (defined('SOBEY_SORTIDS') && SOBEY_SORTIDS) {
         if (in_array($video['vod_sort_id'], explode(',', SOBEY_SORTIDS))) {
             $this->input['mp4_from_sobey'] = 1;
         }
     }
     $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
     $data = array("sourceFile" => array(array('source' => $video['source_base_path'] . $source_path, 'start' => '0', 'duration' => '', 'is_water_marked' => '0')), "id" => "{$id}", "app_id" => APPID, "app_key" => APPKEY, "type" => "transcode_upload", "targetDir" => $video['video_base_path'] . $video['video_path'], "output_filename" => $output_tmp[0], "config" => $vod_config, "force_recodec" => $this->input['force_recodec'] ? '1' : '', "mp4_from_sobey" => $this->input['mp4_from_sobey'] ? '1' : '', "callback" => $this->settings['App_mediaserver'], "absolute_path" => '1');
     //如果没传保持原来状态就将状态变成转码中
     if (!$this->input['retain_status']) {
         $sql = "UPDATE " . DB_PREFIX . "vodinfo SET status = 0  WHERE id = {$id}";
         $this->db->query($sql);
     }
     //如果是制定强制转码的话就要调取该视频第一次提交转码的时候的马赛克和水印配置
     if (file_exists(UPLOAD_DIR . 'water/' . $id . '.json')) {
         $_param = file_get_contents(UPLOAD_DIR . 'water/' . $id . '.json');
         $_param = json_decode($_param, 1);
         if ($data['force_recodec']) {
             if ($_param['mosaic']) {
                 $data['config']['mosaic'] = $_param['mosaic'];
             }
             if ($_param['water']) {
                 $data['config']['water_mark'] = $_param['water']['water_mark'];
                 $data['config']['water_mark_x'] = $_param['water']['water_mark_x'];
                 $data['config']['water_mark_y'] = $_param['water']['water_mark_y'];
             }
         }
         if ($_param['metadata']) {
             $data['metadata'] = $_param['metadata'];
         }
     }
     //选取转码服务器(此处强制转码与普通转码在选取服务器的时候有所不同)
     if ($data['force_recodec']) {
         $tran_server = select_assign_servers(true);
     } else {
         $tran_server = select_servers($id);
     }
     if (!$tran_server) {
         $this->errorOutput(NO_SELECT_TRANSERVER);
     }
     $trans = new transcode($tran_server);
     //根据选取到转码服务器是否需要携带文件,选择各自的提交方式
     if ($tran_server['need_file']) {
         $data['upload_file_in_callback'] = "1";
         //$data['sourceFile'][0]['url'] = 'http://' . ltrim(SOURCE_VIDEO_DOMIAN,'http://') . '/' . $source_path;
         $data['sourceFile'][0]['url'] = 'http://' . ltrim(rtrim($video['source_hostwork'], '/'), 'http://') . '/' . $source_path;
     }
     $ret = $trans->addTranscodeTask($data);
     $return = json_decode($ret, 1);
     if ($return['return'] == 'fail') {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$id}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $id);
         //将转码失败的信息记录下来
     }
     //此处是处理强制转码之后删除json文件
     if (file_exists(UPLOAD_DIR . 'water/' . $id . '.json') && $data['force_recodec']) {
         @unlink(UPLOAD_DIR . 'water/' . $id . '.json');
     }
     echo $ret;
 }
Exemple #8
0
 private function videomark($type)
 {
     if (!$this->input['id']) {
         $this->errorOutput(NOID);
         //任务id
     }
     $vid = intval($this->input['id']);
     $start = $this->input['start'];
     //对应每个视频片段的开始时间
     $duration = $this->input['duration'];
     //对应每个视频片段的时长
     $source_dir = $this->input['source_dir'];
     //视频片段的原文件目录
     $is_water_marked = $this->input['is_water_marked'];
     //视频片段的是否已经加过水印
     $is_forcecode = $this->input['is_forcecode'];
     //标识视频是否已经经过强制转码
     $video_base_path = $this->input['video_base_path'];
     //视频的基路径
     if (!is_array($start)) {
         foreach (array("start", "duration", "source_dir") as $v) {
             ${$v} = array(${$v});
         }
     }
     if (count($start) != count($duration) || count($start) != count($source_dir)) {
         $this->errorOutput(NOMATCH);
     }
     /****************查询视频配置信息*****************************/
     $vod_config = transcode_config();
     /***************配置一下转码完成之后视频的存放目录****************/
     if ($type == 'transcode_mark') {
         //判断当前该视频是不是正在转码,如果正在转码就删除该任务
         if ($t_server = checkStatusFromAllServers($vid)) {
             $s_tran = new transcode($t_server);
             $s_tran->stop_transcode_task($vid);
         }
         //拆条的时候要判断原视频是否正在转码中,如果正在转码中,暂时不拆条
         $sql = "SELECT * FROM " . DB_PREFIX . "vodinfo WHERE id = '" . $vid . "'";
         $cur_video = $this->db->query_first($sql);
         if ($cur_video['original_id']) {
             if (checkStatusFromAllServers($cur_video['original_id'])) {
                 $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id =  {$vid}";
                 $this->db->query($sql);
                 $this->addLogs('拆条有误', '', '', '当前拆条的原视频正在转码中,该视频id:' . $vid);
                 $this->errorOutput('当前拆条的原视频正在转码中,请稍后对该视频拆条');
             }
         }
         $v_videodir = create_video_dir();
         $vod_dir_names = $v_videodir[0];
         $target_dir = $v_videodir[1];
         $all_target_dir = TARGET_DIR . $target_dir;
         if (!hg_mkdir($all_target_dir) || !is_writeable($all_target_dir)) {
             $this->errorOutput(NOWRITE);
         }
         $output_filename = $vod_dir_names . '';
         //为了兼容老版本
         if (defined("TARGET_VIDEO_DOMAIN")) {
             $server_host = ltrim(TARGET_VIDEO_DOMAIN, 'http://');
         } else {
             $server_host = $this->settings['videouploads']['host'];
         }
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET video_base_path = '" . TARGET_DIR . "', video_path = '" . $target_dir . "',video_filename = '" . $output_filename . "." . $vod_config['output_format'] . "',hostwork = 'http://" . $server_host . "' WHERE id =  {$vid}";
         $this->db->query($sql);
     } else {
         //快编的时候由于要覆盖原视频,所以$target_dir目录是原来视频的目录,从数据库查询
         $sql = "SELECT video_base_path,video_path,video_filename,transcode_server FROM " . DB_PREFIX . "vodinfo WHERE id = {$vid}";
         $arr = $this->db->query_first($sql);
         //检测该视频有没有被拆过条,如果有的话就不允许快编
         if ($this->checkVideoIsSplit($vid)) {
             $this->addLogs('快编有误', '', '', '该视频已经有拆条不能快编,该视频id:' . $vid);
             $this->errorOutput('该视频已经有拆条不能快编');
         }
         //判断当前该视频是不是正在转码,如果正在转码就删除该任务
         foreach (array($vid, $vid . '_more') as $_vid) {
             if ($t_server = checkStatusFromAllServers($_vid)) {
                 $s_tran = new transcode($t_server);
                 $s_tran->stop_transcode_task($_vid);
             }
         }
         $target_dir = $arr['video_path'];
         $filename = explode('.', $arr['video_filename']);
         $output_filename = $filename[0];
         if ($this->input['audit_auto']) {
             $this->settings['App_mediaserver']['extends'] = $this->input['audit_auto'];
         }
         $all_target_dir = $arr['video_base_path'] . $target_dir;
     }
     //更新当前清晰度,表明当前视频使用哪个转码配置进行转码的
     $sql = " UPDATE " . DB_PREFIX . "vodinfo SET cur_clarity = '" . $vod_config['unique_id'] . "',status = 0,app_uniqueid = 'livmedia',mod_uniqueid = 'livmedia' WHERE id =  {$vid}";
     $this->db->query($sql);
     //选取转码服务器
     $tran_server = select_servers($vid);
     if (!$tran_server) {
         $this->errorOutput(NO_SELECT_TRANSERVER);
     }
     $source_conf = array();
     foreach ($source_dir as $k => $v) {
         $source_conf[] = array('source' => $video_base_path[$k] . $source_dir[$k], 'start' => $start[$k], 'duration' => $duration[$k], 'is_water_marked' => $is_water_marked[$k], 'is_recodec' => $is_forcecode[$k], 'url' => $tran_server['need_file'] ? defined("TARGET_VIDEO_DOMAIN") ? 'http://' . ltrim(TARGET_VIDEO_DOMAIN, 'http://') . '/' . $source_dir[$k] : $this->settings['videouploads']['protocol'] . $this->settings['videouploads']['host'] . '/' . $source_dir[$k] : '');
     }
     /**************用curl将视频连同转码参数一并提交过去****************/
     $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
     $data = array("sourceFile" => $source_conf, "id" => "{$vid}", "app_id" => APPID, "app_key" => APPKEY, "type" => $type, "targetDir" => $all_target_dir, "output_filename" => $output_filename, "config" => $vod_config, "callback" => $this->settings['App_mediaserver'], "absolute_path" => '1');
     //根据选取到转码服务器是否需要携带文件,选择各自的提交方式
     if ($tran_server['need_file']) {
         $data['upload_file_in_callback'] = "1";
     }
     //头信息
     if ($this->settings['metadata']) {
         $metadata = $this->settings['metadata'];
         foreach ($metadata as $k => $v) {
             if (!$v) {
                 unset($metadata[$k]);
             } else {
                 $metadata[$k] = urlencode($v);
             }
         }
         $data['metadata'] = $metadata;
     }
     $trans = new transcode($tran_server);
     $ret = $trans->addTranscodeTask($data);
     $return = json_decode($ret, 1);
     if ($return['return'] == 'fail') {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$vid}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $vid);
         //将转码失败的信息记录下来
     }
     echo $ret;
 }
Exemple #9
0
 public function submit_transcode($video, $target, $output_filename, $vid, $img_info)
 {
     //构建提交转码的参数
     $vod_config = transcode_config();
     //$video = $this->input;
     $source_arr = array('source' => UPLOAD_DIR . $video['filepath'], 'start' => '0', 'duration' => '', 'is_water_marked' => '0');
     $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
     $data = array("sourceFile" => array($source_arr), "id" => $vid, "app_id" => APPID, "app_key" => APPKEY, "type" => "transcode_upload", "targetDir" => TARGET_DIR . $target, "output_filename" => $output_filename, "config" => $vod_config, "callback" => $this->settings['App_mediaserver'], "ts_need_preprocess" => $video['ts_need_preprocess'] ? '1' : '0', "mp4_from_sobey" => $video['mp4_from_sobey'] ? '1' : '0', "force_recodec" => $video['force_recodec'] ? '1' : '0', "absolute_path" => '1');
     //头信息
     if ($this->settings['metadata']) {
         $metadata = $this->settings['metadata'];
         foreach ($metadata as $k => $v) {
             if (!$v) {
                 unset($metadata[$k]);
             } else {
                 $metadata[$k] = urlencode($v);
             }
         }
         $data['metadata'] = $metadata;
     }
     //选取转码服务器
     if ($video['server_id']) {
         $tran_server = select_servers_by_id($video['server_id'], $vid);
     } else {
         $tran_server = select_servers($vid);
         //自动选择转码服务器
     }
     if (!$tran_server) {
         $this->errorOutput(NO_SELECT_TRANSERVER);
     }
     $trans = new transcode($tran_server);
     //根据选取到转码服务器是否需要携带文件,选择各自的提交方式
     if ($tran_server['need_file']) {
         $data['upload_file_in_callback'] = "1";
         $data['sourceFile'][0]['url'] = 'http://' . ltrim(SOURCE_VIDEO_DOMIAN, 'http://') . '/' . $source_arr['source'];
     }
     $ret = $trans->addTranscodeTask($data);
     $return = json_decode($ret, 1);
     if ($return['return'] == 'fail') {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$vid}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $vid);
         //将转码失败的信息记录下来
         $error_info = array('return' => 'success', 'transcode' => 'fail', 'id' => $vid, 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'img' => $img_info, 'ErrorCode' => '0x0025', 'ErrorText' => '提交转码失败');
         echo json_encode($error_info);
         //上传成功,转码失败
     } else {
         //返回数据
         $video_info = array('id' => $vid, 'protocol' => 'http://', 'host' => defined("TARGET_VIDEO_DOMAIN") ? ltrim(TARGET_VIDEO_DOMAIN, 'http://') : $this->settings['videouploads']['host'], 'dir' => $target, 'file_name' => $output_filename, 'img' => $img_info, 'type' => $vod_config['output_format'], 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'return' => 'success');
         return $video_info;
         /*****************************
         			//请求用户提供的callback地址,并且携带用户提供的数据
         			if($video['callback_url'])
         			{
         				if($video['callback_data'])
         				{
         					$callback_data = json_decode(base64_decode($video['callback_data']),1);
         					//此处在视频库里面记录电视剧的id
         					if($callback_data['tv_play_id'])
         					{
         						$sql = "UPDATE " . DB_PREFIX ."vodinfo SET tv_play_id = '" . $callback_data['tv_play_id'] . "' WHERE id = '" .$vid. "'";
         						$this->db->query($sql);
         					}
         					$video_info['callback_data'] = $callback_data;
         				}
         				
         				if($callbackReturnData = $this->mediaserverCallback($video['callback_url'],$video_info))
         				{
         					$callbackReturnData = json_decode($callbackReturnData,1);
         					if($callbackReturnData['ErrorCode'])
         					{
         						$video_info['callback_return'] = $callbackReturnData;
         					}
         					else
         					{
         						$video_info['callback_return'] = $callbackReturnData[0];
         					}
         				}
         			}
         			
         			//可以接受用户传递的callback,这个callback是在转码完成之后回调
         			if($video['after_callback_url'])
         			{
         				//在data目录产生文件记录这个url
         				$after_dir = DATA_DIR . 'after_callback_url/';
         				if (!hg_mkdir($after_dir) || !is_writeable($after_dir))
         				{
         					$this->errorOutput(NOWRITE);
         				}
         				file_put_contents($after_dir . $vid . '.url',$video['after_callback_url']);
         			}
         			
         			$this->addItem($video_info);
         			$this->output();
         			*****************************/
     }
 }
 public function set_waiting_task_weight()
 {
     if (!$this->input['id'] || !$this->input['server_id']) {
         $this->errorOutput(NOID);
     }
     $weight = intval($this->input['weight']);
     $sql = " SELECT * FROM " . DB_PREFIX . "transcode_center WHERE id = '" . $this->input['server_id'] . "'";
     $server = $this->db->query_first($sql);
     if (!$server) {
         $this->errorOutput(NOSERVER);
     }
     $t_server = array('host' => $server['trans_host'], 'port' => $server['trans_port']);
     $transcode = new transcode($t_server);
     $ret = $transcode->set_waiting_task_weight($this->input['id'], $weight);
     $ret = json_decode($ret, 1);
     if ($ret['return'] && $ret['return'] == 'success') {
         $this->addItem('success');
         $this->output();
     }
 }
 public function run()
 {
     //在转码服务器空闲的时候进行多码流
     /*
     $route = select_servers();//选取服务器	
     if(!$route)
     {
     	$this->errorOutput(NO_SELECT_TRANSERVER);
     }
     $transcode = new transcode($route);
     $task_info = json_decode($transcode->get_transcode_tasks(),1);
     if($task_info['transcoding_tasks'])
     {
     	$this->errorOutput(EXECAFTERMOREBIT);
     }
     */
     //先选取转码服务器
     /*
     if(defined('MORE_BITRATE_SERVER') && MORE_BITRATE_SERVER)//指定转码服务器
     {
     	$tran_server = select_servers_by_id(intval(MORE_BITRATE_SERVER));
     }
     else 
     {
     	//$tran_server = select_servers($vid);//自动选择转码服务器
     	$this->errorOutput('没有可用于多码流的服务器');
     }
     */
     //选取用于多码流的服务器
     $tran_server = select_assign_servers();
     if (!$tran_server) {
         $this->errorOutput(NO_SELECT_TRANSERVER);
     }
     //先找出视频需要多码流的视频(排除转码中与暂停状态的视频以及失败的视频)
     $sql = "SELECT * FROM " . DB_PREFIX . "vodinfo WHERE morebitrate_config_id != '' AND is_morebitrate=0 AND status NOT IN (0,4,-1,5) ORDER BY create_time DESC LIMIT 0,2";
     $q = $this->db->query($sql);
     $videos = array();
     while ($r = $this->db->fetch_array($q)) {
         $videos[$r['id']] = $r;
     }
     $task_ids = array();
     //记录任务反馈
     if (!$videos) {
         $this->errorOutput(NOVIDEOS);
     }
     //判断选取的视频在不在多码流转码中,在的话就不提交这个视频了
     foreach ($videos as $k => $v) {
         if (checkStatusFromAllServers($k . '_more') || checkStatusFromAllServers($k)) {
             unset($videos[$k]);
         }
     }
     if (!$videos) {
         $this->errorOutput(NOVIDEOS);
     }
     //$transcode_configs = get_transcode_configs($config_id);
     foreach ($videos as $kk => $video) {
         //采用转码之后的视频
         $video_source = rtrim($video['video_path'], '/') . '/' . $video['video_filename'];
         //构建target的目录
         $output_file = array();
         $clarityUniqueId = array();
         //记录清晰度标识
         $transcode_configs = get_transcode_configs($video['morebitrate_config_id']);
         if (!$transcode_configs) {
             $this->errorOutput(NO_DATA);
         }
         //如果获取的转码配置个数小于1,就不执行了
         if (count($transcode_configs) < 1) {
             $this->errorOutput(NOMOREBITRATE);
         }
         //array_shift($transcode_configs);
         foreach ($transcode_configs as $k => $v) {
             if ($v['unique_id'] == $video['cur_clarity']) {
                 continue;
             }
             $target_dir_info = pathinfo(rtrim($video['video_path'], '/'));
             $new_target_dir = $target_dir_info['dirname'] . '/' . $v['unique_id'] . '_' . $target_dir_info['basename'];
             if (!hg_mkdir($video['video_base_path'] . $new_target_dir) || !is_writeable($video['video_base_path'] . $new_target_dir)) {
                 $this->errorOutput(NOWRITE);
             }
             $output_file[] = array('targetDir' => $video['video_base_path'] . $new_target_dir, 'output_filename' => $v['unique_id'] . '_' . $target_dir_info['filename'], 'config' => $v);
             $clarityUniqueId[] = $v['unique_id'];
         }
         if ($tran_server['need_file']) {
             /*
             if(defined("TARGET_VIDEO_DOMAIN"))
             {
             	$url = 'http://' . ltrim(TARGET_VIDEO_DOMAIN,'http://') . '/' . $video_source;
             }
             else
             {
             	$url = $this->settings['videouploads']['protocol'] . $this->settings['videouploads']['host'] . '/' . $video_source;
             }
             */
             $url = 'http://' . ltrim(rtrim($video['hostwork'], '/'), 'http://') . '/' . $video_source;
         } else {
             $url = '';
         }
         $sourceFile = array();
         $sourceFile[] = array('source' => $video['video_base_path'] . $video_source, 'start' => '0', 'duration' => '', 'is_water_marked' => '0', 'url' => $url);
         //构建提交转码的数据
         $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
         $this->settings['App_mediaserver']['filename'] = 'more_bitrate_callback.php';
         //设置多码流回调
         $data = array("sourceFile" => $sourceFile, "id" => $video['id'] . '_more', "app_id" => APPID, "app_key" => APPKEY, "type" => 'transcode_multi_bitrate', "outputFile" => $output_file, "callback" => $this->settings['App_mediaserver'], "absolute_path" => '1');
         //头信息
         if ($this->settings['metadata']) {
             $metadata = $this->settings['metadata'];
             foreach ($metadata as $k => $v) {
                 if (!$v) {
                     unset($metadata[$k]);
                 } else {
                     $metadata[$k] = urlencode($v);
                 }
             }
             $data['metadata'] = $metadata;
         }
         $trans = new transcode($tran_server);
         $ret = $trans->addTranscodeTask($data);
         //提交后更新清晰度字段
         $sql = "UPDATE " . DB_PREFIX . "vodinfo SET clarity = '" . serialize($clarityUniqueId) . "',is_morebitrate=1 WHERE id = '" . $video['id'] . "'";
         $this->db->query($sql);
         $return = json_decode($ret, 1);
         if ($return['return'] == 'fail') {
             $this->addLogs('提交多码流', $data, $return, '提交多码流,视频id:' . $return['id']);
         }
         $task_ids[] = $return;
     }
     $this->addItem($task_ids);
     $this->output();
 }
 public function count()
 {
     if (!$this->input['server_id']) {
         $this->errorOutput(NO_SERVER_ID);
     }
     $sql = "SELECT * FROM " . DB_PREFIX . "transcode_center  WHERE id = '" . intval($this->input['server_id']) . "'";
     $arr = $this->db->query_first($sql);
     $trans = new transcode(array('host' => $arr['trans_host'], 'port' => $arr['trans_port']));
     $ret = $trans->get_transcode_status();
     $ret = json_decode($ret, 1);
     $total = count($ret['running']) + count($ret['waiting']);
     echo json_encode($total);
 }
Exemple #13
0
 public function submit_transcode()
 {
     //构建提交转码的参数
     $vod_config = transcode_config();
     $is_file = 0;
     //标识是否是文件上传
     if ($_FILES) {
         $is_file = 1;
         $this->check_has_videofile($vod_config);
         $this->input['filepath'] = $this->dir_info['filepath'];
     } else {
         if (!$this->input['notcheck']) {
             if (!$this->input['filepath'] || !file_exists(UPLOAD_DIR . $this->input['filepath'])) {
                 $this->errorOutput(NOTFINDFILE);
             }
         }
         //如果没传title就取文件名
         if (!$this->input['title']) {
             $_path_name = basename($this->input['filepath']);
             $this->input['title'] = substr($_path_name, 0, strrpos($_path_name, '.'));
         }
         $this->create_dir($vod_config);
     }
     $video = $this->input;
     $v_vinfo = $this->storage_data($video, $vod_config);
     $vid = $v_vinfo['vid'];
     $img_info = $v_vinfo['img_info'];
     if ($video['audit_auto']) {
         $this->settings['App_mediaserver']['extends'] = $video['audit_auto'];
     }
     //传到转码服务器的duration参数的单位是秒,所以此处因为接受外部的时间单位是ms
     if ($is_file) {
         $duration = '';
         $start = '0';
     } else {
         $duration = $video['duration'] ? $video['duration'] : intval($video['end'] - $video['start']) / 1000 . '';
         $start = $video['start'];
     }
     $source_arr = array('source' => UPLOAD_DIR . $video['filepath'], 'start' => $start, 'duration' => $duration, 'is_water_marked' => '0');
     $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
     $data = array("sourceFile" => array($source_arr), "id" => "{$vid}", "app_id" => APPID, "app_key" => APPKEY, "type" => "transcode_upload", "targetDir" => TARGET_DIR . $this->dir_info['target_dir'], "output_filename" => $this->dir_info['output_filename'], "config" => $vod_config, "callback" => $this->settings['App_mediaserver'], "ts_need_preprocess" => $video['ts_need_preprocess'] ? '1' : '0', "mp4_from_sobey" => $video['mp4_from_sobey'] ? '1' : '0', "force_recodec" => $video['force_recodec'] ? '1' : '0', "absolute_path" => '1');
     //加马赛克(优先采用用户提供的自定义的马赛克)
     if ($video['mosaic']) {
         $mosaic = explode(',', $video['mosaic']);
         if (count($mosaic) == 4) {
             $mosaicArr = array('x' => $mosaic[0], 'y' => $mosaic[1], 'width' => $mosaic[2], 'height' => $mosaic[3]);
             $data['config']['mosaic'] = $mosaicArr;
         }
     } elseif ($video['mosaic_id']) {
         $mosaic_mode = new mosaic_mode();
         $mosaic_config = $mosaic_mode->detail($video['mosaic_id']);
         if ($mosaic_config) {
             $data['config']['mosaic'] = array('x' => $mosaic_config['x'], 'y' => $mosaic_config['y'], 'width' => $mosaic_config['width'], 'height' => $mosaic_config['height']);
         }
     }
     //指定不用加水印
     if ($video['no_water']) {
         $data['config']['water_mark'] = '';
     } else {
         if ($video['water_id']) {
             $water_mode = new water_config_mode();
             $waterInfo = $water_mode->detail($video['water_id']);
             if ($waterInfo) {
                 $data['config']['water_mark'] = $waterInfo['base_path'] . $waterInfo['img_path'];
                 if ($video['water_pos']) {
                     $_pos = explode(',', $video['water_pos']);
                 } else {
                     $_pos = explode(',', WATER_POS);
                     //如果没传位置,就用默认位置
                 }
                 $data['config']['water_mark_x'] = $_pos[0];
                 $data['config']['water_mark_y'] = $_pos[1];
             }
         }
     }
     //头信息
     if ($this->settings['metadata']) {
         $metadata = $this->settings['metadata'];
         foreach ($metadata as $k => $v) {
             if (!$v) {
                 unset($metadata[$k]);
             } else {
                 $metadata[$k] = urlencode($v);
             }
         }
         $data['metadata'] = $metadata;
     }
     //记录水印与马赛克的参数,在强制转码的时候可能会用到这些参数,因为在第一次转码的时候不一定是强制转码,这样水印与马赛克不一定有
     $_param = array('mosaic' => $data['config']['mosaic'], 'water' => array('water_mark' => $data['config']['water_mark'], 'water_mark_x' => $data['config']['water_mark_x'], 'water_mark_y' => $data['config']['water_mark_y']), 'metadata' => $data['metadata']);
     file_put_contents(DATA_DIR . $vid . '.json', json_encode($_param));
     //选取转码服务器
     if ($video['server_id']) {
         $tran_server = select_servers_by_id($video['server_id'], $vid);
     } else {
         $tran_server = select_servers($vid);
         //自动选择转码服务器
     }
     if (!$tran_server) {
         $this->errorOutput(NO_SELECT_TRANSERVER);
     }
     $trans = new transcode($tran_server);
     //根据选取到转码服务器是否需要携带文件,选择各自的提交方式
     if ($tran_server['need_file']) {
         $data['upload_file_in_callback'] = "1";
         $data['sourceFile'][0]['url'] = 'http://' . ltrim(SOURCE_VIDEO_DOMIAN, 'http://') . '/' . $source_arr['source'];
     }
     $ret = $trans->addTranscodeTask($data);
     $return = json_decode($ret, 1);
     if ($return['return'] == 'fail') {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$vid}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $vid);
         //将转码失败的信息记录下来
         $error_info = array('return' => 'success', 'transcode' => 'fail', 'id' => $vid, 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'img' => $img_info, 'ErrorCode' => '0x0025', 'ErrorText' => '提交转码失败');
         echo json_encode($error_info);
         //上传成功,转码失败
     } else {
         //返回数据
         $video_info = array('id' => $vid, 'protocol' => 'http://', 'host' => defined("TARGET_VIDEO_DOMAIN") ? ltrim(TARGET_VIDEO_DOMAIN, 'http://') : $this->settings['videouploads']['host'], 'dir' => $this->dir_info['target_dir'], 'file_name' => $this->dir_info['output_filename'], 'img' => $img_info, 'type' => $vod_config['output_format'], 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'return' => 'success');
         //请求用户提供的callback地址,并且携带用户提供的数据
         if ($video['callback_url']) {
             if ($video['callback_data']) {
                 $callback_data = json_decode(base64_decode($video['callback_data']), 1);
                 //此处在视频库里面记录电视剧的id
                 if ($callback_data['tv_play_id']) {
                     $sql = "UPDATE " . DB_PREFIX . "vodinfo SET tv_play_id = '" . $callback_data['tv_play_id'] . "' WHERE id = '" . $vid . "'";
                     $this->db->query($sql);
                 }
                 $video_info['callback_data'] = $callback_data;
             }
             if ($callbackReturnData = $this->mediaserverCallback($video['callback_url'], $video_info)) {
                 $callbackReturnData = json_decode($callbackReturnData, 1);
                 if ($callbackReturnData['ErrorCode']) {
                     $video_info['callback_return'] = $callbackReturnData;
                 } else {
                     $video_info['callback_return'] = $callbackReturnData[0];
                 }
             }
         }
         $this->addItem($video_info);
         $this->output();
     }
 }
 private function _get_form()
 {
     $form = new Forge("admin/transcode", "", "post", array("id" => "g-admin-transcode-form"));
     $group = $form->group("system")->label(t("System"));
     $ffmpegPath = transcode::whereis("ffmpeg");
     $codecs = $this->_get_supported_audio_codecs();
     $group->input("ffmpeg_path")->id("ffmpeg_path")->label(t("Path to ffmpeg binary:"))->value(module::get_var("transcode", "ffmpeg_path", $ffmpegPath))->callback("transcode::verify_ffmpeg_path")->error_messages("required", t("You must enter the path to ffmpeg"))->error_messages("invalid", t("File does not exist"))->error_messages("is_dir", t("File is a directory"))->message("Auto detected ffmpeg here: " . $ffmpegPath . "<br />Click <a href='javascript:verifyffmpeg();'>here</a> to verify ffmpeg path and continue.");
     $group->input("ffmpeg_flags")->id("ffmpeg_flags")->label(t("Extra ffmpeg flags:"))->value(module::get_var("transcode", "ffmpeg_flags"));
     $group->dropdown("audio_codec")->id("audio_codec")->label(t("Audio codec to use:"))->options($codecs)->selected(module::get_var("transcode", "audio_codec"));
     $group->checkbox("ffmpeg_audio_kbits")->label(t("Send audio bitrate as kbits instead of bits/s"))->checked(module::get_var("transcode", "ffmpeg_audio_kbits"));
     $group = $form->group("resolutions")->label(t("Resolutions"));
     $group->checkbox("resolution_240p")->label("240p")->checked(module::get_var("transcode", "resolution_240p"));
     $group->checkbox("resolution_360p")->label("360p")->checked(module::get_var("transcode", "resolution_360p"));
     $group->checkbox("resolution_480p")->label("480p")->checked(module::get_var("transcode", "resolution_480p"));
     $group->checkbox("resolution_576p")->label("576p")->checked(module::get_var("transcode", "resolution_576p"));
     $group->checkbox("resolution_720p")->label("720p")->checked(module::get_var("transcode", "resolution_720p"));
     $group->checkbox("resolution_1080p")->label("1080p")->checked(module::get_var("transcode", "resolution_1080p"));
     $form->submit("submit")->value(t("Save"));
     return $form;
 }
Exemple #15
0
function checkIsExistTranscode()
{
    //获取开启的转码服务器
    if (!($servers = hg_get_transcode_servers())) {
        return 0;
    }
    if (!class_exists('transcode')) {
        include_once CUR_CONF_PATH . 'lib/transcode.class.php';
    }
    foreach ($servers as $k => $v) {
        $t_server = array('host' => $v['trans_host'], 'port' => $v['trans_port']);
        $transcode = new transcode($t_server);
        $task_info = json_decode($transcode->get_transcode_tasks(), 1);
        if ($task_info['transcoding_tasks']) {
            return 1;
        }
    }
    return 2;
}
Exemple #16
0
 public function submit_transcode()
 {
     //构建提交转码的参数
     /*
     	if($this->input['vod_config_id'])
     	{
     		    	$type_id = $this->input['vod_config_id'];
     		    	$condition = " AND type_id=".$type_id;
     	}
     	else
     	{
     		    	$condition = " AND is_default=1";
     	}
     $vod_config = transcode_config($condition);
     */
     $vod_config = transcode_config($this->input['vod_config_id']);
     $is_file = 0;
     //标识是否是文件上传
     $is_url = 0;
     //标识是否是url提交
     if ($_FILES) {
         $max_size = ini_get('upload_max_filesize');
         if ($max_size) {
             if ($_FILES['videofile']['size'] > $max_size * 1024 * 1024) {
                 $this->errorOutput('上传视频不能超过' . $max_size . 'M');
             }
         }
         $is_file = 1;
         $this->check_has_videofile($vod_config);
         $this->input['filepath'] = $this->dir_info['filepath'];
     } elseif ($this->input['url']) {
         //检查url连接
         $is_url = 1;
         $url = $this->input['url'];
         $basename = basename($url);
         $r = strpos($basename, '.');
         //这里需要修改
         if (!$r) {
             $this->errorOutput('下载链接不正确');
         }
         //检查文件格式
         $filetype = strtolower(strrchr($basename, '.'));
         $allowtype = explode(',', $this->settings['video_type']['allow_type']);
         if (!in_array($filetype, $allowtype)) {
             $this->errorOutput(FORBIDTYPE);
         }
         if (!$this->input['title']) {
             $this->input['title'] = $basename;
         }
         //构建下载目录
         $dir = UPLOAD_DIR . 'url/' . hg_build_dowload_dir();
         //构建目标视频目录
         $v_videodir = create_video_dir();
         $vod_dir_names = $v_videodir[0];
         $video_dir = $v_videodir[1];
         if (!hg_mkdir(TARGET_DIR . $video_dir) || !is_writeable(TARGET_DIR . $video_dir)) {
             $this->errorOutput(NOWRITE);
         }
         $this->dir_info['target_dir'] = $video_dir;
         $this->dir_info['output_filename'] = $vod_dir_names . '';
         $this->dir_info['output_format'] = $vod_config['output_format'];
         //开始下载
         $re = $this->download_from_url($url, $dir);
         if ($re) {
             $this->input['filepath'] = strrchr($re, 'url');
         } else {
             $this->errorOutput('下载失败');
         }
         //如果不需要转码,拷贝一份到目标视频目录(注: IS_TRANSCODE_URL只用于url提交)
         if (!defined('IS_TRANSCODE_URL')) {
             //$source = $dir.basename($url);
             $dest = TARGET_DIR . $video_dir . $vod_dir_names . strrchr(basename($url), '.');
             $re = copy($re, $dest);
             if (!$re) {
                 $this->errorOutput('复制失败');
             }
         }
     } else {
         if (!$this->input['notcheck']) {
             if (!$this->input['filepath'] || !file_exists(UPLOAD_DIR . $this->input['filepath'])) {
                 $this->errorOutput(NOTFINDFILE);
             }
         }
         //如果没传title就取文件名
         if (!$this->input['title']) {
             $_path_name = basename($this->input['filepath']);
             $this->input['title'] = substr($_path_name, 0, strrpos($_path_name, '.'));
         }
         if ($this->settings['video_cloud'] && $this->settings['video_file_cloud'] == 1) {
             include CUR_CONF_PATH . 'lib/cloud/' . $this->settings['video_cloud'] . '.php';
             $cloud = new $this->settings['video_cloud']();
             $uparray = array('name' => $this->input['title'], 'tmp_name' => UPLOAD_DIR . $this->input['filepath'], 'size' => filesize(UPLOAD_DIR . $this->input['filepath']));
             $cloud->setFiles($uparray);
             $cloud->setInput($this->input);
             $cloud->setSettings($this->settings);
             $ret = $cloud->upload();
             $videoinfo = $cloud->getVideoInfo();
             $this->input['content_id'] = $videoinfo['content_id'];
             $this->input['extend_data'] = $videoinfo['extend_data'];
             $this->input['notranscode'] = $videoinfo['notranscode'];
             $this->dir_info['original'] = $original;
             if ($ret != 'success') {
                 $this->errorOutput($ret);
             }
         } else {
             $this->create_dir($vod_config);
         }
     }
     $video = $this->input;
     $v_vinfo = $this->storage_data($video, $vod_config);
     //如果不需要转码,则直接返回
     if (!defined('IS_TRANSCODE_URL') && $is_url) {
         //将转码状态置为1
         $sql = "UPDATE " . DB_PREFIX . "vodinfo SET status=1 WHERE id = '" . $v_vinfo['vid'] . "'";
         $this->db->query($sql);
         $this->addItem($v_vinfo);
         $this->output();
     }
     $vid = $v_vinfo['vid'];
     $img_info = $v_vinfo['img_info'];
     if ($video['audit_auto']) {
         $this->settings['App_mediaserver']['extends'] = $video['audit_auto'];
     } else {
         $defaultstate = $this->get_status_setting('create');
         if ($defaultstate) {
             $defaultstate = 2;
         }
         $this->settings['App_mediaserver']['extends'] = $defaultstate;
     }
     //传到转码服务器的duration参数的单位是秒,所以此处因为接受外部的时间单位是ms
     if (!$this->input['notranscode']) {
         if ($is_file || $is_url) {
             $duration = '';
             $start = '0';
         } else {
             $duration = $video['duration'] ? $video['duration'] : intval($video['end'] - $video['start']) / 1000 . '';
             $start = $video['start'];
         }
         $source_arr = array('source' => UPLOAD_DIR . $video['filepath'], 'start' => $start, 'duration' => $duration, 'is_water_marked' => '0');
         $this->settings['App_mediaserver']['dir'] = $this->settings['App_mediaserver']['dir'] . 'admin/';
         $data = array("sourceFile" => array($source_arr), "id" => "{$vid}", "app_id" => APPID, "app_key" => APPKEY, "type" => "transcode_upload", "targetDir" => TARGET_DIR . $this->dir_info['target_dir'], "output_filename" => $this->dir_info['output_filename'], "config" => $vod_config, "callback" => $this->settings['App_mediaserver'], "ts_need_preprocess" => $video['ts_need_preprocess'] ? '1' : '0', "mp4_from_sobey" => $video['mp4_from_sobey'] ? '1' : '0', "force_recodec" => $video['force_recodec'] ? '1' : '0', "absolute_path" => '1');
         //加马赛克(优先采用用户提供的自定义的马赛克)
         if ($video['mosaic']) {
             $mosaic = explode(',', $video['mosaic']);
             if (count($mosaic) == 4) {
                 $mosaicArr = array('x' => $mosaic[0], 'y' => $mosaic[1], 'width' => $mosaic[2], 'height' => $mosaic[3]);
                 $data['config']['mosaic'] = $mosaicArr;
             }
         } elseif ($video['mosaic_id']) {
             $mosaic_mode = new mosaic_mode();
             $mosaic_config = $mosaic_mode->detail($video['mosaic_id']);
             if ($mosaic_config) {
                 $data['config']['mosaic'] = array('x' => $mosaic_config['x'], 'y' => $mosaic_config['y'], 'width' => $mosaic_config['width'], 'height' => $mosaic_config['height']);
             }
         }
         //指定不用加水印
         if ($video['no_water']) {
             $data['config']['water_mark'] = '';
         } else {
             if ($video['water_id']) {
                 $water_mode = new water_config_mode();
                 $waterInfo = $water_mode->detail($video['water_id']);
                 if ($waterInfo) {
                     $data['config']['water_mark'] = $waterInfo['base_path'] . $waterInfo['img_path'];
                     if ($video['water_pos']) {
                         $_pos = explode(',', $video['water_pos']);
                     } else {
                         $_pos = explode(',', WATER_POS);
                         //如果没传位置,就用默认位置
                     }
                     $data['config']['water_mark_x'] = $_pos[0];
                     $data['config']['water_mark_y'] = $_pos[1];
                 }
             } else {
                 if ($data['config']['water_mark'] && $video['water_pos']) {
                     $_pos = explode(',', $video['water_pos']);
                     $data['config']['water_mark_x'] = $_pos[0];
                     $data['config']['water_mark_y'] = $_pos[1];
                 }
             }
         }
         //头信息
         if ($this->settings['metadata']) {
             $metadata = $this->settings['metadata'];
             foreach ($metadata as $k => $v) {
                 if (!$v) {
                     unset($metadata[$k]);
                 } else {
                     $metadata[$k] = urlencode($v);
                 }
             }
             $data['metadata'] = $metadata;
         }
         //记录水印与马赛克的参数,在强制转码的时候可能会用到这些参数,因为在第一次转码的时候不一定是强制转码,这样水印与马赛克不一定有
         $_param = array('mosaic' => $data['config']['mosaic'], 'water' => array('water_mark' => $data['config']['water_mark'], 'water_mark_x' => $data['config']['water_mark_x'], 'water_mark_y' => $data['config']['water_mark_y']), 'metadata' => $data['metadata']);
         if (!is_dir(UPLOAD_DIR . 'water/')) {
             hg_mkdir(UPLOAD_DIR . 'water/');
         }
         file_put_contents(UPLOAD_DIR . 'water/' . $vid . '.json', json_encode($_param));
         //选取转码服务器
         if ($video['server_id']) {
             $tran_server = select_servers_by_id($video['server_id'], $vid);
         } else {
             $tran_server = select_servers($vid);
             //自动选择转码服务器
         }
         if (!$tran_server) {
             $this->errorOutput(NO_SELECT_TRANSERVER);
         }
         $trans = new transcode($tran_server);
         //根据选取到转码服务器是否需要携带文件,选择各自的提交方式
         if ($tran_server['need_file']) {
             $data['upload_file_in_callback'] = "1";
             $data['sourceFile'][0]['url'] = 'http://' . ltrim(SOURCE_VIDEO_DOMIAN, 'http://') . '/' . $source_arr['source'];
         }
         $ret = $trans->addTranscodeTask($data);
         $return = json_decode($ret, 1);
     }
     if ($return['return'] == 'fail') {
         $sql = " UPDATE " . DB_PREFIX . "vodinfo SET status = -1 WHERE id = {$vid}";
         $this->db->query($sql);
         hg_do_transcode_fail($data, $vid);
         //将转码失败的信息记录下来
         $error_info = array('return' => 'success', 'transcode' => 'fail', 'tran_server' => $tran_server, 'id' => $vid, 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'img' => $img_info, 'ErrorCode' => '0x0025', 'ErrorText' => '提交转码失败');
         echo json_encode($error_info);
         //上传成功,转码失败
     } else {
         //返回数据
         $video_info = array('id' => $vid, 'protocol' => 'http://', 'host' => defined("TARGET_VIDEO_DOMAIN") ? ltrim(TARGET_VIDEO_DOMAIN, 'http://') : $this->settings['videouploads']['host'], 'dir' => $this->dir_info['target_dir'], 'file_name' => $this->dir_info['output_filename'], 'img' => $img_info, 'type' => $vod_config['output_format'], 'app' => APP_UNIQUEID, 'module' => MOD_UNIQUEID, 'tran_server' => $tran_server, 'return' => 'success');
         //请求用户提供的callback地址,并且携带用户提供的数据
         if ($video['callback_url']) {
             if ($video['callback_data']) {
                 $callback_data = json_decode(base64_decode($video['callback_data']), 1);
                 //此处在视频库里面记录电视剧的id
                 if ($callback_data['tv_play_id']) {
                     $sql = "UPDATE " . DB_PREFIX . "vodinfo SET tv_play_id = '" . $callback_data['tv_play_id'] . "' WHERE id = '" . $vid . "'";
                     $this->db->query($sql);
                 }
                 $video_info['callback_data'] = $callback_data;
             }
             if ($callbackReturnData = $this->mediaserverCallback($video['callback_url'], $video_info)) {
                 $callbackReturnData = json_decode($callbackReturnData, 1);
                 if ($callbackReturnData['ErrorCode']) {
                     $video_info['callback_return'] = $callbackReturnData;
                 } else {
                     $video_info['callback_return'] = $callbackReturnData[0];
                 }
             }
         }
         //可以接受用户传递的callback,这个callback是在转码完成之后回调
         if ($video['after_callback_url']) {
             if ($video['callback_data']) {
                 $callback_data = json_decode(base64_decode($video['callback_data']), 1);
                 //此处在视频库里面记录电视剧的id
                 if ($callback_data['tv_play_id']) {
                     $sql = "UPDATE " . DB_PREFIX . "vodinfo SET tv_play_id = '" . $callback_data['tv_play_id'] . "' WHERE id = '" . $vid . "'";
                     $this->db->query($sql);
                 }
             }
             //在data目录产生文件记录这个url
             $after_dir = DATA_DIR . 'after_callback_url/';
             if (!hg_mkdir($after_dir) || !is_writeable($after_dir)) {
                 $this->errorOutput(NOWRITE);
             }
             file_put_contents($after_dir . $vid . '.url', $video['after_callback_url']);
         }
         $this->addItem($video_info);
         $this->output();
     }
 }
Exemple #17
0
 public function get_transcode_task_info($arr)
 {
     $trans = new transcode($arr);
     $ret = $trans->get_transcode_status();
     $ret = json_decode($ret, 1);
     return $ret;
 }