package com.szzz.util;
import lombok.SneakyThrows;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class PushPullUrlUtil {
/**
* 输出Url
* @param startTime
*/
@SneakyThrows
public static Map<String ,String> printUrls(String startTime){
//startTime是字符串如"2021-08-11 10:55"
//过期时间
/* SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Long datetime = simpleDateFormat.parse(startTime).getTime();//将你的日期转换为时间戳
Long unixTime=datetime+43200000;//开始时间的12个小时后过期*/
//过期时间//Unix时间戳
Long unixTime = getUnixTime(startTime)+43200000;
//bizid
String bizId = "1567893";
//随机码 随机码自己随意填写---唯一不可重复这里用的时间戳
String randomStr = String.valueOf(System.currentTimeMillis());
//推流防盗链 推流主key----开通腾讯云账号可去域名管理那里获取
String key = "34a558e1cf81d34955";
String streamId = bizId+"_"+randomStr;
//时间戳16进制
String txTime = Integer.toHexString(unixTime.intValue()).toUpperCase();
//获取md5 txSecret
String txSecret = getMd5(key+streamId+txTime);
//视频推送url
String pushUrl = "rtmp://"+bizId+".livepush.myqcloud.com/live/"+streamId+"?bizid="+bizId+"&txSecret="+txSecret+"&txTime="+txTime;
//视频播放url rtmp
String playUrlRtmp = "rtmp://player.diasia.com/live/"+streamId;
//视频播放url flv
String playUrlFlv = "http://player.diasia.com/live/"+streamId+".flv";
//视频播放url hls
String playUrlHls = "http://player.diasia.com/live/"+streamId+".m3u8";
Map<String ,String> mapURL=new HashMap<>();
mapURL.put("pushUrl",pushUrl);
mapURL.put("playUrlRtmp",playUrlRtmp);
mapURL.put("playUrlFlv",playUrlFlv);
mapURL.put("playUrlHls",playUrlHls);
System.out.println("pushUrl推流地址="+pushUrl);
System.out.println("playUrlRtmp播放流="+playUrlRtmp);
System.out.println("playUrlFlv播放流="+playUrlFlv);
System.out.println("playUrlHls播放流="+playUrlHls);
return mapURL;
}
/**
* 获取unix时间戳
* @return
* @throws Exception
*/
public static Long getUnixTime (String dateStr) {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long epoch = df.parse(dateStr).getTime();
return epoch/1000;
} catch (ParseException e) {
e.printStackTrace();
}
return 0L;
}
/**
* 获取md5字符串
* @param str
* @return
*/
public static String getMd5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] bs = md5.digest(str.getBytes());
StringBuilder sb = new StringBuilder(40);
for(byte x:bs) {
if((x & 0xff)>>4 == 0) {
sb.append("0").append(Integer.toHexString(x & 0xff));
} else {
sb.append(Integer.toHexString(x & 0xff));
}
}
return sb.toString();
}
}
java生成推拉流地址工具类--腾讯云--直播
最新推荐文章于 2024-03-08 16:16:33 发布