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; }
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(); }