生活随笔
收集整理的這篇文章主要介紹了
阿里云长视频上传以及返回播放地址
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
視頻上傳
下載上傳SDK
阿里云視頻上傳SDK下載
工具類
我自己只用到了本地上傳,所以只貼出本地上傳的Util,其他上傳方式可以自己看demo
AK信息在控制臺右上角頭像查看
package com
.example
.demo
.utils
;import com
.alibaba
.fastjson
.JSONObject
;
import com
.aliyun
.vod
.upload
.impl
.UploadVideoImpl
;
import com
.aliyun
.vod
.upload
.req
.*
;
import com
.aliyun
.vod
.upload
.resp
.*
;
import com
.aliyuncs
.DefaultAcsClient
;
import com
.aliyuncs
.IAcsClient
;
import com
.aliyuncs
.exceptions
.ClientException
;
import com
.aliyuncs
.exceptions
.ServerException
;
import com
.aliyuncs
.profile
.DefaultProfile
;
import com
.aliyuncs
.vod
.model
.v20170321
.GetPlayInfoRequest
;
import com
.aliyuncs
.vod
.model
.v20170321
.GetPlayInfoResponse
;
import com
.google
.gson
.Gson
;public class UploadVideoUtil {private static final String accessKeyId
= "<Your accessKeyId>";private static final String accessKeySecret
= "<Your accessKeySecret>";public static void main(String
[] args
) {JSONObject json
= UploadVideo("這里是視頻上傳到阿里的標(biāo)題,無關(guān)緊要","這里是文件本地絕對路徑,記得轉(zhuǎn)義");try {Thread
.sleep(10000);} catch (InterruptedException e
) {e
.printStackTrace();}String url
= GetPlayInfo((String
)json
.get("vid"));System
.out
.println(url
);}public static JSONObject
UploadVideo(String title
, String fileName
) {UploadVideoRequest request
= new UploadVideoRequest(accessKeyId
, accessKeySecret
, title
, fileName
);request
.setPartSize(2 * 1024 * 1024L
);request
.setTaskNum(1);request
.setIsShowWaterMark(false);UploadVideoImpl uploader
= new UploadVideoImpl();UploadVideoResponse response
= uploader
.uploadVideo(request
);System
.out
.print("RequestId=" + response
.getRequestId() + "\n"); JSONObject json
= new JSONObject();if (response
.isSuccess()) {System
.out
.print("VideoId=" + response
.getVideoId() + "\n");json
.put("status",1);json
.put("vid",response
.getVideoId());return json
;} else {if(response
.getVideoId()==null
||response
.getVideoId()=="") {json
.put("status",2);}System
.out
.print("ErrorCode=" + response
.getCode() + "\n");System
.out
.print("ErrorMessage=" + response
.getMessage() + "\n");return json
;}}public static String
GetPlayInfo(String vid
) {DefaultProfile profile
= DefaultProfile
.getProfile("cn-Shanghai", accessKeyId
, accessKeySecret
); IAcsClient client
= new DefaultAcsClient(profile
);GetPlayInfoRequest request1
= new GetPlayInfoRequest();request1
.setVideoId(vid
);String url
= null
;try {GetPlayInfoResponse response1
= client
.getAcsResponse(request1
);System
.out
.println(new Gson().toJson(response1
));for (GetPlayInfoResponse
.PlayInfo playInfo
: response1
.getPlayInfoList()) {System
.out
.println("PlayInfo.PlayURL = " + playInfo
.getPlayURL());String str
= playInfo
.getPlayURL();if(str
!= null
|| str
!= "") {if(str
.substring(str
.length()-3,str
.length()).equals("mp4")) {url
= playInfo
.getPlayURL();} }}return url
;} catch (ServerException e
) {e
.printStackTrace();} catch (ClientException e
) {System
.out
.println("ErrCode:" + e
.getErrCode());System
.out
.println("ErrMsg:" + e
.getErrMsg());System
.out
.println("RequestId:" + e
.getRequestId());}return null
;}}
總結(jié)
以上是生活随笔為你收集整理的阿里云长视频上传以及返回播放地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。