阿里云视频直播代码

不得不佩服阿里的东西简单粗暴,从看api到代码完成2天搞定

package com.zhaohua.common.util;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Configuration;

import com.aliyuncs.DefaultAcsClient;

import com.aliyuncs.exceptions.ClientException;

import com.aliyuncs.exceptions.ServerException;

import com.aliyuncs.profile.DefaultProfile;

import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;

import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;

import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;

import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;

import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;

import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;

import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;

import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;

import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;

import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoRequest;

import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoResponse;

import com.zhaohua.nkt.vo.VideoUploadBean;

@Configuration

public class AliyunUtil {

public static String accessKeyID;

public static String accessKeySecret;

@Value("${aliyun.accessKeyID}")

public  void setAccessKeyID(String accessKeyID) {

AliyunUtil.accessKeyID = accessKeyID;

}

@Value("${aliyun.accessKeySecret}")

public  void setAccessKeySecret(String accessKeySecret) {

AliyunUtil.accessKeySecret = accessKeySecret;

}

/**

* 初始化客户端(获取key和secret)

*

*/

public static DefaultAcsClient initVodClient() {

    //点播服务所在的Region,国内请填cn-shanghai,不要填写别的区域

    String regionId = "cn-shanghai";

    DefaultProfile profile = DefaultProfile.getProfile(regionId, AliyunUtil.accessKeyID, AliyunUtil.accessKeySecret);

    DefaultAcsClient client = new DefaultAcsClient(profile);

    return client;

}

/**

* 获取播放凭证

*

* @param videoId 视频id

* @return GetVideoPlayAuthResponse

* @throws ClientException

* @throws ServerException

*/

public static GetVideoPlayAuthResponse getVideoPlayAuth(String videoId) throws ServerException, ClientException {

DefaultAcsClient client = AliyunUtil.initVodClient();

GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

request.setVideoId(videoId);

GetVideoPlayAuthResponse response = client.getAcsResponse(request);

return response;

}

/**

* 获取视频上传地址和凭证函数

* @param  VideoUploadBean videoUploadBean

*/

public static CreateUploadVideoResponse createUploadVideo(VideoUploadBean videoUploadBean) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

    CreateUploadVideoRequest request = new CreateUploadVideoRequest();

    request.setTitle(videoUploadBean.getTitle());

    request.setDescription(videoUploadBean.getDescription());

    request.setFileName(videoUploadBean.getFileName());

    request.setTags(videoUploadBean.getTags());

    request.setCoverURL(videoUploadBean.getCoverUrl());

    return client.getAcsResponse(request);

}

/**

* 刷新视频上传凭证

* @param DefaultAcsClient client

* @param videoId 视频id

* @return RefreshUploadVideoResponse

*/

public static RefreshUploadVideoResponse refreshUploadVideo(String videoId) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();

    request.setVideoId(videoId);

    return client.getAcsResponse(request);

}

/**

* 修改视频信息

*

* @param videoId 视频id

* @return GetVideoInfoResponse

*/

public static UpdateVideoInfoResponse updateVideoInfo(VideoUploadBean videoUploadBean) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

UpdateVideoInfoRequest request = new UpdateVideoInfoRequest();

request.setVideoId(videoUploadBean.getVideoId());

    request.setTitle(videoUploadBean.getTitle());

    request.setDescription(videoUploadBean.getDescription());

    request.setTags(videoUploadBean.getTags());

    return client.getAcsResponse(request);

}

/**

* 删除视频函数

*

* @param DefaultAcsClient client

* @param videoId 视频id

* @return GetVideoInfoResponse

*/

public static DeleteVideoResponse deleteVideo(String videoIds) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

DeleteVideoRequest request = new DeleteVideoRequest();

    //多个用逗号分隔,最多支持20个

//     request.setVideoIds("视频ID1,视频ID2");

    request.setVideoIds(videoIds);

    return client.getAcsResponse(request);

}

/**

* 获取视频信息

*

* @param videoId 视频id

* @return GetVideoInfoResponse

*/

public static GetVideoInfoResponse getVideoInfo(String videoId) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

GetVideoInfoRequest request = new GetVideoInfoRequest();

    request.setVideoId(videoId);

    return client.getAcsResponse(request);

}

/**

* 获取播放信息(包括播放地址)

*

* @param videoId 视频id

* @return GetVideoInfoResponse

*/

public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {

DefaultAcsClient client = AliyunUtil.initVodClient();

    GetPlayInfoRequest request = new GetPlayInfoRequest();

    request.setVideoId(videoId);

    return client.getAcsResponse(request);

}

}


