VideosControllerApi.java
8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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);
//查询一下 存在,更新 没有,插入
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"));
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");
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 = avsample(shyVideos1);//请求抽帧
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("num", 24);
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;
}
}