天天看點

java阿裡雲直播推流拉流工具類

package com.core.common.utils;

import com.alibaba.fastjson.JSONObject;

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

/**
 * @Author peng
 * @Description: 阿裡雲直播推拉流位址工具類
 */
public class AliPlayAndPushUtils {
    /**
     * 推流域名 阿裡雲配置的推流域名
     */
    private static final String pushDomain = "推流域名";
    /**
     * 拉流域名 阿裡雲配置的拉流域名
     */
    private static final String pullDomain = "播流域名";
    /**
     * appName
     */
    private static final String appName = "随便取一個app名字:比如說 penggelive";
    /**
     * 鑒權key: 阿裡雲建立了推流域名和播流域名過後,他給生成的,每個域名一個,推流用推流的key,播流用播流的key,我這裡隻有一個key,因為我自己定義的一個,然後去阿裡雲裡面把他們兩個的權鑒備用key都設定成我設定的了
     */
    private static final String key = "xxxxxxx";

    /**
     * @param time       十位數的時間戳
     * @return 推流的位址
     */
    public static String CreatePushUrl(String streamName, long time) {
        String strpush = "/" + appName + "/" + streamName + "-" + time + "-0-0-" + key;
        String pushUrl = "rtmp://" + pushDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-0-0-" + MD5Utils.getMD5(strpush);
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>推流:   " + pushUrl);
        return pushUrl;
    }
    /**
     * @param time       十位數的時間戳
//     * @param rand       這是用來辨別的 否則同一個時間戳 生成的位址總是相同的  随機數,建議使用UUID(不能包含中劃線“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式)
     * @return 播放流的位址 預設是flv  也可以更改此代碼
     */
    public static String GetPlayUrl(String streamName, long time) {
        String strviewrtmp1 = null;
        String strviewflv1 = null;
        String strviewm3u81 = null;

        String rtmpurl1 = null;
        String flvurl1 = null;
        String m3u8url1 = null;

        strviewrtmp1 = "/" + appName + "/" + streamName + "-" + time + "-0-0-" + key;
        strviewflv1 = "/" + appName + "/" + streamName + ".flv-" + time + "-0-0-" + key;
        strviewm3u81 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-0-0-" + key;
        rtmpurl1 = "rtmp://" + pullDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-0-0-" + MD5Utils.getMD5(strviewrtmp1);
        flvurl1 = "http://" + pullDomain + "/" + appName + "/" + streamName + ".flv?auth_key=" + time + "-0-0-" + MD5Utils.getMD5(strviewflv1);
        m3u8url1 = "http://" + pullDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-0-0-" + MD5Utils.getMD5(strviewm3u81);

        Map<String, String> resultMap = new HashMap<>(5);
        resultMap.put("rtm", rtmpurl1);
        resultMap.put("flv", flvurl1);
        resultMap.put("m3u8", m3u8url1);
//        System.out.println(">>>>>>>>>>>>>>>>>>拉流:  " + rtmpurl1);
//        System.out.println(">>>>>>>>>>>>>>>>>>拉流:    " + flvurl1);
//        System.out.println(">>>>>>>>>>>>>>>>>>拉流:    " + m3u8url1);
        return JSONObject.toJSONString(resultMap);
    }

}
           
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.UUID;

/**
 * @Author wp
 * @Description:
 */
public class MD5Utils {

    public static String getMD5(String str) {
        try {
            // 生成一個MD5加密計算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 計算md5函數
            md.update(str.getBytes());
            // digest()最後确定傳回md5 hash值,傳回值為8為字元串。因為md5 hash值是16位的hex值,實際上就是8位的字元
            // BigInteger函數則将8位的字元串轉換成16位hex值,用字元串來表示;得到字元串形式的hash值
            String md5=new BigInteger(1, md.digest()).toString(16);
            //BigInteger會把0省略掉,需補全至32位
            return fillMD5(md5);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密錯誤:"+e.getMessage(),e);
        }
    }
    private static String fillMD5(String md5){
        return md5.length()==32?md5:fillMD5("0"+md5);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}