VideosControllerApi.java 8.58 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.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;

    @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);
        Integer duration = json.getInteger("duration");
        if (CommonUtils.isNotNull(duration)) duration = duration / 1000;
        //查询一下 存在,更新 没有,插入
        List<ShyVideos> listshyVideos = videosService.findshyVideo(shyVideos1);
        if (listshyVideos.size() > 0) {
            ShyVideos shyVideos = listshyVideos.get(0);
            //封装数据入库
            shyVideos.setVideo_id(videoId);
            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_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);
            boolean YorN = videosService.updateshyVideos(shyVideos);
            if (YorN == true) {
                return new ResponseBean(ErrorEnum.SUCCESS);
            } else {
                return new ResponseBean(ErrorEnum.ERROR);
            }
        }
        //查了,没有,直接插入数据库
        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.setState(CommonConst.AUDIT_INTT);
        shyVideos1.setBucket(json.getString("bucketName"));
        shyVideos1.setPlat(json.getInteger("bucketType"));
        shyVideos1.setDuration(duration);
        if (CommonUtils.isNotNull(duration) && duration > 80) {
            shyVideos1 = avsample(shyVideos1);//视频至少可以抽8张,请求抽帧
        }
        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();
            attach.put("interval", 10);
            attach.put("strategy", 1);
            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);
            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);
            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;
    }

}