package com.qxueyou.scc.base;
|
|
import java.io.BufferedReader;
|
import java.io.ByteArrayOutputStream;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.qxueyou.scc.base.dao.CommonDAO;
|
import com.qxueyou.scc.base.model.Result;
|
import com.qxueyou.scc.media.model.MediaVideo;
|
import com.qxueyou.scc.sys.service.IOssService;
|
|
/**
|
* aliyun×ÊÔ´ÁÙʱÊÚȨ
|
* @author cyq
|
*
|
*/
|
@Controller
|
@RequestMapping(value = "/sts")
|
public class StsCtrl {
|
@SuppressWarnings("unused")
|
private static final String M3U8 = "m3u8";
|
|
private static final String MP4 = "mp4";
|
|
@SuppressWarnings("unused")
|
private static final String AES_VIDEO_KEY = "u9WH6ldpMd29cJ0Z";
|
|
private final Logger log = LogManager.getLogger(StsCtrl.class);
|
|
@Autowired
|
IOssService ossService;
|
|
/**
|
* ¹«¹²Êý¾Ý·ÃÎʶÔÏó
|
*/
|
@Autowired
|
private CommonDAO commonDAO;
|
|
/**
|
* »ñÈ¡ÊÓÆµµÄm3u8·ÃÎÊȨÏÞ£¬ÓûñÈ¡ÁÙʱ·ÃÎÊȨÏÞµÄkey·¾¶×÷Ϊ½âÃÜÃÜÔ¿£¬ÁÙʱÉú³Ém3u8Îļþ£¬²¢ÉÏ´«µ½°¢ÀïÔÆ£¬²¢·µ»ØÂ·¾¶
|
* @param videoId
|
* @param definition
|
* @return
|
*/
|
@RequestMapping(value="video/m3u8", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
|
public @ResponseBody Result videoM3u8(@RequestParam("videoId") String videoId, String definition) {
|
if (StringUtils.isEmpty(videoId)) {
|
return new Result(false, "²ÎÊý´íÎó");
|
}
|
|
// json.put("result", "success");
|
// // ¼ì²é»º´æÖÐÊÇ·ñÓпÉÓõÄurl£¬Èç¹ûÓУ¬ÔòÖ±½Ó·µ»Ø
|
// String urlCache = cacheService.get("sts_url_" + resId, String.class);
|
// if (StringUtils.isNotEmpty(urlCache)) {
|
// json.put("url", urlCache);
|
|
|
BufferedReader bf = null;
|
ByteArrayOutputStream byteArrOS =null;
|
try {
|
String def = definition;
|
MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
|
|
if (video == null) {
|
return new Result(false, "×ÊÔ´²»´æÔÚ");
|
}
|
|
String resId = "";
|
String resUrl = null;
|
JSONObject json = null;
|
|
//ÅжÏÊÇ·ñÊÇmp3µÈÒôÀÖ¸ñʽÎļþ£¬Èç¹ûÊÇÔòÖ±½Ó·µ»ØoriginalµØÖ·
|
if(checkIsVoiceMedia(video)){
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
return new Result(true, "", json);
|
}
|
|
resUrl = this.getM3u8ResUrl(def, video);
|
|
//ÅжÏÊÇ·ñÊǼÓÃÜÊÓÆµ£¬Î´¼ÓÃÜÔòÖ±½Ó·µ»ØÊÓÆµµØÖ·
|
if(StringUtils.isEmpty(video.getSecretKey())){
|
//Èç¹ûδתÂë¼ÓÃÜ£¬ÇÒm3u8ÊÓÆµ²»´æÔÚ£¬Ôò³¢ÊÔ¶ÁÈ¡MP4ÊÓÆµ¡£
|
if (StringUtils.isEmpty(resUrl)) {
|
resUrl = StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD() : video.getAndroidLD();
|
|
if (StringUtils.isEmpty(resUrl)) {
|
return new Result(false, "×ÊÔ´²»´æÔÚ");
|
}
|
}
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
}else{
|
//Èç¹ûתÂë¼ÓÃܺó£¬ÒÀÈ»²»ÄܶÁÈ¡µ½M3U8ÊÓÆµ»ò¶ÁÈ¡µ½µÄÊÇMP4Ö±½Ó·µ»Ø£¬ÌáʾÊÓÆÁ²»´æÔÚ
|
if (StringUtils.isEmpty(resUrl)||resUrl.endsWith(MP4)) {
|
return new Result(false, "×ÊÔ´²»´æÔÚ");
|
}
|
|
resId = resUrl.substring(resUrl.indexOf("/video/") + 1);
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", resId);
|
json.put("isConver",true);
|
|
|
// String resDir = resId.substring(0, resId.lastIndexOf('/'));
|
|
//»ñÈ¡ÔÊÓÆµVideoId
|
// String keyAddr = video.getOriginVideoId();
|
// if(StringUtils.isBlank(keyAddr)){
|
// keyAddr = video.getVideoId();
|
// }
|
|
// // »ñÈ¡½âÃÜkeyµÄÁÙʱ·ÃÎÊȨÏÞ
|
// if(StringUtils.isNotEmpty(video.getM3u8KeyPath())){
|
// json.put("url", video.getM3u8KeyPath().substring(video.getM3u8KeyPath().indexOf("/video/") + 1));
|
// }else{
|
// String m3u8KeyPath = resDir + "/" + keyAddr + ".key";
|
// }
|
|
|
|
// String m3u8ResKey = resDir + "/" + uuid + ".m3u8";
|
|
// InputStream in = ossService.getObject(resId);
|
// String temp=null;
|
// byteArrOS=new ByteArrayOutputStream();
|
// bf = new BufferedReader(new InputStreamReader(in));
|
//
|
// while ((temp = bf.readLine()) != null) {
|
// if (temp.startsWith("#EXT-X-KEY")) {
|
// temp = temp.replace("_x.key", ".key");
|
// }
|
// temp= temp.concat("\n");
|
// byteArrOS.write(temp.getBytes());
|
// }
|
|
|
}
|
return new Result(true, "", json);
|
} catch (Exception e) {
|
log.error(e, e);
|
return new Result(false);
|
}finally{
|
IOUtils.closeQuietly(bf);
|
IOUtils.closeQuietly(byteArrOS);
|
}
|
}
|
|
|
/**
|
* »ñÈ¡ÊÓÆµµÄm3u8·ÃÎÊȨÏÞ£¬ÓûñÈ¡ÁÙʱ·ÃÎÊȨÏÞµÄkey·¾¶×÷Ϊ½âÃÜÃÜÔ¿£¬ÁÙʱÉú³Ém3u8Îļþ£¬²¢ÉÏ´«µ½°¢ÀïÔÆ£¬²¢·µ»ØÂ·¾¶
|
* @param videoId
|
* @param definition
|
* @return
|
*/
|
@SuppressWarnings("unused")
|
@RequestMapping(value="video/mp4", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
|
public @ResponseBody Result videoMp4(@RequestParam("videoId") String videoId, String definition) {
|
if (StringUtils.isEmpty(videoId)) {
|
return new Result(false, "²ÎÊý´íÎó");
|
}
|
|
BufferedReader bf = null;
|
ByteArrayOutputStream byteArrOS =null;
|
try {
|
String def = definition;
|
MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
|
|
if (video == null) {
|
return new Result(false, "×ÊÔ´²»´æÔÚ");
|
}
|
|
String resId = "";
|
String resUrl = null;
|
JSONObject json = null;
|
|
//ÅжÏÊÇ·ñÊÇmp3µÈÒôÀÖ¸ñʽÎļþ£¬Èç¹ûÊÇÔòÖ±½Ó·µ»ØoriginalµØÖ·
|
if(checkIsVoiceMedia(video)){
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
|
json.put("isConver",false);
|
return new Result(true, "", json);
|
}
|
|
resUrl = this.getMp4ResUrl(def, video);
|
|
//Èç¹ûתÂëµØÖ·²»´æÔÚ£¬²¢ÇÒÔ´ÂëµØÖ·ÊÇmp4ÔòÖ±½ÓʹÓÃÔÊÓÆµµØÖ·²¥·Å
|
if(StringUtils.isEmpty(resUrl) && video.getOrigUrl().endsWith(MP4)){
|
resUrl = video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1);
|
}
|
|
//²é¿´ÔÊÓÆµÊÇ·ñÊÇMP4
|
if (StringUtils.isEmpty(resUrl)) {
|
return new Result(false, "×ÊÔ´²»´æÔÚ");
|
}
|
|
json = new JSONObject();
|
json.put("result", "success");
|
json.put("url", resUrl.substring(resUrl.indexOf("/video/") + 1));
|
json.put("isConver",false);
|
|
return new Result(true, "", json);
|
} catch (Exception e) {
|
log.error(e, e);
|
return new Result(false);
|
}finally{
|
IOUtils.closeQuietly(bf);
|
IOUtils.closeQuietly(byteArrOS);
|
}
|
}
|
|
|
/**
|
* »ñÈ¡resurl
|
*
|
* @param def
|
* @param video
|
* @return
|
*/
|
private String getM3u8ResUrl(String def, MediaVideo video){
|
return StringUtils.isNotEmpty(video.getIosHD()) ? video.getIosHD() : StringUtils.isNotEmpty(video.getIosSD()) ? video.getIosSD() : video.getIosLD();
|
}
|
|
/**
|
* »ñÈ¡resurl
|
*
|
* @param def
|
* @param video
|
* @return
|
*/
|
private String getMp4ResUrl(String def, MediaVideo video){
|
return StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD(): video.getAndroidLD();
|
}
|
|
/**
|
* ÅжÏÊÇ·ñÊÇÒôƵmedia,¾«È·ÅжÏÐèҪö¾ÙÒôƵ¸ñʽ
|
* @return
|
*/
|
private boolean checkIsVoiceMedia(MediaVideo media){
|
if(media==null || StringUtils.isEmpty(media.getOrigUrl())){
|
return false;
|
}
|
|
return !(StringUtils.isNotEmpty(media.getAndroidHD())
|
||StringUtils.isNotEmpty(media.getAndroidSD())
|
||StringUtils.isNotEmpty(media.getAndroidLD())
|
||StringUtils.isNotEmpty(media.getIosHD())
|
||StringUtils.isNotEmpty(media.getIosSD())
|
||StringUtils.isNotEmpty(media.getIosLD()));
|
}
|
|
/**
|
* ²âÊÔ½Ó¿ÚרÓÃ
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value="test", method=RequestMethod.GET)
|
public @ResponseBody String test(HttpServletRequest request, HttpServletResponse response) {
|
return null;
|
}
|
}
|