阿里云多媒体处理视频截图转码

视频截图

视频截图支持指定视频媒体文件按照一定规则进行视频截图操作,此功能可以为视频生成预览图、或通过截图来做视频封面等使用场景。以下为使用OpenAPIJavaSDK创建转码任务的示例。

环境准备

在Maven项目中加入依赖项:

<dependency><groupid>com.cloud.sdk</groupid>core<version>1.0.0</version></dependency><dependency><groupid>com.cloud.sdk</groupid>mps<version>0.3.6</version></dependency>

更多环境准备信息请参考OpenAPIJavaSDK简介

视频转码

packagecom.cloud.mps.client;importcom.cloud.sdk.auth.StaticCredentialsProvider;importcom.cloud.sdk.http.HttpRequestConfig;importcom.cloud.sdk.http.Protocol;importcom.cloud.sdk.service.mps.client.MpsClient;importcom.cloud.sdk.service.mps.model.;publicclassMpsClientExample{publicstaticvoidmain(String[]args){//以下参数均为必填项Stringregion=&quot;cn-north-1&quot;;//cn-north-1/cn-south-1/cn-east-1/cn-east-2StringsourceBucketName=&quot;&quot;;//源bucketStringsourceObjectKey=&quot;&quot;;//源bucket中要截图的文件StringdestBucketName=&quot;&quot;;//截图放置的目标bucketStringaccessKey=&quot;&quot;;//用户AccessKeyStringsecretKey=&quot;&quot;;//用户SecretKeyMpsClientclient=MpsClient.builder().credentialsProvider(newStaticCredentialsProvider(accessKey,secretKey)).httpRequestConfig(newHttpRequestConfig.Builder().protocol(Protocol.HTTP).build()).build();//创建任务ThumbnailTaskSourcecreateSource=newThumbnailTaskSource().bucket(sourceBucketName).key(sourceObjectKey);ThumbnailTaskTargetcreateTarget=newThumbnailTaskTarget().destBucket(destBucketName);//设置截图规则,默认可不设置ThumbnailTaskRulerule=newThumbnailTaskRule();rule.setCount(1);//截图数量,mode=single时不可选.default:1rule.setStartTimeInSecond(1);//截图在视频中开始时间(秒),default:0rule.setEndTimeInSecond(10);//截图在视频中结束时间(秒),default:-1(代表视频时长)rule.setKeyFrame(true);//是否开启关键帧截图,default:truerule.setMode(&quot;multi&quot;);//截图模式单张:single多张:multi平均:averagedefault:singleCreateThumbnailTaskRequestcreateRequest=newCreateThumbnailTaskRequest().source(createSource).target(createTarget).rule(rule).regionId(region);CreateThumbnailTaskResultcreateResult=client.createThumbnailTask(createRequest).getResult();//获取任务GetThumbnailTaskRequestgetRequest=newGetThumbnailTaskRequest().taskId(createResult.getTaskID()).regionId(region);GetThumbnailTaskResultgetResult=client.getThumbnailTask(getRequest).getResult();//任务列表ListThumbnailTaskRequestlistRequest=newListThumbnailTaskRequest().regionId(region);ListThumbnailTaskResultlistResult=client.listThumbnailTask(listRequest).getResult();}}