因为工作需要于是封装了一个工具类
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.live.v20180801.LiveClient;
import com.tencentcloudapi.live.v20180801.models.DayStreamPlayInfo;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveForbidStreamListRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveForbidStreamListResponse;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveStreamOnlineInfoRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveStreamOnlineInfoResponse;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveStreamOnlineListRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeLiveStreamOnlineListResponse;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamDayPlayInfoListRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamDayPlayInfoListResponse;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamPlayInfoListRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamPlayInfoListResponse;
import com.tencentcloudapi.live.v20180801.models.DropLiveStreamRequest;
import com.tencentcloudapi.live.v20180801.models.DropLiveStreamResponse;
import com.tencentcloudapi.live.v20180801.models.ForbidLiveStreamRequest;
import com.tencentcloudapi.live.v20180801.models.ForbidLiveStreamResponse;
import com.tencentcloudapi.live.v20180801.models.ForbidStreamInfo;
import com.tencentcloudapi.live.v20180801.models.PlayDataInfoByStream;
import com.tencentcloudapi.live.v20180801.models.ResumeLiveStreamRequest;
import com.tencentcloudapi.live.v20180801.models.ResumeLiveStreamResponse;
import com.tencentcloudapi.live.v20180801.models.StreamInfo;
import com.tencentcloudapi.live.v20180801.models.StreamOnlineInfo;
/**
* 直播使用的腾讯云操作工具
*
* @author hl128k
*
*/
public class TecentCloudUtil {
private static Logger logger = LoggerFactory.getLogger(TecentCloudUtil.class);
// 用于 生成推流防盗链的key
private static final String PUSH_KEY = "";
// API密钥id
private static final String SECRET_ID = "";
// API密钥key
private static final String SECRET_KEY = "";
// 用于 生成拉流防盗链的key 可无
private static final String LIVE_KEY = "";
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss'Z'");
private static final SimpleDateFormat FORMAT1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat FORMAT2 = new SimpleDateFormat("yyyy-MM-dd");
private static Credential credential = null;
private static LiveClient liveClient = null;
// 推流域名
public static final String PUSH_DOMAIN = "";
// 拉流域名
public static final String LIVE_DOMAIN = "";
// API回调地址
public static final String API_ADDRESS = "live.tencentcloudapi.com";
public static final String PUSH_DOMIAN_NAME = PUSH_DOMAIN + ".livepush.myqcloud.com";
public static final String LIVE_DOMIAN_NAME = LIVE_DOMAIN + ".livecdn.liveplay.myqcloud.com";
public static final String APP_NAME = "live";
/**
* 推流地址
*/
public static final String PUSH_URL = "rtmp://" + PUSH_DOMAIN + "/";
public static void main(String[] args) throws Exception {
DayStreamPlayInfo[] describeStreamPlayInfoList = describeStreamPlayInfoList("5CD1485C",
FORMAT2.format(new Date()) + "00:00:00", FORMAT1.format(new Date()));
for (DayStreamPlayInfo dayStreamPlayInfo : describeStreamPlayInfoList) {
HashMap<String, String> map = new HashMap<String, String>();
dayStreamPlayInfo.toMap(map, APP_NAME);
System.out.println(map.toString());
}
System.out.println(getPushUrlByMinute("888881", 30));
System.out.println(getPullFlvUrl("88888", null));
}
/**
* 实例化认证对象
*
* @return
*/
private static synchronized Credential getCredential() {
if (credential == null) {
credential = new Credential(SECRET_ID, SECRET_KEY);
}
return credential;
}
/**
* 实例化认证对象
*
* @return
*/
private static LiveClient getLiveClient() {
if (liveClient == null) {
liveClient = new LiveClient(getCredential(), "");
}
return liveClient;
}
/**
* 禁推 一个直播流 全参数
*
* @author hl128k
* @param streamName
* 直播流名字
* @param appName
* 应用名称
* @param resumeTime
* 禁推时长(单位分钟)
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String forbidLiveStream(String streamName, String appName, Long resumeTime)
throws TencentCloudSDKException {
Long currentTime = System.currentTimeMillis() + resumeTime * 60L * 1000L;
LiveClient client = getLiveClient();
ForbidLiveStreamRequest req = new ForbidLiveStreamRequest();
req.setAppName(appName);
req.setStreamName(streamName);
req.setDomainName(PUSH_DOMIAN_NAME);
req.setResumeTime(FORMAT.format(new Date(currentTime)));
;
ForbidLiveStreamResponse res = client.ForbidLiveStream(req);
return res.getRequestId();
}
/**
* 禁推 一个直播流
*
* @author hl128k
* @param streamName
* 直播流名字
* @param resumeTime
* 禁推时长(单位分钟)
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String forbidLiveStream(String streamName, Long resumeTime) throws TencentCloudSDKException {
return forbidLiveStream(streamName, APP_NAME, resumeTime);
}
/**
* 禁推 一个直播流 禁推30分钟自动生成
*
* @author hl128k
* @param streamName
* 直播流名字
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String forbidLiveStream(String streamName) throws TencentCloudSDKException {
return forbidLiveStream(streamName, APP_NAME, 30L);
}
/**
* 查询禁播列表
*
* @author hl128k
* @param PageNum
* 页数1开始
* @throws TencentCloudSDKException
*/
public static ForbidStreamInfo[] getForbidStreamList(Integer PageNum) throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DescribeLiveForbidStreamListRequest req = new DescribeLiveForbidStreamListRequest();
req.setPageNum(PageNum);
req.setPageSize(10);
DescribeLiveForbidStreamListResponse res = client.DescribeLiveForbidStreamList(req);
return res.getForbidStreamList();
}
/**
* 恢复一个被禁止的直播流 全参数
*
* @author hl128k
* @param streamName
* 直播流名字
* @param appName
* 应用名称
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String resumeLiveStream(String streamName, String appName) throws TencentCloudSDKException {
LiveClient client = getLiveClient();
ResumeLiveStreamRequest req = new ResumeLiveStreamRequest();
req.setAppName(appName);
req.setDomainName(PUSH_DOMIAN_NAME);
req.setStreamName(streamName);
ResumeLiveStreamResponse res = client.ResumeLiveStream(req);
return res.getRequestId();
}
/**
* 恢复一个被禁止的直播流
*
* @author hl128k
* @param streamName
* 直播流名字
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String resumeLiveStream(String streamName) throws TencentCloudSDKException {
return resumeLiveStream(streamName, APP_NAME);
}
/**
* 暂停一个直播流 全参数
*
* @author hl128k
* @param streamName
* 直播流名字
* @param appName
* 应用名称
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String dropLiveStream(String streamName, String appName) throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DropLiveStreamRequest req = new DropLiveStreamRequest();
req.setAppName(appName);
req.setDomainName(PUSH_DOMIAN_NAME);
req.setStreamName(streamName);
DropLiveStreamResponse res = client.DropLiveStream(req);
return res.getRequestId();
}
/**
* 暂停一个直播流 全参数
*
* @author hl128k
* @param streamName
* 直播流名字
* @return 请求 ID
* @throws TencentCloudSDKException
*/
public static String dropLiveStream(String streamName) throws TencentCloudSDKException {
return dropLiveStream(streamName, APP_NAME);
}
/**
* 在线推流信息列表
*
* @author hl128k
* @param PageNum
* 页码
* @param Status
* 0:未开始推流 1:正在推流
* @param streamName
* 直播流名字
* @return
* @throws TencentCloudSDKException
*/
public static StreamInfo[] describeLiveStreamOnlineInfo(Integer PageNum, Integer Status, String streamName)
throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DescribeLiveStreamOnlineInfoRequest req = new DescribeLiveStreamOnlineInfoRequest();
req.setPageNum(PageNum);
req.setStatus(Status);
req.setStreamName(streamName);
req.setPageSize(10);
DescribeLiveStreamOnlineInfoResponse res = client.DescribeLiveStreamOnlineInfo(req);
return res.getStreamInfoList();
}
/**
* 正在直播中的流列表
*
* @author hl128k
* @param PageNum
* 页码
* @param streamName
* 直播流名字
* @param appName
* 应用名称
* @throws TencentCloudSDKException
*/
public static StreamOnlineInfo[] describeLiveStreamOnlineList(Integer PageNum, String streamName)
throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DescribeLiveStreamOnlineListRequest req = new DescribeLiveStreamOnlineListRequest();
req.setAppName(APP_NAME);
req.setStreamName(streamName);
req.setPageNum(PageNum);
req.setPageSize(10);
DescribeLiveStreamOnlineListResponse res = client.DescribeLiveStreamOnlineList(req);
return res.getOnlineInfo();
}
/**
* 查询播放数据,支持按流名称查询详细播放数据,也可按播放域名查询详细总数据
*
* @author hl128k
* @param streamName
* 直播流名字
* @throws TencentCloudSDKException
*/
public static DayStreamPlayInfo[] describeStreamPlayInfoList(String streamName, String StartTime, String EndTime)
throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DescribeStreamPlayInfoListRequest req = new DescribeStreamPlayInfoListRequest();
req.setStreamName(streamName);
req.setStartTime(StartTime);
req.setEndTime(EndTime);
DescribeStreamPlayInfoListResponse res = client.DescribeStreamPlayInfoList(req);
return res.getDataInfoList();
}
/**
* 查询天维度每条流的播放数据,包括总流量等
*
* @author hl128k
* @param streamName
* 直播流名字
* @param DayTime
* 日期, 格式:YYYY-mm-dd。
* @throws TencentCloudSDKException
*/
public static PlayDataInfoByStream[] describeStreamDayPlayInfoList(String DayTime) throws TencentCloudSDKException {
LiveClient client = getLiveClient();
DescribeStreamDayPlayInfoListRequest req = new DescribeStreamDayPlayInfoListRequest();
req.setDayTime(DayTime);
DescribeStreamDayPlayInfoListResponse res = client.DescribeStreamDayPlayInfoList(req);
return res.getDataInfoList();
}
/**
* 这是推流防盗链的生成 KEY+ streamName + txTime
*
* @author hl128k
* @param key
* 防盗链使用的key
* @param streamName
* 通常为直播码
* @param txTime
* 到期时间
* @return
* @throws UnsupportedEncodingException
*/
public static String getSafeUrl(String key, String streamName, long txTime) throws UnsupportedEncodingException {
String input = new StringBuilder().append(key).append(streamName).append(Long.toHexString(txTime).toUpperCase())
.toString();
String txSecret = null;
txSecret = MD5.md5(input);
return txSecret == null ? ""
: new StringBuilder().append("txSecret=").append(txSecret).append("&").append("txTime=")
.append(Long.toHexString(txTime).toUpperCase()).toString();
}
/**
* 推流地址生成 全参数设置
*
* @author hl128k
* @param streamName
* 流名称/房间号
* @param appName
* 应用名称
* @param minute
* 过期时间
* @return
* @throws UnsupportedEncodingException
*/
public static String getPushUrl(String streamName, String appName, long minute)
throws UnsupportedEncodingException {
Long now = System.currentTimeMillis() + 60L * minute * 1000L;// 要转成long类型
// 分*秒*时*毫秒
// 当前毫秒数+需要加上的时间毫秒数 = 过期时间毫秒数
Long txTime = now / 1000;// 推流码过期时间秒数
String safeUrl = getSafeUrl(PUSH_KEY, streamName, txTime);
String realPushUrl = PUSH_URL + appName + "/" + streamName + "?" + safeUrl;
return realPushUrl;
}
/**
* 推流地址生成 minute分钟
*
* @author hl128k
* @throws UnsupportedEncodingException
*/
public static String getPushUrlByMinute(String streamName, long minute) throws UnsupportedEncodingException {
return getPushUrl(streamName, APP_NAME, minute);
}
/**
* 推流地址生成 Hour小时
*
* @author hl128k
* @throws UnsupportedEncodingException
*/
public static String getPushUrlByHour(String streamName, long Hour) throws UnsupportedEncodingException {
return getPushUrlByMinute(streamName, Hour * 60L);
}
/**
* 推流地址生成 day天
*
* @author hl128k
* @throws UnsupportedEncodingException
*/
public static String getPushUrlByDay(String streamName, long day) throws UnsupportedEncodingException {
return getPushUrlByHour(streamName, day * 24L);
}
/**
* 推流地址生成 30天
*
* @author hl128k
* @throws UnsupportedEncodingException
*/
public static String getPushUrl(String streamName) throws UnsupportedEncodingException {
return getPushUrlByDay(streamName, 30);
}
/**
* 获取拉流地址 全属性
*
* @author hl128k
* @param protocol
* 流类型
* @param streamName
* 直播流名字
* @param appName
* 应用名称
* @param resolution
* 清晰度(540,720,1080)
* @param suffix
* 流地址的后缀
* @return
*/
public static String getPullUrl(String protocol, String streamName, String appName, String resolution,
String suffix) {
String PullUrl = "";
PullUrl += ((protocol == null || protocol.isEmpty()) ? "rtmp" : protocol) + "://" + LIVE_DOMAIN + "/";
PullUrl += ((appName == null || appName.isEmpty()) ? APP_NAME : appName) + "/";
PullUrl += streamName;
PullUrl += (resolution == null || resolution.isEmpty()) ? "" : ("_" + resolution + "p");
PullUrl += (suffix == null || suffix.isEmpty()) ? "" : ("." + suffix);
return PullUrl;
}
public static String getPullRtmpUrl(String streamName, String resolution) {
return getPullUrl("rtmp", streamName, APP_NAME, resolution, null);
}
public static String getPullFlvUrl(String streamName, String resolution) {
return getPullUrl("http", streamName, APP_NAME, resolution, "flv");
}
public static String getPullM3u8Url(String streamName, String resolution) {
return getPullUrl("http", streamName, APP_NAME, resolution, "m3u8");
}
}
PUSH_KEY:腾讯云给的推流防盗链密钥。开通腾讯云直播即可获取
SECRET_ID:腾讯云的api Id。腾讯云授权管理里面设置。
SECRET_KEY:腾讯云的api 密钥。腾讯云授权管理里面设置。
PUSH_DOMAIN:腾讯云直播功能里自己设置的推流域名。Obs当中的推流服务器就是这个
LIVE_DOMAIN:腾讯云直播功能里自己设置的拉流域名。直播播放页面使用的就是这个域名跟参数播放的。
API_ADDRESS:腾讯云的api请求地址,腾讯云api操作是向某个接口发送携带相关参数的请求完成的。但是使用腾讯云提供的sdk就不用自己设置了。
PUSH_DOMIAN_NAME:完整的推流地址,在自己配置的地址的基础上拼接的腾讯云的直播地址。
LIVE_DOMIAN_NAME:完整的推流地址,在自己配置的地址的基础上拼接的腾讯云的直播地址。
APP_NAME:直播显示的项目名称,即“http://域名/live/88888.flv”中的‘live‘,注意这里生产拉流和推流的地方需要使用同一个appname,禁用和查询列表的时候也是使用同一个
LIVE_KEY:拉流的时候生成的防盗链密钥,因为我们使用的是每次直播使用一个新的流地址所以这个地方没有使用
FORMAT:云直播推流使用的是这个时间表示格式来计算密钥