VideosControllerApi.java
11.3 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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.service.VideosService;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author : chenhao
* @Date: 2018/12/6 11:17
* @Description: 送审接口
*/
@Controller
@RequestMapping("/api/videos")
public class VideosControllerApi {
private static Logger logger = LoggerFactory.getLogger(VideosControllerApi.class);
@Autowired
VideosService videosService;
@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);
boolean yorN;
//目击者类型 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);
//查询一下 存在,更新 没有,插入
ShyVideos shyVideos = new ShyVideos();
//g得到id去查询是否已有
String videoId = json.getString("videoId");
shyVideos.setVideo_id(videoId);
List<ShyVideos> shyVideosList = videosService.findShyVideo(shyVideos);
if (shyVideosList.size() > 0) {
logger.info("点播入库更新开始............................");
ShyVideos shyVideos1 = shyVideosList.get(0);
shyVideos1.setReceive(CommonConst.RECEIVE_BEFORE);
shyVideos1.setReceive_user_id(null);
shyVideos1.setUpdated_at(CommonUtils.getCurrentTime());
//封装数据入库
yorN = saveOrUpdateVideo(json, shyVideos1, source, duration, category, sid);
} else {
logger.info("点播入库保存开始............................");
yorN = saveOrUpdateVideo(json, shyVideos, source, duration, category, sid);
}
if (yorN) {
return new ResponseBean(ErrorEnum.SUCCESS);
} else {
return new ResponseBean(ErrorEnum.ERROR);
}
}
/**
* 保存 和更新 点播视频
*
* @param json
* @param shyVideos
* @param source
* @param duration
* @param category
* @param sid
* @return
*/
private boolean saveOrUpdateVideo(JSONObject json, ShyVideos shyVideos, String source, Integer duration, String category, String sid) {
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");
if (CommonUtils.isEmpty(poster)) {
//ugc短视频只有一张图片
poster = json.getJSONObject("videoCoverUrl").getString("key");
}
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.setVideo_md5(json.getString("videoMD5"));
shyVideos.setState(CommonConst.AUDIT_INTT);
shyVideos.setBucket(json.getString("bucketName"));
shyVideos.setPlat(json.getInteger("bucketType"));
shyVideos.setDuration(duration);
//传入栏目分类
shyVideos.setCategory(category);
//传入sid
shyVideos.setVideo_user_id(sid);
//获取审核类型
videosService.getShenheType(shyVideos);
if (shyVideos.getId() == null) {
shenHeRule(shyVideos, duration);
logger.info("当前视频类型是:" + shyVideos.getShenhe_type());
return videosService.impotVideo(shyVideos);
} else {
logger.info("当前视频类型是:" + shyVideos.getShenhe_type());
return videosService.updateShyVideos(shyVideos);
}
}
/**
* 点播视频保存审核规则
*
* @param shyVideos
* @param duration
*/
private void shenHeRule(ShyVideos shyVideos, Integer duration) {
if (!shyVideos.getShenhe_type().equals(CommonConst.AUDIT_TYPE_FREE)) {
//如果审核类型是人工审核,按照原有规则
if (shyVideos.getShenhe_type().equals(CommonConst.AUDIT_TYPE_USER)) {
if (CommonUtils.isNotNull(duration) && duration > 80) {
//视频至少可以抽8张,请求抽帧
avsample(shyVideos);
}
//如果审核类型是机审
} else if (shyVideos.getShenhe_type().equals(CommonConst.AUDIT_TYPE_MACHANE)) {
//"目击者"视频,小于300秒的抽帧
if (CommonUtils.isNotNull(duration) && duration < 300) {
avsample(shyVideos);
} else {
//机审超过300秒的改为人工审核
shyVideos.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
}
}
}
}
/**
* 申请抽帧
*
* @param video
* @return
*/
public ShyVideos avsample(ShyVideos video) {
try {
JSONObject params = new JSONObject();
String videoUrl = video.getVideo_url();
Integer plat = video.getPlat();
String bucket = video.getBucket();
Integer spid = video.getSpid();
String path = videoUrl.replace("//", "");
path = path.substring(path.indexOf("/"), path.length());
String domainName = videoUrl.replace(path, "");
path = "/" + bucket + path;
JSONObject attach = new JSONObject();
JSONObject extParam = new JSONObject();
//非“目击者”视频
if (!video.getCategory().equals(CommonConst.CATEGORY_WITNESS)) {
//默认抽帧时长 抽帧时长的算法:视频时长每增加一小时,抽帧时长增加10秒
Integer duration = video.getDuration();
Integer interval = duration / 3600 * 10 + 10;
attach.put("interval", interval);
attach.put("strategy", 1);
attach.put("duration", video.getDuration());
if (duration < 60) {
int num = duration / 10;
attach.put("num", num == 0 ? 1 : num);
}
} else {
//“目击者”视频时间大于4秒
if (video.getDuration() > 4) {
//间隔 单位s
attach.put("interval", 4);
//覆盖转码服务的默认抽帧图数量
attach.put("num", video.getDuration() / 4);
attach.put("duration", video.getDuration());
} else {
//抽帧图片张数
attach.put("num", 1);
//是否首帧,0:是。num=1时有效
attach.put("first", 0);
}
}
logger.info("抽帧attach参数:attach:{}", attach);
extParam.put("domain", domainName);
if (CommonConst.KS_PLAT.equals(plat)) {
//金山
params.put("plat", 0);
params.put("dstBucket", ks_bucket);
} else if (CommonConst.QINIU_PLAT.equals(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 = System.currentTimeMillis();
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;
}
}