更新时间:2024-07-29 GMT+08:00
新建截图任务
核心代码
- 创建截图任务请求。
新建截图任务请求包括输入文件、输出文件的路径。具体参数请参考新建截图任务接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//设置截图输入视频地址 ObsObjInfo input = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01").withObject("ok.mp4"); //设置截图输出路径 ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01").withObject("output"); //创建截图请求 CreateThumbnailsTaskRequest req = new CreateThumbnailsTaskRequest() .withBody(new CreateThumbReq().withInput(input).withOutput(output) //设置截图类型,此处理按时间点截图 .withThumbnailPara(new ThumbnailPara().withType(ThumbnailPara.TypeEnum.DOTS) //设置截图输出文件名称 .withOutputFilename("photo") //设置截图的时间点 .withDots(Collections.singletonList(2)) //设置截图的宽 .withWidth(480) //设置截图的高 .withHeight(360)));
说明:生成的截图文件按截图时间戳命名,从首帧开始截取,中间按时间间隔截取,最后末帧截取一张。如视频文件20s,截图间隔为11s,则生成的截图文件为0.jpg,11.jpg,20.jpg。
- 发送创建截图任务请求并显示返回消息。
1 2
CreateThumbnailsTaskResponse rsp = initMpcClient().createThumbnailsTask(req); System.out.println("CreateThumbnailsTaskResponse=" + JsonUtils.toJSON(rsp));
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
package SdkTestCase.thumbnail; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.http.HttpConfig; import com.huaweicloud.sdk.core.utils.JsonUtils; import com.huaweicloud.sdk.mpc.v1.MpcClient; import com.huaweicloud.sdk.mpc.v1.model.CreateThumbReq; import com.huaweicloud.sdk.mpc.v1.model.CreateThumbnailsTaskRequest; import com.huaweicloud.sdk.mpc.v1.model.CreateThumbnailsTaskResponse; import com.huaweicloud.sdk.mpc.v1.model.ObsObjInfo; import com.huaweicloud.sdk.mpc.v1.model.ThumbnailPara; import com.obs.services.internal.ServiceException; import java.util.Collections; public class TestThumbnail { /** * 初始化 MpcClient * @return */ public static MpcClient initMpcClient() { HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3); //http代理设置,请根据实际情况设置 //httpConfig.withProxyHost("xxxxxx").withProxyPort(xxxxxx).withProxyUsername("xxxxxx"). // withProxyPassword("xxxxxx"); //根据实际填写ak,sk,在控制台账号名下“我的凭证 > 访问密钥”上创建和查看您的AK/SK String ak = System.getenv("SDK_AK"); String sk = System.getenv("SDK_SK"); //根据实际填写项目ID,在控制台账号名下“我的凭证 > API凭证”下查看您的项目ID String projectId = System.getenv("PROJECT_ID"); //根据实际填写所需endpoint,这里以“region01”为例 String endpoint = "https://mpc.region01.myhuaweicloud.com"; BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId); return MpcClient.newBuilder() .withHttpConfig(httpConfig) .withCredential(auth) .withEndpoint(endpoint) .build(); } /** * 创建截图任务 * @param args */ public static void main(String[] args) { //设置截图输入视频地址 ObsObjInfo input = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01").withObject("ok.mp4"); //设置截图输出路径 ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01").withObject("output"); //创建截图请求 CreateThumbnailsTaskRequest req = new CreateThumbnailsTaskRequest() .withBody(new CreateThumbReq().withInput(input).withOutput(output) //设置截图类型,此处理按时间点截图 .withThumbnailPara(new ThumbnailPara().withType(ThumbnailPara.TypeEnum.DOTS) //设置截图文件名称 .withOutputFilename("photo") //设置截图的时间点 .withDots(Collections.singletonList(2)) //设置截图的宽 .withWidth(480) //设置截图的高 .withHeight(360))); //发送截图请求 try { CreateThumbnailsTaskResponse rsp = initMpcClient().createThumbnailsTask(req); System.out.println("CreateThumbnailsTaskResponse=" + JsonUtils.toJSON(rsp)); } catch (ClientRequestException | ConnectionException | RequestTimeoutException | ServiceException e) { System.out.println(e); } } } |
父主题: 抽帧截图