Пример #1
0
 public function download_and_transcode()
 {
     //下载
     $data = $this->input;
     if (!$data) {
         return false;
     }
     $is_forcecode = intval($data['is_forcecode']);
     $url = $data['hostwork'] . '/' . $data['video_path'] . $data['video_filename'];
     //通过url下载视频
     $fileformat = substr($data['video_filename'], strrpos($data['video_filename'], '.'));
     $v_videodir = create_video_dir();
     $vod_dir_names = $v_videodir[0];
     $video_dir = $v_videodir[1];
     $filename = $vod_dir_names . $fileformat;
     $video_path = $video_dir;
     $video_base_path = TARGET_DIR;
     $video_filename = $filename;
     $source_hostwork = defined("SOURCE_VIDEO_DOMIAN") ? 'http://' . ltrim(SOURCE_VIDEO_DOMIAN, 'http://') : '';
     $source_base_path = UPLOAD_DIR;
     $source_path = 'yun/' . hg_build_dowload_dir();
     $source_filename = $data['video_filename'];
     $filepath = $source_base_path . $source_path;
     if (hg_mkdir($filepath)) {
         $file = file_get_contents($url);
         file_put_contents($filepath . $data['video_filename'], $file);
     }
     if (!hg_mkdir(TARGET_DIR . $video_dir) || !is_writeable(TARGET_DIR . $video_dir)) {
         $this->errorOutput(NOWRITE);
     }
     $sql = "UPDATE " . DB_PREFIX . "vodinfo SET source_hostwork='" . $source_hostwork . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  video_base_path = '" . $video_base_path . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  source_base_path = '" . $source_base_path . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  source_path = '" . $source_path . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  video_path = '" . $video_path . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  video_filename = '" . $video_filename . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  source_filename = '" . $source_filename . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  is_forcecode = '" . $is_forcecode . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t  hostwork = 'http://" . TARGET_VIDEO_DOMAIN . "'\n\t\t\t\t\t\t\t\t\t\t\t\t\t  WHERE id = '" . $data['id'] . "'";
     $this->db->query($sql);
     if (defined('IS_TRANSCODE') && IS_TRANSCODE) {
         $sql = "UPDATE " . DB_PREFIX . "vodinfo SET status=0 WHERE id = '" . $data['id'] . "'";
         $this->db->query($sql);
         $ret = $this->submit_transcode($tr_data, $video_dir, $vod_dir_names, $data['id'], unserialize($data['img_info']));
         if (!$ret) {
             $ret['error'] = 'transcode';
         }
     } else {
         $target_file = TARGET_DIR . $video_path . $video_filename;
         $ret = copy($filepath . $data['video_filename'], $target_file);
         if (!$ret) {
             $ret['error'] = 'download';
         }
     }
     $this->publish_video($data['id'], 'insert');
     $this->addItem($ret);
     $this->output();
 }
Пример #2
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();
     }
 }