sipp ffmpeg - 2 - 使用ffmpeg打流

guide: https://ffmpeg.org/ffmpeg-protocols.html

play_audio.sh

=======
#!/bin/bash

action="${1}"
sipnum="${2}"
lo_m_host="${3}"
lo_m_port="${4}"
play_file="${5}"
ro_sdpmsg="${6}"

pid_file="/var/run/sipp/$sipnum.pid"
log_file="/home/sipp/play.log"

do_start () {
    # 打流文件
    [[ "$play_file" == "" ]] && exit

    play_path="/home/sipp/$play_file"
    
    [[ ! -f "$play_path"  ]] && exit

    # 音视频端口
    lo_a_port=$lo_m_port

    [[ "$ro_sdpmsg" != "" ]] && {
        ro_m_host=$(echo "$ro_sdpmsg" | sed -n '/c=IN/p'    | sed -n '1p' | sed 's/\(.*\) \(.*\)/\2/g' | xargs)
        ro_a_port=$(echo "$ro_sdpmsg" | sed -n '/m=audio/p' | sed -n '1p' | awk '{ print $2 }')
    }
    
    [[ -z "$lo_m_host" || -z "$lo_a_port" || -z "$ro_m_host" || -z "$ro_a_port" ]] && {
        echo "$(date +"%F %T") error [$sipnum] L:[$lo_m_host:a:$lo_a_port] <=> R:[$ro_m_host:a:$ro_a_port]" >> $log_file
        exit
    }

    [[ ! -d /var/run/sipp ]] && mkdir -p /var/run/sipp

    nohup ffmpeg -loglevel quiet -re -stream_loop -1 -i "$play_path" -vn -filter_complex aresample=8000,asetnsamples=n=160 -c:a pcm_alaw -f rtp -seq 0 "rtp://${ro_m_host}:${ro_a_port}?localrtpport=${lo_a_port}&localaddr=${lo_m_host}" >/dev/null 2>&1 &

    xpid="$!"
    errn="$?"

    echo "$(date +"%F %T") start [$sipnum] pid[$xpid] \$?[$errn] L:[$lo_m_host:a:$lo_a_port] <=> R:[$ro_m_host:a:$ro_a_port]" >> $log_file
    # set +x

    echo "$xpid" > $pid_file
}

do_stop () {
    xpid=$(cat $pid_file)
    echo "$(date +"%F %T") stop [$$sipnum] pid[$xpid]" >> $log_file

    [[ "$xpid" != "" ]] && kill -9 $xpid
    rm -rf $pid_file
}

[[ -z "$sipnum" ]] && exit

case "$action" in
    "start")
        [[ -f $pid_file ]] || do_start
        ;;
    "stop")
        [[ -f $pid_file ]] && do_stop
        ;;
esac

play_video.sh

=======
#!/bin/bash

sipnum="${2}"
lo_m_host="${3}"
lo_m_port="${4}"
lo_m_ssrc="${5}"
play_file="${6}"
play_type="${7}"
ro_sdpmsg="${8}"

pid_file="/var/run/sipp/$sipnum.pid"
log_file="/home/sipp/play.log"

# 指定成员播放流
play_cfg="/home/sipp/media_play_user.txt"

