文档

截图

更新时间:

视频截图是指对视频截取指定时间、指定尺寸的图片,用于生产视频封面、雪碧图、播放器进度条缩略图等场景,截图作业支持指定时间点、时间间隔、截图数量、类型以及是否拼图等。您可以通过媒体处理控制台、API或SDK提交截图作业。本文提供了Java SDK截图相关功能的API调用示例,包含提交截图作业、查询截图任务作业。

前提条件

使用前请先初始化客户端。详细操作,请参见初始化

提交截图任务

调用SubmitSnapshotJob接口,完成提交截图任务功能。

说明
  • 通过SDK提交作业时,Object需要经过URLEncode,否则会导致作业失败。更多信息,请参见URL Encoding说明

  • 请按照规范填写文件名称,否则会找不到文件导致作业失败。详细名称规范,请参见参数详情

  • 建议您在提交截图作业时记录任务的JobID,便于后续进行查询操作。

/**
 * 提交截图任务
 * @param client
 * @return
 * @throws Exception
 */
public static SubmitSnapshotJobResponse submitSnapshotJob(IAcsClient client) throws Exception{

    //创建API请求并设置参数
    SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();

    JSONObject input = new JSONObject();
    input.put("Bucket","<your bucket name>");
    input.put("Location","oss-cn-beijing");
    input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
    request.setInput(input.toJSONString());

    //截图输出路径
    JSONObject outputFile = new JSONObject();
    outputFile.put("Bucket","<your bucket name>");
    outputFile.put("Location","oss-cn-beijing");
    outputFile.put("Object",URLEncoder.encode("mps-test/demo/test-{Count}.jpg", "utf-8"));

    //此处示例为:从视频开头100秒的位置开始截图,均匀截取6张普通帧视频,不对截图进行拼图,不输出vtt文件
    JSONObject snapshotConfig = new JSONObject();
    snapshotConfig.put("OutputFile",outputFile.toJSONString());
    //截图开始时间
    snapshotConfig.put("Time","100");
    //截图时间间隔
    snapshotConfig.put("Interval","0");
    //截图数量
    snapshotConfig.put("Num","6");
    //截图类型 normal(默认) :普通帧、intra:关键帧
    snapshotConfig.put("FrameType","normal");

//        JSONObject subOut = new JSONObject();
//        //false:不拼图、true:拼图
//        subOut.put("IsSptFrag","true");
//        snapshotConfig.put("Format","vtt");
//        snapshotConfig.put("SubOut",subOut);

    request.setSnapshotConfig(snapshotConfig.toJSONString());
    request.setPipelineId(pipelineId);
    //request.setUserData("");

    return client.getAcsResponse(request);
}

查询截图任务结果

调用QuerySnapshotJobList接口,完成查询截图任务结果功能。

/**
 * 查询截图任务结果
 * @param client
 * @return
 * @throws Exception
 */
public static QuerySnapshotJobListResponse querySnapshotJobList(IAcsClient client) throws Exception{

    //创建API请求并设置参数
    QuerySnapshotJobListRequest request = new QuerySnapshotJobListRequest();

    //支持查询多个JobID,一次最多查询10个,使用半角逗号(,)分隔,通过submitSnapshotJob获取
    request.setSnapshotJobIds("47859822caba8aadddd4f6e7****");

    return client.getAcsResponse(request);
}

完整代码

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;

import java.net.URLEncoder;

/**
 * *****   使用须知     ******
 * 本demo为基本的截图作业示例,按视频时长平均截图
 *
 * *****   方法介绍     ******
 * submitSnapshotJob  提交截图任务作业 https://help.aliyun.com/document_detail/602857.html
 * querySnapshotJobList  查询截图任务结果 https://help.aliyun.com/document_detail/602858.html
 *
 * 更多截图参数请参考https://help.aliyun.com/document_detail/29253.htm#section-zh2-324-y2b
 */
public class SnapshotJob {

    //管道ID, 可以在MPS控制台 > 全局设置 > 管道及回调查看
    private static String pipelineId = "bee7a0a0cbf34a526****";

    public static void main(String[] args) throws ClientException {

        //初始化调用 client
        DefaultAcsClient client = InitClient.initMpsClient();

        SubmitSnapshotJobResponse response;
        try {
            response = submitSnapshotJob(client);
            System.out.println("RequestId is:" + response.getRequestId());
            System.out.println("JobId is:" + response.getSnapshotJob().getId());
        } catch (Exception e) {
            e.printStackTrace();
        }

//        QuerySnapshotJobListResponse response;
//        try {
//            response = querySnapshotJobList(client);
//            System.out.println("RequestId is:" + response.getRequestId());
//            System.out.println("SnapshotJobList is:" + JSON.toJSON(response.getSnapshotJobList()));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 提交截图任务
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitSnapshotJobResponse submitSnapshotJob(IAcsClient client) throws Exception{

        //创建API请求并设置参数
        SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();

        JSONObject input = new JSONObject();
        input.put("Bucket","<your bucket name>");
        input.put("Location","oss-cn-beijing");
        input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
        request.setInput(input.toJSONString());

        //截图输出路径
        JSONObject outputFile = new JSONObject();
        outputFile.put("Bucket","<your bucket name>");
        outputFile.put("Location","oss-cn-beijing");
        outputFile.put("Object",URLEncoder.encode("mps-test/demo/test-{Count}.jpg", "utf-8"));


        //此处示例为:从视频开头100秒的位置开始截图,均匀截取6张普通帧视频,不对截图进行拼图,不输出vtt文件
        JSONObject snapshotConfig = new JSONObject();
        snapshotConfig.put("OutputFile",outputFile.toJSONString());
        //截图开始时间
        snapshotConfig.put("Time","100");
        //截图时间间隔
        snapshotConfig.put("Interval","0");
        //截图数量
        snapshotConfig.put("Num","6");
        //截图类型 normal(默认) :普通帧、intra:关键帧
        snapshotConfig.put("FrameType","normal");

//        JSONObject subOut = new JSONObject();
//        //false:不拼图、true:拼图
//        subOut.put("IsSptFrag","true");
//        snapshotConfig.put("Format","vtt");
//        snapshotConfig.put("SubOut",subOut);

        request.setSnapshotConfig(snapshotConfig.toJSONString());
        request.setPipelineId(pipelineId);
        //request.setUserData("");

        return client.getAcsResponse(request);
    }

    /**
     * 查询截图任务结果
     * @param client
     * @return
     * @throws Exception
     */
    public static QuerySnapshotJobListResponse querySnapshotJobList(IAcsClient client) throws Exception{

        //创建API请求并设置参数
        QuerySnapshotJobListRequest request = new QuerySnapshotJobListRequest();

        //支持查询多个JobID, 一次最多查询10个,使用半角逗号(,)分隔,通过submitSnapshotJob获取
        request.setSnapshotJobIds("47859822caba8aa421ddd****");

        return client.getAcsResponse(request);
    }
}

            

相关文档

  • 本页导读 (1)
文档反馈