阿里云视频点播服务Vod工具类——实现视频上传、删除、播放

阿里云视频点播服务Vod工具类——实现视频上传、删除、播放

  • 阿里云视频点播服务Vod工具类
    • maven依赖
    • 工具类实现
    • 工具类的测试
      • 获取视频
        • 1. 获取单个视频的播放地址和ID:
        • 2. 获取所有视频的播放地址和ID:
      • 上传视频
      • 删除视频

阿里云视频点播服务Vod工具类

maven依赖

如果报错或者是下载不下来,这里需要你把maven的镜像调成aliyun的才行


    com.aliyun
    aliyun-java-sdk-core
    4.4.4


    com.aliyun
    aliyun-java-sdk-vod
    1.0.9

工具类实现

需要注意的是,你需要将ACCESS_KEY_ID、ACCESS_KEY_SECRET替换成你自己的阿里云账号的AccessKey信息,将REGION_ID替换成你自己的VOD服务所在的区域。同时,视频文件的路径需要根据你自己的实际情况来修改。

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import java.util.ArrayList;
import java.util.List;
public class VodUtil {
    // 阿里云账号AccessKey信息
    private static final String ACCESS_KEY_ID = "your_access_key_id";
    private static final String ACCESS_KEY_SECRET = "your_access_key_secret";
    // VOD客户端信息
    private static final String REGION_ID = "cn-shanghai";
    private static final String ENDPOINT = "https://vod." + REGION_ID + ".aliyuncs.com";
    // 默认视频封面
    private static final String DEFAULT_COVER_URL = "http://vod-image-sample.oss-cn-hangzhou.aliyuncs.com/example.jpg";
    // 初始化VOD客户端
    public static DefaultAcsClient init() {
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        client.setEndpoint(ENDPOINT);
        return client;
    }
    // 创建上传视频请求
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client, String title, String fileName) throws ClientException {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);
        return client.getAcsResponse(request);
    }
    // 上传视频文件
    public static UploadVideoResponse uploadVideo(DefaultAcsClient client, String uploadAddress, String uploadAuth, String filePath, String fileName) throws ClientException {
        UploadVideoRequest request = new UploadVideoRequest(uploadAddress, uploadAuth, filePath, fileName);
        return client.getAcsResponse(request);
    }
    // 获取视频播放地址
    public static String getPlayUrl(DefaultAcsClient client, String videoId) throws ClientException {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        GetPlayInfoResponse response = client.getAcsResponse(request);
        List playInfoList = response.getPlayInfoList();
        if (playInfoList != null && !playInfoList.isEmpty()) {
            return playInfoList.get(0).getPlayURL();
        }
        return null;
    }
    // 获取视频列表
    public static List getVideoList(DefaultAcsClient client) throws ClientException {
        List videoIds = new ArrayList<>();
        SearchMediaRequest request = new SearchMediaRequest();
        request.setPageSize(100);
        SearchMediaResponse response = client.getAcsResponse(request);
        List mediaList = response.getMediaList();
        for (SearchMediaResponse.Media media : mediaList) {
            videoIds.add(media.getVideo().getVideoId());
        }
        return videoIds;
    }
    // 删除视频
    public static void deleteVideo(DefaultAcsClient client, String videoId) throws ClientException {
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        client.getAcsResponse(request);
    }
    // 上传视频并返回视频ID和播放地址
    public static VideoInfo uploadAndPlay(String title, String filePath) throws ClientException {
        DefaultAcsClient client = init();
        // 创建上传视频请求
        CreateUploadVideoResponse uploadResponse = createUploadVideo(client, title, filePath);
        String videoId = uploadResponse.getVideoId();
        String uploadAddress = uploadResponse.getUploadAddress();
        String uploadAuth = uploadResponse.getUploadAuth();
        // 上传视频文件
        UploadVideoResponse response = uploadVideo(client, uploadAddress, uploadAuth, filePath, filePath);
        // 获取视频播放地址
        String playUrl = getPlayUrl(client, videoId);
        // 设置视频封面
        SetVideoCoverRequest coverRequest = new SetVideoCoverRequest();
        coverRequest.setVideoId(videoId);
        coverRequest.setCoverURL(DEFAULT_COVER_URL);
        client.getAcsResponse(coverRequest);
        // 封装视频信息并返回
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setVideoId(videoId);
        videoInfo.setPlayUrl(playUrl);
        return videoInfo;
    }
    // 获取所有视频的播放地址和ID
    public static List getAllVideos() throws ClientException {
        DefaultAcsClient client = init();
        List videoIds = getVideoList(client);
        List videoInfoList = new ArrayList<>();
        for (String videoId : videoIds) {
            String playUrl = getPlayUrl(client, videoId);
            VideoInfo videoInfo = new VideoInfo();
            videoInfo.setVideoId(videoId);
            videoInfo.setPlayUrl(playUrl);
            videoInfoList.add(videoInfo);
        }
        return videoInfoList;
    }
    // 删除视频
    public static void deleteVideo(String videoId) throws ClientException {
        DefaultAcsClient client = init();
        deleteVideo(client, videoId);
    }
}

工具类的测试

获取视频

1. 获取单个视频的播放地址和ID:

VideoInfo videoInfo = VodUtil.getVideo("video_id");
System.out.println("VideoId: " + videoInfo.getVideoId());
System.out.println("PlayUrl: " + videoInfo.getPlayUrl());

2. 获取所有视频的播放地址和ID:

List videoInfoList = VodUtil.getAllVideos();
for (VideoInfo videoInfo : videoInfoList) {
    System.out.println("VideoId: " + videoInfo.getVideoId());
    System.out.println("PlayUrl: " + videoInfo.getPlayUrl());
}

上传视频

VideoInfo videoInfo = VodUtil.uploadAndPlay("video_title", "video_file_path");
System.out.println("VideoId: " + videoInfo.getVideoId());
System.out.println("PlayUrl: " + videoInfo.getPlayUrl());

删除视频

VodUtil.deleteVideo("video_id");

你可能感兴趣的:(java,微服务,阿里云,音视频,java)