栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 = "";

    
    public static Map createUrl(String appName ,  String streamName, Long timestamp , String templateId) {
        HashMap 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;
    }

    
    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博客_生成拉流地址

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000068.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号