VideosControllerApi.java 12.1 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
import com.cnlive.shenhe.serviceImpl.UsersServiceImpl;
import com.cnlive.shenhe.serviceImpl.VideosServiceImpl;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chenhao
 * @Date: 2018/12/6 11:17
 * @Description: 视频送审接口
 */
@Controller
@RequestMapping("/api/videos")
public class VideosControllerApi {
    private static Logger logger = LoggerFactory.getLogger(VideosControllerApi.class);
    @Autowired
    AuditPass Pass;
    @Autowired
    VideosServiceImpl videosService;
    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    SitesServiceImpl sitesService;

    @Value("${avsmple_appKey}")
    String appkey;

    @Value("${avsmple_appSecret}")
    String appSecret;

    @Value("${avsmple_url}")
    String url;

    @Value("${avsmple_callback}")
    String callback;

    @Value("${qn_bucket}")
    String qn_bucket;

    @Value("${ks_bucket}")
    String ks_bucket;

    @Value("${desdir}")
    String desdir;

    @PostMapping("/import")
    @ResponseBody
    public ResponseBean impotVideo(String param) {
        logger.info("点播入库,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        ShyVideos shyVideos1 = new ShyVideos();
        //g得到视频id去查询是否已有
        String videoId = json.getString("videoId");
        shyVideos1.setVideo_id(videoId);
        //目击者类型  0-目击者 1-童声共济 2-全民健身 3-目击者政府监督
        String source = json.getString("source");
        Integer duration = json.getInteger("duration");
        if (CommonUtils.isNotNull(duration)) duration = duration / 1000;
        //获取栏目分类  
        String category=CommonUtils.isEmpty(json.getString("multiplecategory"))?"":json.getString("multiplecategory");
        //获取视频上传人的id
        String sid=CommonUtils.isEmpty(json.getString("videoClipsName"))?"":json.getString("videoClipsName");
        logger.info("videoClipsName:"+sid);
        sid=CommonUtils.isEmpty(sid)?"":sid.split("_")[2];
        logger.info("sid:"+sid);
        //初始化一个审核状态
//        Integer shenheType=3;//审核类型,1:免审,2:机审,3:人工审
//        shenheType=videosService.getShenheType(json.getInteger("spId"),category,sid);

        //查询一下 存在,更新 没有,插入
        List<ShyVideos> listshyVideos = videosService.findshyVideo(shyVideos1);
        if (listshyVideos.size() > 0) {
            ShyVideos shyVideos = listshyVideos.get(0);
            //封装数据入库
            shyVideos.setVideo_id(videoId);
            shyVideos.setSource(source);
            shyVideos.setVideo_title(json.getString("videoTitle"));
            shyVideos.setVideo_url(json.getString("videoUrl"));
            String videoCoverUrl = json.getString("videoCoverUrl");
            shyVideos.setVideo_imgs(videoCoverUrl);
            if (CommonUtils.isNotEmpty(videoCoverUrl)) {
                String poster = json.getJSONObject("videoCoverUrl").getString("224*126");
                shyVideos.setVideo_poster(poster);
            }
            shyVideos.setVideo_desc(json.getString("videoIntro"));
            shyVideos.setVideo_tags(json.getString("videoTag"));
            shyVideos.setVideo_keyword(json.getString("videoKeyword"));
            shyVideos.setVideo_md5(json.getString("videoMD5"));
            shyVideos.setVideo_upload_time(json.getDate(" uploadTime"));
            shyVideos.setVideo_priority(json.getString("priority"));
            shyVideos.setCallback(json.getString("callback"));
            shyVideos.setSpid(json.getInteger("spId"));
            shyVideos.setState(CommonConst.AUDIT_INTT);
            shyVideos.setBucket(json.getString("bucketName"));
            shyVideos.setPlat(json.getInteger("bucketType"));
            shyVideos.setDuration(duration);
            shyVideos.setCategory(category);//传入栏目分类
            shyVideos.setVideo_user_id(sid);//传入sid
            shyVideos.setReceive(CommonConst.RECEIVE_BEFORE);
            shyVideos.setReceive_user_id(null);
            shyVideos.setUpdated_at(CommonUtils.getCurrentTime());
            //获取审核类型
            videosService.getShenheType(shyVideos);
            boolean YorN = videosService.updateshyVideos(shyVideos);
            if (YorN == true) {
                return new ResponseBean(ErrorEnum.SUCCESS);
            } else {
                return new ResponseBean(ErrorEnum.ERROR);
            }
        }
        //查了,没有,直接插入数据库
        shyVideos1.setSource(source);
        shyVideos1.setVideo_title(json.getString("videoTitle"));
        shyVideos1.setVideo_url(json.getString("videoUrl"));
        String videoCoverUrl = json.getString("videoCoverUrl");
        shyVideos1.setVideo_imgs(videoCoverUrl);
        if (CommonUtils.isNotEmpty(videoCoverUrl)) {
            String poster = json.getJSONObject("videoCoverUrl").getString("224*126");
            if (CommonUtils.isEmpty(poster)) {
                //ugc短视频只有一张图片
                poster = json.getJSONObject("videoCoverUrl").getString("key");
            }
            shyVideos1.setVideo_poster(poster);
        }
        shyVideos1.setVideo_desc(json.getString("videoIntro"));
        shyVideos1.setVideo_tags(json.getString("videoTag"));
        shyVideos1.setVideo_keyword(json.getString("videoKeyword"));
        shyVideos1.setVideo_upload_time(json.getDate("uploadTime"));
        shyVideos1.setVideo_priority(json.getString("priority"));
        shyVideos1.setCallback(json.getString("callback"));
        shyVideos1.setSpid(json.getInteger("spId"));
        shyVideos1.setVideo_md5(json.getString("videoMD5"));
        shyVideos1.setState(CommonConst.AUDIT_INTT);
        shyVideos1.setBucket(json.getString("bucketName"));
        shyVideos1.setPlat(json.getInteger("bucketType"));
        shyVideos1.setDuration(duration);
        shyVideos1.setCategory(category);//传入栏目分类
        shyVideos1.setVideo_user_id(sid);//传入sid
        videosService.getShenheType(shyVideos1);//获取审核类型

        if(shyVideos1.getShenhe_type()!=1){
            if(shyVideos1.getShenhe_type()==CommonConst.AUDIT_TYPE_USER){//如果审核类型是人工审核,按照原有规则
                if (CommonUtils.isNotNull(duration) && duration > 80) {
                    shyVideos1 = avsample(shyVideos1);//视频至少可以抽8张,请求抽帧
                }
            }else if(shyVideos1.getShenhe_type()==CommonConst.AUDIT_TYPE_MACHANE){//如果审核类型是机审
                if (CommonUtils.isNotNull(duration) && duration < 60) {//"目击者"视频,小于60秒的抽帧
                    shyVideos1 = avsample(shyVideos1);
                }else{//机审超过60秒的改为人工审核
                    shyVideos1.setShenhe_type(3);
                }
            }
        }
        logger.info("当前视频类型是:"+shyVideos1.getShenhe_type());
        boolean result = videosService.impotVideo(shyVideos1);

        if (result == true) {
            return new ResponseBean(ErrorEnum.SUCCESS);
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }
    }

    /**
     * 申请抽帧
     *
     * @param video
     * @return
     */
    public ShyVideos avsample(ShyVideos video) {
        try {
            JSONObject params = new JSONObject();
            String video_url = video.getVideo_url();
            Integer plat = video.getPlat();
            String bucket = video.getBucket();
            Integer spid = video.getSpid();

            String path = video_url.replace("//", "");
            path = path.substring(path.indexOf("/"), path.length());
            String domian = video_url.replace(path, "");
            path = "/" + bucket + path;

            JSONObject attach = new JSONObject();
            JSONObject extParam = new JSONObject();
            
            if(!video.getCategory().equals(CommonConst.CATEGORY_WITNESS)){//非“目击者”视频
            	Integer interval=10;//默认抽帧时长
            	interval=video.getDuration()/3600*10+10;//抽帧时长的算法:视频时长每增加一小时,抽帧时长增加10秒
            	attach.put("interval", interval);
            	attach.put("strategy", 1);
            	attach.put("duration", video.getDuration());
            }else{
            	if(video.getDuration()>4){//“目击者”视频时间大于4秒
            		attach.put("interval", 4);//间隔 单位s
            		attach.put("num", video.getDuration()/4);//覆盖转码服务的默认抽帧图数量
            		attach.put("duration", video.getDuration());
//            		attach.put("strategy", 1);//是否从头抽到尾(1:是,优先级大于num)
            	}else{
            		attach.put("num", 1);//抽帧图片张数
            		attach.put("first", 0);//是否首帧,0:是。num=1时有效
            	}
            }
            
            
            logger.info("抽帧attach参数:attach:{}", attach);
            extParam.put("domain", domian);
            if (CommonConst.KS_PLAT == plat) {
                //金山
                params.put("plat", 0);
                params.put("dstBucket", ks_bucket);

            } else if (CommonConst.QINIU_PLAT == plat) {
                //七牛
                params.put("plat", 1);
                params.put("dstBucket", qn_bucket);
            }
            String dstDir = desdir + "/" + spid + "/" + CommonUtils.getCurrentDate("yyyy/MM/dd");
            String formats = path.substring(path.indexOf(".") + 1);
            params.put("path", path);
            params.put("dstDir", dstDir);
            params.put("type", "avsample");
            params.put("formats", formats.toLowerCase());
            params.put("spId", spid);
            params.put("cbUrl", callback);
            params.put("attach", attach);
            params.put("extParam", extParam);
            logger.info("抽帧params参数:params:{}", params);
            long timestamp = new Date().getTime();
            String sign = CommonUtils.md5(URLEncoder.encode(appkey + timestamp + params.toJSONString() + appSecret, "utf-8"));
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("timestamp", timestamp + "");
            dataMap.put("params", params.toJSONString());
            dataMap.put("appKey", appkey);
            dataMap.put("sign", sign);
            logger.info("抽帧dataMap参数:dataMap:{}", dataMap);
            HttpUtil httpUtil = HttpUtil.init();
            httpUtil.setParamMap(dataMap);
            Map<String, String> resultMap = httpUtil.post(url);
            JSONObject result = JSON.parseObject(resultMap.get("result"));
            logger.info("请求抽帧结果:result:{}", result);
            Integer code = result.getInteger("code");
            if (code == 0) {
                video.setAvsample(CommonConst.AVSAMPLE_ING);
            } else {
                video.setAvsample(CommonConst.AVSAMPLE_FAIL);
            }
            video.setTask_id(result.getString("taskId"));
            video.setAvsample_msg(result.toJSONString());
        } catch (Exception e) {
            logger.error("请求抽帧失败,video_id:{}", video.getVideo_id(), e);
            video.setAvsample(CommonConst.AVSAMPLE_FAIL);
        }
        return video;
    }

}