do_start () {
    # 打流文件
    [[ "$play_file" == "" ]] && exit

    play_path="/home/sipp/$play_file"
    
    [[ ! -f "$play_path"  ]] && exit

    # 大小流 main mini
    [[ "$play_type" == "mini" ]] && lo_m_ssrc=$((lo_m_ssrc+1))

    # 音视频端口
    lo_a_port=$lo_m_port
    lo_v_port=$((lo_m_port+2))

    [[ "$ro_sdpmsg" != "" ]] && {
        ro_m_host=`echo "$ro_sdpmsg" | sed -n '/c=IN/p'    | sed -n '1p' | sed 's/\(.*\) \(.*\)/\2/g' | xargs`
        ro_a_port=`echo "$ro_sdpmsg" | sed -n '/m=audio/p' | sed -n '1p' | awk '{ print $2 }'`
        ro_v_port=`echo "$ro_sdpmsg" | sed -n '/m=video/p' | sed -n '1p' | awk '{ print $2 }'`
    }
    
    [[ -z "$lo_m_host" || -z "$lo_a_port" || -z "$lo_v_port" || -z "$ro_m_host" || -z "$ro_a_port" || -z "$ro_v_port" ]] && {
        echo "$(date +"%F %T") error [$sipnum] L:[$lo_m_host:a:$lo_a_port:v:$lo_v_port] <=> R:[$ro_m_host:a:$ro_a_port:v:$ro_v_port]" >> $log_file
        exit
    }

    [[ ! -d /var/run/sipp ]] && mkdir -p /var/run/sipp

    # ffmpegcmd="ffmpeg -loglevel quiet -re -stream_loop -1 -i $play_path -vn -filter_complex aresample=8000,asetnsamples=n=160 -c:a pcm_alaw -f rtp -seq 0 'rtp://${ro_m_host}:${ro_a_port}?localrtpport=${lo_a_port}&localaddr=${lo_m_host}' -an -c:v copy -f rtp -seq 0 -ssrc $lo_m_ssrc -rtpflags skip_rtcp -payload_type 108 'rtp://${ro_m_host}:${ro_v_port}?localrtpport=${lo_v_port}&localrtcpport=$lo_v_rtcp&localaddr=${lo_m_host}'"
    # echo "$ffmpegcmd" >> $log_file
    # $ffmpegcmd
    
    # set -x
    # eval ffmpeg -loglevel quiet -re -stream_loop -1 -i $play_path -vn -filter_complex aresample=8000,asetnsamples=n=160 -c:a pcm_alaw -f rtp -seq 0 'rtp://${ro_m_host}:${ro_a_port}?localrtpport=${lo_a_port}&localaddr=${lo_m_host}' \
    #                                                              -an -c:v copy -f rtp -seq 0 -ssrc $lo_m_ssrc -rtpflags skip_rtcp -payload_type 108 'rtp://${ro_m_host}:${ro_v_port}?localrtpport=${lo_v_port}&localrtcpport=$lo_v_rtcp&localaddr=${lo_m_host}' -loglevel quiet
    nohup ffmpeg -loglevel quiet -re -stream_loop -1 -i $play_path -an -c:v copy -f rtp -seq 0 -ssrc $lo_m_ssrc -rtpflags skip_rtcp -payload_type 108 "rtp://${ro_m_host}:${ro_v_port}?localaddr=${lo_m_host}&localrtpport=${lo_v_port}" >/dev/null 2>&1 &

    xpid="$!"
    errn="$?"

    echo "$(date +"%F %T") start [$sipnum] pid[$xpid] \$?[$errn] L:[$lo_m_host:a:$lo_a_port:v:$lo_v_port] <=> R:[$ro_m_host:a:$ro_a_port:v:$ro_v_port]" >> $log_file
    # set +x

    echo "$xpid" > $pid_file
}

do_stop () {
    xpid=$(cat $pid_file)
    echo "$(date +"%F %T") stop [$$sipnum] pid[$xpid]" >> $log_file

    [[ "$xpid" != "" ]] && kill -9 $xpid
    rm -rf $pid_file
}

[[ -z "$sipnum" ]] && exit

[[ -f $play_cfg ]] && {
    [[ "$(head -c 1 $play_cfg)" != "" ]] && {
        hit=$(grep -o -w $sipnum $play_cfg)
        [[ -z "$hit" ]] && exit
    }
}

case "$1" in
    "start")
        [[ -f $pid_file ]] || do_start
        ;;
    "stop")
        [[ -f $pid_file ]] && do_stop
        ;;
esac

具体命令示例

输出结果:

ffmpeg -loglevel quiet -re -stream_loop -1 -i /home/sipp/yb_gop150_fps30_PCMA_720P.mkv -an -c:v copy -f rtp -seq 0 -ssrc 1000000000 -rtpflags skip_rtcp -payload_type 108 'rtp://192.168.78.39:47440?localaddr=192.168.78.81&localrtpport=20002'

posted @ 2024-05-12 18:35  LiYanbin  阅读(6)  评论(0编辑  收藏  举报