SpringBoot 整合 阿里直播推流 OBS

因为离职,没有登录阿里账号查看,只在代码记录的一些

代码 工具类 LiveUtils

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

public class LiveUtils {

    /**
     * 计算md5
     * @param param
     * @return
     */
    public static String md5(String param) {
        if(param == null || param.length() == 0) {
            return null;
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(param.getBytes());
            byte[] byteArray = md5.digest();

            BigInteger bigInt = new BigInteger(1, byteArray);
            // 参数16表示16进制
            String result = bigInt.toString(16);
            // 不足32位高位补零
            while(result.length() < 32) {
                result = "0" + result;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 生成推流地址
     * @param pushDomain 推流域名
     * @param pushKey 推流域名配置的鉴权Key
     * @param appName 推流AppName
     * @param streamName 推流StreamName
     * @param expireTime 过期时间(单位是秒)
     */
    public static String generate_push_url(String pushDomain,String pushKey,String appName,String streamName,long expireTime) {
        String pushUrl = "";
        //推流域名未开启鉴权功能的情况下
        if(pushKey=="") {
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName;
        }else {
            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
            String stringToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pushKey;
            String authKey = md5(stringToMd5);
            pushUrl = "rtmp://"+pushDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+authKey;
        }
        //System.out.println("推流地址是: "+pushUrl);
        return pushUrl;
    }
    /**
     * 生成播放地址
     * @param pullDomain 播放域名
     * @param pullKey 播放鉴权Key
     * @param appName 播放appName(同推流appName)
     * @param streamName 播放streamName (同推流streamName)
     * @param expireTime 过期时间(单位是秒
     */
    public static HashMap general_pull_url(String pullDomain, String pullKey, String appName, String streamName, long expireTime) {
        String rtmpUrl = ""; //rtmp的拉流地址
        String hlsUrl = "";  //m3u8的拉流地址
        String flvUrl = "";  //flv的拉流地址
        //播放域名未配置鉴权Key的情况下
        if(pullKey == "") {
            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName;
            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8";
            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv";
        }else {
            long timeStamp = System.currentTimeMillis()/1000L + expireTime;
            String rtmpToMd5 = "/"+appName+"/"+streamName+"-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String rtmpAuthKey = md5(rtmpToMd5);
            rtmpUrl = "rtmp://"+pullDomain+"/"+appName+"/"+streamName+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+rtmpAuthKey;

            String hlsToMd5 = "/"+appName+"/"+streamName+".m3u8-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String hlsAuthKey = md5(hlsToMd5);
            hlsUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".m3u8"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+hlsAuthKey;

            String flvToMd5 = "/"+appName+"/"+streamName+".flv-"+Long.toString(timeStamp)+"-0-0-"+pullKey;
            String flvAuthKey = md5(flvToMd5);
            flvUrl = "http://"+pullDomain+"/"+appName+"/"+streamName+".flv"+"?auth_key="+Long.toString(timeStamp)+"-0-0-"+flvAuthKey;
        }
        //System.out.println("RTMP播放地址为: "+rtmpUrl);
        //System.out.println("m3u8播放地址为: "+hlsUrl);
        //System.out.println("flv播放地址为: "+flvUrl);
        HashMap<String,String> map = new HashMap<>();
        map.put("RTMP",rtmpUrl);
        map.put("m3u8",hlsUrl);
        map.put("flv",flvUrl);
        return map;

    }
    public static HashMap GetPushAndPullUrl(String AppName, String StreamName, Long ExpireTime) {
        //直播最长时长
        long expireTime = 3600L;
        //播流域名
        String pullDomain = "live.governmentbuy.com";
        //播流鉴权
        String pullKey = "2020GovBuyZhiBo0413";
        //推流域名
        String pushDomain = "zhibo.governmentbuy.com";
        //推流鉴权
        String pushKey = "GovBuy2020ZhiBo";
        //调用方法生成播流地址
        HashMap hashMap = general_pull_url(pullDomain, pullKey, AppName, StreamName, expireTime);
        //调用方法生成推流地址
        String s = generate_push_url(pushDomain, pushKey, AppName, StreamName, expireTime);
        hashMap.put("tuiliu",s);
        return hashMap;
    }

    public static void main(String[] args) {
        //使用方法,所有流地址都在hashMap中 自己通过键值(flv、boliu、RTMP、m3u8) 获得值就行
        HashMap<String,String> hashMap = LiveUtils.GetPushAndPullUrl("a", "b", 3600l);
        for (String o : hashMap.keySet()) {
            System.out.println(o+":"+hashMap.get(o));
        }
    }
}

还是那句话,你只看到了大佬轻松解决问题的光鲜,却不记得当初人家死磕Bug的痛苦。

万物皆入轮回,谁也躲不掉!

以上文章,均是我实际体验,写出来的笔记资料,不会出现全文盗用别人文章!烦请各位,支持下原创,请勿直接盗用,有条件的可以开通个会员支持下,谢谢!!!

Java Code Study » SpringBoot 整合 阿里直播推流 OBS

招聘、单纯问问题,都可以联系我

加我QQ 公司缺人,拉我入伙