package com.zhaohua.nkt.controller;import java.util.List;import javax.validation.Valid;import org.apache.log4j.Logger;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.JSONObject;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.exceptions.ServerException;import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse.Video;import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;import com.zhaohua.common.util.AliyunUtil;import com.zhaohua.framework.shiro.entity.RestEntity;import com.zhaohua.nkt.vo.VideoUploadBean;/** * 和阿里云视频相关的操作 * * @author 尹东亮 */@RestController@RequestMapping("/aliyun")public class AliyunController {private Logger logger = Logger.getLogger(getClass());/** * 刷新上传凭证 3000秒过期时间 * * @throws Exception */@RequestMapping(value = "/refreshUploadAuten/{videoId}", method = RequestMethod.GET)public Object refreshUploadAuten(@PathVariable String videoId) throws Exception {RefreshUploadVideoResponse response = AliyunUtil.refreshUploadVideo(videoId);JSONObject jsonObject = new JSONObject();jsonObject.put("uploadAddress", response.getUploadAddress());jsonObject.put("uploadAuth", response.getUploadAuth());return RestEntity.success(jsonObject);}/** * 获取视频上传地址和凭证 * * @param videoUploadBean * @throws Exception * */@RequestMapping(value = "/getUploadVideoUrlAuten", method = RequestMethod.POST)public Object getUploadVideoUrlAuten(@RequestBody VideoUploadBean videoUploadBean) throws Exception {CreateUploadVideoResponse response = AliyunUtil.createUploadVideo(videoUploadBean);JSONObject jsonObject = new JSONObject();jsonObject.put("videoId", response.getVideoId());jsonObject.put("uploadAddress", response.getUploadAddress());jsonObject.put("uploadAuth", response.getUploadAuth());return RestEntity.success(jsonObject);}/** * 删除视频,(最多一次可以删除20个) * * @throws Exception * */@RequestMapping(value = "/delete/{videoId}", method = RequestMethod.GET)public Object delete(@PathVariable String videoId) throws Exception {AliyunUtil.deleteVideo(videoId);return RestEntity.success("删除成功");}/** * 修改视频信息 * * @throws Exception * */@RequestMapping(value = "/update", method = RequestMethod.POST)public Object update(@RequestBody @Valid VideoUploadBean videoUploadBean, BindingResult bindingResult) throws Exception {if (bindingResult.hasErrors()) {StringBuffer sbf = new StringBuffer();bindingResult.getAllErrors().forEach(e -> {logger.error(e.getDefaultMessage());sbf.append(e.getDefaultMessage()).append(";");});return RestEntity.failed(sbf.toString());}AliyunUtil.updateVideoInfo(videoUploadBean);return RestEntity.success("更新成功");}/** * 获取单个视频信息 * * @throws Exception * */@RequestMapping(value = "/getVideoInfo/{videoId}", method = RequestMethod.GET)public Object getUploadImgUrlAuten(@PathVariable String videoId) throws Exception {GetVideoInfoResponse response = AliyunUtil.getVideoInfo(videoId);Video video=response.getVideo();return RestEntity.success(video);}/** * 获取临时播放url(url播放方式) * @throws Exception * * @throws ClientException * @throws ServerException * */@RequestMapping(value = "/getTempPlayUrl/{videoId}", method = RequestMethod.GET)public Object getTempPlayUrl(@PathVariable String videoId) throws Exception {GetPlayInfoResponse response = AliyunUtil.getPlayInfo(videoId);List playInfoList = response.getPlayInfoList();

// 播放地址

String playURL=null;

for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {

if(("mp4").equals(playInfo.getFormat())) {

playURL= playInfo.getPlayURL();

break;

}

}

JSONObject jsonObject = new JSONObject();

jsonObject.put("playURL", playURL);

return RestEntity.success(jsonObject);

}

/**

* 获取播放凭证(凭证播放方式)

* 100秒过期

* @throws ClientException

* @throws ServerException

*

*/

@RequestMapping(value = "/getPlayAuten/{videoId}", method = RequestMethod.GET)

public Object getPlayAuten(@PathVariable String videoId) throws ServerException, ClientException {

GetVideoPlayAuthResponse response = AliyunUtil.getVideoPlayAuth(videoId);

JSONObject jsonObject = new JSONObject();

jsonObject.put("palyAuten", response.getPlayAuth());

return RestEntity.success(jsonObject);

}

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容