直播截图提供实时截图功能,满足您在观看直播同时的截图需求。本文详细为您介绍视频直播截图功能。
前提条件
视频直播截图功能需要将截图文件存放在Bucket中,因此需要开通OSS服务并且拥有至少一个Bucket用于存储这些截图文件。更多详情,请参见开通OSS服务和创建存储空间。
Bucket必须与播流域名的直播中心同地域,不支持跨地域截图。
直播截图规则
直播截图是针对推流的
AppName
为粒度设置的。您可以设置某个AppName
下所有推流的截图规则。说明如果设置
AppName
为星号(*),则表示截图规则适用于该直播域名下的所有推流。直播截图功能配置时,可以定义覆盖截图和实时截图两种类型。
覆盖截图是每次新生成截图覆盖之前生成的截图文件。
实时截图是每次新生成的截图不覆盖之前生成的截图文件,截图递增存储。
目前直播截图仅支持生成JPG格式。
说明两种类型可同时设置,且必须设置其中之一。
直播截图功能介绍
新配置的截图模板对正在进行的直播流不生效,需要重新推流才生效。
视频截图功能配置
配置视频直播截图功能可以通过以下两种方法:
控制台配置截图功能
- 登录视频直播控制台。
在左侧导航栏选择功能管理>直播截图,进入直播截图页面,选择截图配置页签。
选择待配置的播流域名。
单击添加。
配置截图模板。
截图模板参数及说明如下表所示。
参数
描述
AppName
视频的应用名称,输入的AppName必须与直播推流的AppName保持一致,方可生效。如果您想要进行域名级别截图配置,输入星号(*)即可。
截图频率
截图频率,单位:秒,取值范围:5~3600。
存储位置
选择截图文件在OSS上的存储位置。
OSS Bucket必须与播流域名的直播中心同地域,不支持跨地域截图。
存储方式
存储方式分两种类型,支持复选。
覆盖截图:视频截图按照截图频率依次截图,新截取的图片覆盖上一张旧图。
实时截图:视频截图按照截图频率依次截图,新图片按照N+1(N≥0)的顺序依次存储在OSS中。
说明复选时,会同时以两种方式进行截图。
说明配置完成后该域名下所有AppName(设置好的AppName),都会按照设置好的模板配置进行截图并存储。
截图设置修改后,会在下次推流后生效。
单击确定。
API配置截图功能
// This file is auto-generated, don't edit it. Thanks.
package demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
public class AddLiveAppSnapshotConfig {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/live
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
AddLiveAppSnapshotConfigRequest addLiveAppSnapshotConfigRequest = AddLiveAppSnapshotConfigRequest.builder()
.domainName("<Your DomainName>")
.appName("<Your AppName>")
.timeInterval(5)
.ossEndpoint("<Your OssEndpoint>")
.ossBucket("<Your OssBucket>")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<AddLiveAppSnapshotConfigResponse> response = client.addLiveAppSnapshotConfig(addLiveAppSnapshotConfigRequest);
// Synchronously get the return value of the API request
AddLiveAppSnapshotConfigResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}
配置成功后,重新推流将会产生截图文件。截图文件将会储存至OSS的<BucketName>中,由于同时配置了覆盖截图和实时截图文件路径,将会同时产生覆盖截图文件和实时截图文件。覆盖截图文件储存路径为<{AppName}/{StreamName}.jpg>。实时截图文件储存路径为<{AppName}/{StreamName}/{UnixTimestamp}.jpg>。
若对覆盖截图和实时截图的区别还不了解,请参见文档开头部分直播截图规则。
更多参数说明请参见AddLiveAppSnapshotConfig。
截图文件管理
查看截图文件
您可以通过三种方式查看截图文件。
控制台查看截图文件
- 登录视频直播控制台。
在左侧导航栏选择功能管理>直播截图,进入直播截图页面,选择截图管理。
选择待查询的域名及时间,然后输入或选择AppName和StreamName。
单击查询,查看选择时间段内的截图。若截图无法查看,请确定OSS Bucket读写权限是否为公共读,具体操作,请参见配置OSS bucket读写权限。
将鼠标移动到某一图片上方,可以查看大图或复制截图的流地址。
API查看截图文件
// This file is auto-generated, don't edit it. Thanks.
package demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
public class DescribeLiveStreamSnapshotInfo {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/live
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
DescribeLiveStreamSnapshotInfoRequest describeLiveStreamSnapshotInfoRequest = DescribeLiveStreamSnapshotInfoRequest.builder()
.domainName("<Your DomainName>")
.appName("<Your AppName>")
.streamName("<Your StreamName>")
.startTime("<Your StartTime>")
.endTime("<Your EndTime>")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<DescribeLiveStreamSnapshotInfoResponse> response = client.describeLiveStreamSnapshotInfo(describeLiveStreamSnapshotInfoRequest);
// Synchronously get the return value of the API request
DescribeLiveStreamSnapshotInfoResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}
目前只支持查询一年内其中某一天(EndTime和StartTime之间的间隔不能超过1天)的截图文件。
截图类型配置为覆盖截图+实时截图的情况下,只能查询出实时截图的文件内容。若在此情况下要查看覆盖截图的截图文件,请在OSS中查看,详情可参见OSS列举文件。
接口暂不支持分页,且一次最多返回100条数据(默认返回10条数据)。
更多查询截图文件内容接口说明,请参见DescribeLiveStreamSnapshotInfo。
OSS查看截图文件
在配置截图功能时,您已指定了截图文件在OSS中的存储地址。如果需要在OSS中查看截图文件,可参见OSS列举文件。
删除截图文件
视频直播储存的是截图文件信息,如AppName,StreamName,OSS文件储存路径等,文件是储存在OSS中。如果在删除截图时需要同步删除储存在OSS中的文件,则需要创建服务角色AliyunMTSVideoLifecycleRole,并精确授权AliyunMTSVideoLifecycleRolePolicy系统策略。
详细授权过程
在直播控制台删除截图或录制文件时需要先进行授权,您需要按照如下步骤在RAM控制台创建并授权RAM角色后,再登录直播控制台删除截图或录制文件。
创建普通服务角色
使用RAM管理员登录RAM控制台。
在左侧导航栏,选择身份管理 > 角色。
在角色页面,单击创建角色。
在创建角色页面,选择信任主体类型为云服务,然后选择具体的阿里云服务,最后单击确定。
说明信任主体名称下拉框中请选择“媒体处理”。
在弹出的创建角色对话框,角色名称栏中输入“AliyunMTSVideoLifecycleRole”作为角色名称,然后单击确定。
角色创建成功后,在角色基本信息页面下选择权限管理页签。
点击精确授权,请按照以下参数配置:
选择权限类型:系统策略
输入策略名称:AliyunMTSVideoLifecycleRolePolicy
配置完成后,单击确定。
您可以通过三种方式删除截图文件:
控制台删除截图文件
- 登录视频直播控制台。
在左侧导航栏选择功能管理>直播截图,进入直播截图页面,选择截图管理。
选择待查询的域名及时间,然后输入或选择AppName和StreamName。
将鼠标移动到某一图片上方,单击删除按钮可进行单张删除。
勾选多张截图后单击批量删除,可批量删除截图。最多可支持批量删除200张截图。
API删除截图文件
// This file is auto-generated, don't edit it. Thanks.
package demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
public class DeleteSnapshotFiles {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/live
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
DeleteSnapshotFilesRequest deleteSnapshotFilesRequest = DeleteSnapshotFilesRequest.builder()
.regionId("<Your RegionId>")
.domainName("<Your DomainName>")
.appName("<Your AppName>")
.streamName("<Your StreamName>")
.removeFile(true)
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<DeleteSnapshotFilesResponse> response = client.deleteSnapshotFiles(deleteSnapshotFilesRequest);
// Synchronously get the return value of the API request
DeleteSnapshotFilesResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}
目前只支持删除一年内的截图文件。
截图文件时间戳可通过查询截图文件获取。详情可参见查看截图文件。
对于覆盖截图的截图文件,目前只能在OSS中进行删除,具体操作可参见OSS删除文件。
更多删除直播截图文件接口说明,请参见DeleteSnapshotFiles。
以上示例实现了对截图文件的删除(同时删除了储存在OSS中的文件)。
您可以为截图功能设置回调事件,获取回调参数将截图信息保存至您自己的业务系统(同时储存时间戳),在需要删除文件时,您就可以根据您系统的策略(如通过定时任务每天定时删除N个月以前的截图文件)查询出截图文件时间戳对截图文件进行删除。若想了解截图回调,可参见直播截图回调开发指南。
OSS删除截图文件
一般情况下,不建议您直接删除存储在OSS中的截图文件。如果需要删除存储在OSS中的截图文件,可以通过视频直播删除截图文件时同步删除存储在OSS中的文件。如果您因为业务需求需要直接删除存储在OSS中的截图文件,可参考文档OSS删除文件。
相关文档
使用Java SDK,请参见Java SDK使用说明。
更多直播截图API,可参见直播截图相关API。