JAVA生成阿里云直播推流和拉流

import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.lang.StringUtils;


import java.util.HashMap;
import java.util.Map;

public class AliLiveUtils {

    //推流域名
    private final static String pushDomainName = "";
    //拉流域名
    private final static String pullDomainName = "";
    //推流鉴权KEY
    private final static String pushKey = "";
    //拉流鉴权KEY
    private final static String pullKey = "";

    /**
     * 生成推拉流地址
     * @param appName APP名称
     * @param timestamp 失效时间
     * @param streamName 流名称
     * @param templateId 模版id
     * @return
     */
    public static Map<String, String> createUrl(String appName ,  String streamName, Long timestamp , String templateId) {
        HashMap<String, String> map = new HashMap<>();

//        if (!StringUtils.isEmpty(templateId))
//            streamName = streamName + "_" + templateId;

        Long newTimestamp = (System.currentTimeMillis() / 1000L) + timestamp;

        // 推流地址
        String rtmpURL = "rtmp://" + pushDomainName + "/" + appName + "/" + streamName ;
        String rtsURL = "artc://" + pushDomainName + "/" + appName + "/" + streamName ;

        // 拉流地址
        //如果转码id存在
        String pullRtmpURL = null;
        String udp = null;
        String flvUrl = null;
        String M3U8Url = null;
        if (!StringUtils.isEmpty(templateId)){

            pullRtmpURL = "rtmp://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId ;
            udp = "artc://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId;
            flvUrl = "http://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId + ".flv";
            M3U8Url = "http://" + pullDomainName + "/" + appName + "/" + streamName + '_' + templateId + ".m3u8";

            map.put("udp", createAuthKey(udp, appName, streamName +  '_' + templateId, newTimestamp, pullKey));
            map.put("pull", createAuthKey(pullRtmpURL, appName, streamName +  '_' + templateId, newTimestamp, pullKey));
            map.put("flv", createAuthKey(flvUrl, appName, streamName + + '_' + templateId + ".flv", newTimestamp, pullKey));
            map.put("m3u8", createAuthKey(M3U8Url, appName, streamName + + '_' + templateId + ".m3u8", newTimestamp, pullKey));
        }else{

            pullRtmpURL = "rtmp://" + pullDomainName + "/" + appName + "/" + streamName  ;
            udp = "artc://" + pullDomainName + "/" + appName + "/" + streamName ;
            flvUrl = "http://" + pullDomainName + "/" + appName + "/" + streamName  + ".flv";
            M3U8Url = "http://" + pullDomainName + "/" + appName + "/" + streamName  + ".m3u8";

            map.put("udp", createAuthKey(udp, appName, streamName , newTimestamp, pullKey));
            map.put("pull", createAuthKey(pullRtmpURL, appName, streamName, newTimestamp, pullKey));
            map.put("flv", createAuthKey(flvUrl, appName, streamName + ".flv", newTimestamp, pullKey));
            map.put("m3u8", createAuthKey(M3U8Url, appName, streamName + ".m3u8", newTimestamp, pullKey));
        }

        // 生成推流拉流地址
        map.put("push", createAuthKey(rtmpURL, appName, streamName, newTimestamp, pushKey));
        map.put("rts", createAuthKey(rtsURL, appName, streamName , newTimestamp, pushKey));
        
        
        return map;
    }

    /**
     * 生成鉴权地址
     * @param url
     * @param timestamp
     * @param key
     * @return
     */
    public static String createAuthKey(String url, String appName, String streamName, Long timestamp, String key) {
        String hashValue = "/" + appName + "/" + streamName + "-" + timestamp + "-0-0-" + key;
        String md5Value = DigestUtil.md5Hex(hashValue);
        String auth_key = timestamp + "-0-0-" + md5Value;
        String newUrl = url + "?auth_key=" + auth_key;
        return newUrl;
    }


}

大佬的模板地址生成稍微有些问题,稍作了修改

转载:阿里视频直播自定义推拉流地址生成_给我一包烟丶的博客-CSDN博客_生成拉流地址


版权声明:本文为gyoryu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。