相关shell脚本记录

1,ts2big.sh

#!/bin/sh
indir=$1
outdir=$2
brfilter=$3
vod_id=$4
outname=$5
response_url=$6

filter=""
error=""
output=""
filternum=""
FFMPEG="/usr/local/ffmpeg/bin/ffmpeg"
logdir=/home/otvcloud/hls/
start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}${start_time}_${vod_id}.log

if [ ! -e $logdir ] ;then
	mkdir -p $logdir
elif [ ! -d $logdir ];then
	rm -f $logdir
	mkdir -p $logdir
fi

if [ ! -e $outdir ] ;then
	mkdir -p $outdir
	if [ ! -e $outdir ] ;then
	   chmod 777 $outdir
	fi
	echo "mk newdir:$outdir" >>$log
	
fi

echo "indir:$indir" >>$log
echo "outdir:$outdir" >>$log
echo "brfilter:$brfilter" >>$log
echo "vod_id:$vod_id" >>$log
echo "outname:$outname" >>$log
echo "response_url:$response_url" >>$log


function response_cms()
{
  for ((j = 0; j < 3; j++))	
  do
  	response_time=`date -d today +%Y%m%d%H%M%S`
	curl --retry 3 --retry-delay 5 -d "{\"res\":\"$1\", \"des\":\"$2\",\"vod_id\":\"$3\",\"url\":\"$output\"}"    "http://$response_url"

	if [ $? -eq 0 ] ; then 
		echo "$response_time response cms [success]" >>$log
		exit 0
	else 
		echo "$response_time response cms [retry] [$j]" >>$log
		continue
	fi
  done
  
  if [ $j -eq 3 ] && [ $? -ne 0 ]; then
	echo "$response_time response cms [faild] [quit]" >>$log
	exit 1;
  fi	
}


#如果句柄数过大,需要设绿
function ts2mp4_sethandle()
{
    local nts=$1
    echo "sethandle, tsnum:$nts" >>$log
    #--如果句柄数过大,需要设绿
             if [ `whoami` = "root" ];then  
   		            if [ $nts -gt 65536 ]; then
   		                echo "too many files, error"
   		                exit 0
   		            fi 
             else  
     		          if [ $nts -gt 4096 ]; then
   		                echo "need to many handle, chang to root usr"
   		                exit 0
   		            fi 
             fi 
   
         local maxhnum=`ulimit -n`
         echo maxhnum$maxhnum
      	 if [ $nts -gt $maxhnum ]; then
             if [ `whoami` = "root" ];then  
                       echo "set tmp 65536"
   		               ulimit -n  65536
             else  
                       echo "set tmp 4096"
     		           ulimit -n  4096
             fi 
		 fi
   #--如果句柄数过大,需要设绿   
}


#转换成MP4文件
function tstobig_ffmpegcmd()
{
    local file=$1
    local br=$2
    local tmpfile
    local out
    out="$outdir/$outname-$br.ts"
    echo out-$out  >>$log
    #执行ffmpeg命令
    if [ ! -e $out ] ;then
        $FFMPEG -f concat  -safe 0 -i $file -c copy $out >>$log 2>>$log
    fi
    

    if [ ! -e $out ] ;then
            #faild ret csm
            error="taskfaild!"
            response_cms 0 $error $vod_id       
    fi
   
    rm $file -f
}


#处理2级m3u8
function tstobig_dealsm3u8()
{
    local sm3u8=$1
    local br=$2
    local joint
    local dir=${sm3u8%/*}"/"
    local file=$outdir/$br"_concat_$start_time.txt"
    
    echo "in 2m3u8 $sm3u8 $file" >>$log 
    contain_ts=`cat $sm3u8 | grep ".ts" |wc -l`
    if [ $contain_ts = "0" ] ;then
            error="2m3u8%20empty"
            echo $error >>$log
            response_cms 0 $error $vod_id
            exit 1
    fi
    ts2mp4_sethandle $contain_ts
    local tsname=`cat $sm3u8 |grep ".ts"|head -n 1`
    tsname=$dir$tsname
    local tsset=`cat $sm3u8 |grep ".ts"`
    for line in $tsset
    do
        tmp=`echo $line | tr -d "\r" | tr -d "\n"`
        echo "file '$dir$tmp'" >> $file
        joint="$joint|$dir$tmp"
    done
    concat="concat:"${joint#*|}
    tstobig_ffmpegcmd $file $br
} 


#处理1级m3u8
function tstobig_dealm3u8()
{
   local m3u8=$1
   local name
    echo "in 1m3u8 $m3u8 " >>$log 
   #contain_m3u8为码率数
   contain_m3u8=`cat $m3u8 | grep "#EXT-X-STREAM-INF" |wc -l`  
    if [ $contain_m3u8 = "0" ] ;then
       #filternum=${#filter[@]}
       echo "1m3u8 empty fnum:$filternum"  >>$log
	   for((j=0;j<$filternum;j++)){
          sfilter="/"${fiter[$j]}
	      out=`echo $m3u8|grep $sfilter|grep -v grep`
          if [ "a$out" != "a" ];then
               output=$outdir/$outname-${fiter[$j]}".ts"
               tstobig_dealsm3u8 $m3u8  ${fiter[$j]}
               echo "deal one bitrate"
              break;
          fi
       }  
    else   
          #-----以下处理多个2级m3u8,sm3u8set为二级m3u8集合    
          local sm3u8set=(`cat $m3u8 |grep "m3u8"`) 
 		  for((i=0;i<$contain_m3u8;i++)){
    	      name=${sm3u8set[$i]%\?*} 
    	      sm3u8=${m3u8%/*}/$name
    		  sm3u8=`echo $sm3u8|tr -d '\r\n'`
    	      #filternum=${#filter[@]}
    	      echo "reay deal $sm3u8 fnum:$filternum" >>$log
    	      for((j=0;j<$filternum;j++)){
    	      sfilter="/"${fiter[$j]}
    	      echo "try f :$sfilter" >>$log
    	      out=`echo $sm3u8|grep $sfilter|grep -v grep`
    	          if [ "a$out" != "a" ];then
	                    #like 700-640x480/stream.m3u8 700/stream.m3u8
	                    if [ "a$output" != "a" ];then
		                   output=$output","
		                fi
    	               output=$output$outdir/$outname-${fiter[$j]}".ts"
                       tstobig_dealsm3u8 $sm3u8 ${fiter[$j]}
                       echo "deal one bitrate"
    	               break;
    	          fi
    	      }  
		  }
         #-----以上处理多个2级m3u8 
    fi
    
        #faild ret csm
        error="tasksuc!"
        echo $output >>$log
        response_cms 1 $error $vod_id       
}

 
  
#get brfilter
filternum=`echo $brfilter|grep -o ","|wc -l`
echo "filternum:$filternum"  >>$log
 for((i=0;i<$filternum;i++)){
     let num=i+1
     fiter[$i]=`echo $brfilter|cut -d ',' -f $num`
     echo "single filter:${fiter[$i]}" >>$log
}
  
tstobig_dealm3u8 $1

2,  tsonly-tg-20844.sh

#!/bin/sh  
#2017/6/2   1.0.0.2
#此脚本用来只下ts,m3u8放到指定的目录
#usage: ./tsonly.sh   http://192.168.1.214:8090/otv/test/channel01/100.m3u8  /home/test  100
#param1:hls url
#param2:download dir
#param3:task id

url=$1
dir=$2
id=$3
dlthread="8"
m3u8dir="$dir/m3u8"

dlmaxtime="30"
continue_dltime="60"


if [ ! -e $m3u8dir ] ;then
    mkdir -p $m3u8dir
elif [ ! -d $m3u8dir ];then
    rm -f $m3u8dir
    mkdir -p $m3u8dir
fi

sutctime="0" 
eutctime="99999999999" 
logdir=/home/otvcloud/hls/
start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}"onlyts"_${id}_${start_time}.log

if [ ! -e $logdir ] ;then
    mkdir -p $logdir
elif [ ! -d $logdir ];then
    rm -f $logdir
    mkdir -p $logdir
fi

echo "***********************************************"  >>$log
echo " " >>$log
echo "url:$url" >>$log
echo "dir:$dir" >>$log
echo "id:$id" >>$log
echo "***********************************************"  >>$log

#线程池
function dl_thread()
{
local taskfile=$1    
local tasks="0"    
local live
local tsurl
local tsdldir
local tstime
echo "enter dl_thread, $taskfile" >>$log
    while [ 1 -eq 1 ]
    do
        if [ -e $taskfile ] ;then
            tasks=`cat $file|wc -l`
            if [ "$tasks" == "0" ]; then
                 sleep 1
            else
              live=`cat $file`
              for((j=0;j<$tasks;j++)){
                   newline=`echo ${live[$j]}|tr -d '\r\n'`    
                   tsurl=`echo $newline|cut -d '+' -f 1|tr -d '\r\n'`
                   tsdldir=`echo $newline|cut -d '+' -f 2|tr -d '\r\n'`
                   tstime=`echo $newline|cut -d '+' -f 3|tr -d '\r\n'`
                   echo "tsurl:$tsurl  $tsdldir  $tstime.ts"
                   dl_file $tsurl 1 $tsdldir $tstime.ts
              }    
               sed -i '1 d' $taskfile
            fi
        else   
           echo "no taskfile" >>$log
           break;
        fi
        
    done    
}

#1 dl suc ,0 dl 404
function dl_file()
{
    local file=$1
    local mode=$2
    local dldir=$3
    local filename=$4
    local span  
    
    if [ "$filename" == "0" ]; then
         filename=`echo ${file%\?*}`
         filename=${filename##*/} 
    fi
   
         
    dst=$dldir/$filename
    echo "ing dl $dst"
    s_utc=`date +%s`
    while [ 1 -eq 1 ]
    do
       s_time=`date -d today +%Y%m%d%H%M%S`
       ret=`curl -s -o $dst -w %{http_code} -m 12  $file`
       cur_time=`date -d today +%Y%m%d%H%M%S`
       if [ "$ret" == "200" ]; then
          echo "st:$s_time et:$cur_time $filename 200"
          echo "st:$s_time et:$cur_time $filename 200">>$log
          return 1
       else
          c_utc=`date +%s`
          let span=($c_utc - $s_utc) 
          
          if [ "$ret" == "404" ]; then
              echo "st:$s_time et:$cur_time $filename 404">>$log
              if [ -e $dst ] ;then
                rm $dst -f
              fi
              return 0    
          else
              echo "st:$s_time et:$cur_time, $ret,down $filename faild" >>$log
              if [ $mode -eq 0 ] ; then
                    return 0
              else
                    if [[ $span -gt $dlmaxtime ]];then
                        echo "down $filename faild > $dlmaxtime"  >>$log
                        #echo -e "Date:`date -d today +%Y%m%d%H%M%S`\n  " | mailx -s "down $filename faild > $dlmaxtime $ret" qiuyoujie@126.com
                        return 1
                    else
                        sleep 1
                        continue 
                        
                    fi    
              fi 
          fi
       fi
    done
}


function update_time()
{
  local srcm3u8=$1

   while [ 1 -eq 1 ]
      do
         sleep 4
         dl_file $srcm3u8 1 $m3u8dir 0
      done
}


#处理2级m3u8
function down_ts()
{
    local srcm3u8=$1
    local dstm3u8=$2
    local prefix=${srcm3u8%/*}"/" 
    local preTs
    local tail
    local arg
    local span
    local br
    local utime
    local starnum
    local num
    local tsname
    local out
    local sublen
    local sm3u8="$m3u8dir/700.m3u8"
    local fileduration
    local fileseq
    local st       #临时变量
    local nowutc   #当前片的utc时间
    local day      #日期
    local i
    local j
    #for  anti-stealing-link
    out=`echo $srcm3u8 |grep "?"`
    if [ "a$out" != "a" ];then
      arg=${srcm3u8##*\?}
    fi
    
    contain_ts=`cat $dstm3u8 | grep ".ts" |wc -l`
    if [ $contain_ts = "0" ] ;then
            echo "no ts in $dstm3u8"  >>$log
            return 0
    fi
    
    fileduration=`cat $dstm3u8 | grep "#EXT-X-TARGETDURATION:"| cut -d ':' -f 2 `;
    fileseq=`cat $dstm3u8 | grep "#EXT-X-MEDIA-SEQUENCE:"| cut -d ':' -f 2 `;
    
    br="700"   
    utime=`date +%s` 
    
    #第一片的时间
    tsname=`cat $dstm3u8 |grep ".ts"|head -n 1` 
    preTs=${tsname%-*}
    ts_t=${tsname%.*}
    ts_t=${ts_t#*-}
    starnum=$ts_t
    sublen=`echo $starnum|wc -L`
    tail=${tail:sublen} 
    echo "sub:$starnum,sublen:$sublen"
    
    #最后一片的时间
    tsname=`cat $dstm3u8 |grep ".ts"|tail -n 1`
    ts_t=${tsname%.*}
    ts_t=${ts_t#*-}
    echo "$ts_t"
    latestnum=$ts_t
    echo "start num:$starnum, latestnum:$latestnum, sublen:$sublen" >>$log
    echo "first $dstm3u8, s:$utime"  >>$log
    
    
    #写一个临时二级M3U8
     >$sm3u8
    echo  "#EXTM3U" >>$sm3u8
    echo  "#EXT-X-VERSION:3" >>$sm3u8
    st="#EXT-X-TARGETDURATION:$fileduration"
    echo  "$st" >>$sm3u8
    st="#EXT-X-MEDIA-SEQUENCE:$fileseq"
    echo  "$st" >>$sm3u8
    st="#EXTINF:$fileduration.000,"
    day=`date -d today +%Y%m%d`
    tsdldir=$dir/$br/$day
    for((i=0;i<$contain_ts;i++)){
       nowutc=$(( $i * $fileduration + $utime ));
       tstime=`date -d @$nowutc +%Y%m%dT%H%M%S`
       outname="$tsdldir/$tstime.ts"
       echo  "$st" >>$sm3u8
       echo  "$br/$day/$tstime.ts" >>$sm3u8
    }

    #线程池
    for((j=0;j<$dlthread;j++)){
    local file=$m3u8dir"/thread$j"
        touch $file
        > $file
        dl_thread $file  &
    }   

    #写status    
    recfile=$dir/$br/"status"
    if [ -e $recfile ] ;then
       ts_t=`cat $recfile |grep ".ts"`
       
    fi
    
    update_time $srcm3u8  &
    #last_dlreplaytime=$utime
    #2017-01-01 00:00:00 < utime < 2118-01-01 00:00:00
    if [ $starnum -ge $sutctime ]; then
          for((i=$starnum;i<$eutctime;))
        do        
            #ts must exist
            while [ 1 -eq 1 ]
            do
               #echo "curr num:$i, latest num:$latestnum" >>$log
               if [ $i -le $latestnum ]; then
                   echo "$i $latestnum" >>$log
                   break;
               else
               	   #for idling
                   sleep 0.5
                   if [ -e $dstm3u8 ] ;then
                         contain_ts=`cat $dstm3u8 | grep ".ts" |wc -l`
                         if [ $contain_ts != "0" ] ;then
                              #tsname like:smmSD-1673138.ts
                              local tsname=`cat $dstm3u8 |grep ".ts"|tail -n 1`
                              if [ "a$tsname" != "a" ];then
                                  local ts_t1=${tsname%.*}
                                  ts_t1=${ts_t1#*-}
                                  latestnum=$ts_t1
                              fi
                         fi
                   fi
               fi
            done
            
            #下载ts直到成功
            local tsurl="${prefix}$preTs-$i.ts"
            echo "tsurl:$tsurl"
            cur_time=`date -d today +%Y%m%d%H%M%S`
            
            nowutc=$(( ($i -$starnum) * $fileduration + $utime ));
            day=`date -d @$nowutc +%Y%m%d`
            tsdldir=$dir/$br/$day
            if [ ! -e $tsdldir ] ;then
                mkdir -p $tsdldir
            fi
            tstime=`date -d @$nowutc +%Y%m%dT%H%M%S`
            echo "pre dl $br $cur_time ,$i.ts <-> $tstime.ts ">>$log
            #dl_file $tsurl 1 $tsdldir $tstime.ts
            for((j=0;j<$dlthread;j++)){
               local file=$m3u8dir"/thread$j"
                if [ -e $file ] ;then
                     tasks=`cat $file|wc -l`
                    if [ "$tasks" == "0" ]; then
                        echo "$tsurl+$tsdldir+$tstime"  >>$file 
                        echo "insert to $file"
                        let i=$i+1
                        break;
                    fi
                fi
            }
        done
    else
        echo "in $dstm3u8, utime:$utime error"  >>$log
    fi 
} 


#处理2级m3u8
function parse_sm3u8()
{
   local sm3u8=$1
   local filename
   echo "ready parse :$sm3u8" >>$log  
  
   #下载url内容到本地
   dl_file $sm3u8 1 $m3u8dir  0
   if [ $? -eq 1 ] ; then 
       filename=${sm3u8%\?*}
       filename=`echo ${filename##*/}`  
       dtm3u8=$m3u8dir/$filename
       if [ -e $dtm3u8 ] ;then
             down_ts  $sm3u8 $dtm3u8
       else
             echo "no $dtm3u8" >>$log 
       fi
   fi
}
 

#处理2级m3u8
function parse_url()
{
   local dtm3u8
   local filename
   #下载url内容到本地
   dl_file $url 1 $m3u8dir 0
   if [ $? -eq 1 ] ; then 
      #if dl suc 
       filename=${url%\?*}
       filename=`echo ${filename##*/}` 
       dtm3u8=$m3u8dir/$filename
       if [ -e $dtm3u8 ] ;then
           contain_m3u8=`cat $dtm3u8 | grep "#EXT-X-STREAM-INF" |wc -l`
           if [ $contain_m3u8 = "0" ] ;then
              echo "no m3u8 in $dtm3u8" >>$log 
              down_ts  $url $dtm3u8
           else
              echo "$contain_m3u8 m3u8 in $dtm3u8" >>$log 
              local sm3u8set=(`cat $dtm3u8 |grep "m3u8"`) 
              for((i=0;i<$contain_m3u8;i++)){
                      sm3u8=${url%/*}/${sm3u8set[$i]}
                      sm3u8=`echo $sm3u8|tr -d '\r\n'`
                      echo "reay dl $sm3u8" >>$log
                      parse_sm3u8 $sm3u8 &
                     
                  }
           fi
       else
         echo "no $dtm3u8" >>$log 
       fi
   else
     echo "dl $1 faild"  >>$log 
   fi
    
    
   while [ 1 -eq 1 ]
    do
    sleep 10
   done 
  
   echo "parse_url end"
} 


parse_url
 

3, stream_trans.sh

#!/bin/sh
taskid=$1
url=$2
format=$3
chid=$4
rootDir=$5
response_url=$6
serAddr=$7
ykbDir=$8

bitRate="700"

FFMPEG="/usr/local/ffmpeg/bin/ffmpeg"
logdir=/home/otvcloud/kuaibian/
start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}${start_time}_${taskid}_"trans".log
logDays="3"

if [ ! -e $logdir ] ;then
    mkdir -p $logdir
    chmod 777 $logdir
elif [ ! -d $logdir ];then
    rm -f $logdir
    mkdir -p $logdir
    chmod 777 $logdir
fi

echo "taskid:$taskid" >>$log
echo "url:$url" >>$log
echo "format:$format" >>$log
echo "chid:$chid" >>$log
echo "root dir:$rootDir" >>$log
echo "response url:$response_url" >>$log
echo "server addr:$serAddr" >>$log
echo "ykbDir:$ykbDir" >>$log

echo "PID of this script: $$">>$log
echo "PPID of this script: $PPID">>$log


#kill ffmpeg
function killffmpeg()
{
    local hlssrc=$1
    local psres=`ps -ef |grep $hlssrc|grep -v grep`
    local pid=`echo $psres|awk '{print $2}'`
    kill -9 $pid
    if [ $? -eq 0 ] ; then 
        echo "kill ffmpeg$pid suc"    >>$log
    else
        echo "kill ffmpeg$pid error"  >>$log
    fi
}

function response_csm()
{
    for ((j = 0; j < 3; j++))
    do
        response_time=`date -d today +%Y%m%d%H%M%S`
        curl --retry 1 --retry-delay 5 -d "{\"result\":\"$1\", \"msg\":\"$2\",\"task_id\":\"$3\",\"url\":\"$4\"}"    "$response_url"

        if [ $? -eq 0 ] ; then 
            echo "$response_time response csm [success]" >>$log
            break
        else 
            echo "$response_time response csm [retry] [$j]" >>$log
            continue
        fi
    done
    if [ $j -eq 3 ] ; then
        echo "$response_time response csm, taskid:$3 [failed] [quit]" >>$log
        exit 1;
    fi
}

outdir="/data/channellist/channel$chid"
if [ ! -e $outdir ] ;then
    mkdir -p $outdir
    if [ -e $outdir ] ;then
       chmod 777 $outdir
       chown -R otvcloud:otvcloud $outdir
    fi
    echo "c $outdir">>$log
fi

killF=$outdir/"status.txt"
echo "0" > $killF

out=$outdir/"$bitRate.m3u8"
if [ -e $out ] ;then
    rm -r $out
fi

lastm3u8time=`date -r $out +%s`
echo $out  >>$log

fm3u8=$outdir/"index.m3u8"
echo "fm3u8:$fm3u8" >>$log
echo "#EXTM3U" > $fm3u8
echo  "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=716800" >>$fm3u8 
echo  "$bitRate.m3u8" >>$fm3u8

if [ ! -e $fm3u8 ] ;then
        #failed ret csm
        msg="taskfailed!,no,m3u8"
        response_csm 0 $msg $taskid ""
        echo "$fm3u8 not exist, taskid:$taskid" >> $log
        exit 1
fi

function check_trans()
{
    #not 404 case, report to csm according to ts
    local elapse=0
    local msg
    local m3u8Url
    local contain_ts
    local tsmaxtime=30
    
    
    while [ $elapse -le $tsmaxtime ]
    do
        if [ ! -e $out ] ;then
            let elapse++
            sleep 1
            continue
        fi
        contain_ts=`cat $out | grep ".ts" |wc -l`
        if [ $contain_ts != "0" ] ;then
            msg="ts_produced"
            m3u8Url=$serAddr/$ykbDir/channel$chid/"index.m3u8"
            echo "url of m3u8:$m3u8Url" >> $log
            response_csm 1 $msg $taskid $m3u8Url
            echo "ts produced, notify csm" >> $log
            break;
        fi
    done
    if [ $elapse -gt $tsmaxtime ] ;then
        msg="ts_can_not_produced"
        response_csm 0 $msg $taskid ""
        echo "ts not produced in 10s, notify csm" >> $log
        exit 0
    fi
}

function delolddir()
{
    #700
    local newpath=$1/$bitRate
    local curtime
    local filetime
    local newdir
    local stime
    
    if [ -e $newpath ]; then
        curtime=`date +%s`
        for dirs in `ls $newpath`
        do   
            newdir="$newpath/$dirs"
            filetime=`date -d $dirs +%s`
            let day_stamp=($curtime - $filetime)
            let day=($day_stamp/86400)    
            if [[ $day -ge $logDays ]];then
                rm  $newdir -rf
                stime=`date -d today +%Y%m%d%H%M%S`
                echo "[$stime] rmdir $newdir"   >>$log
            fi   
        done
    fi  
}

function transEngin()
{
    local errortimes=0
    local pid
    local ppid
    
    while [ 1 -eq 1 ]
    do
        sleep 2
        
        stime=`date -d today +%Y%m%d%H%M%S`
        #grep outdir
        od=`ps -ef |grep $outdir|grep -v grep`
        echo "$stime   od: $od" >>$log
        if [ "a$od" == "a" ];then
            echo "[$stime] start ffmpeg"    >>$log
            if [ "$bitRate" == "400" ] ;then
                $FFMPEG -loglevel verbose -i $url -vcodec h264 -s 480x270 -g 25 -r 25 -keyint_min 25 -sc_threshold 0 -bsf:v h264_mp4toannexb  -acodec libfaac -ac 2 -ar 48000  -f hls -hls_time 2 -hls_list_size 5 -hls_init_sequence 1 -hls_init_replay  1 $out  >>$log 2>>$log &
            elif [ "$bitRate" == "700" ] ;then               
				$FFMPEG -loglevel verbose -i $url -vcodec h264  -g 25 -r 25 -keyint_min 25 -sc_threshold 0 -bsf:v h264_mp4toannexb  -acodec libfaac -ac 2 -ar 48000  -f hls -hls_time 2 -hls_list_size 5 -hls_init_sequence 1 -hls_init_replay  1 $out  >>$log 2>>$log &
			fi

        else
            delolddir $outdir
            local kFlag=`cat $killF`
            kFlag=`echo $kFlag|tr -d '\r\n'`
            if [ "1" == $kFlag ]; then
                #删除所有转码文件日期目彿
                if [ -e $outdir ] ;then
                   rm -rf $outdir
                fi
                pid=`echo $od|awk '{print $2}'`
                ppid=`echo $od|awk '{print $3}'`
                echo "to kill pid:$pid, ppid:$ppid" >>$log
                #kill ppid and pid
                kill -9 $pid
                if [ $? -eq 0 ] ; then
                    echo "kill ffmpeg pid:$pid suc"    >>$log
                else
                    echo "kill ffmpeg pid:$pid error"  >>$log
                fi
                kill -9 $ppid
                exit 0
            fi
            
            outnum=`echo $od|wc -l`
            echo "[$stime] ff[$pid] $outnum a"   >>$log  
            
            #function1            check m3u8
            if [ -e $out ]; then
                #check audio channels
                curm3u8time=`date -r $out +%s`
                if [[ "$curm3u8time" != "$lastm3u8time" ]];then
                    errortimes=0
                    lastm3u8time=$curm3u8time 
                    newts=`cat $out |grep "ts"|tail -n 1`
                    if [ -n "$newts" ]; then
                        #get latest ts
                        tspath="$outdir/$newts"
                        chs=`ffprobe -i $tspath -show_streams 2>> /dev/null  |grep channels`
                        chs=${chs##*=}
                        echo "ts path: $tspath, chs: $chs"
                        if [[ $chs == "0" ]];then
                            echo "[$stime] $curm3u8time $lastm3u8time $newts chs 0"    >>$log   
                            #kill ffmpeg
                            killffmpeg $outdir 
                            echo "[$stime] chs 0, kill channel[$num] ffmpeg$pid"   >>$log  
                            continue
                        fi
                    fi
                else
                    let errortimes++
                    if [ $errortimes == 15 ];then
                        #after 5*6s m3u8 not update, kill ffmpeg
                        killffmpeg $outdir
                        errortimes=0
                        echo "[$stime] m3u8 not update more than 2x15s, kill channel[$num] ffmpeg$pid"   >>$log 
                        continue
                    fi
                fi
            fi
        fi
        
    done
}


function str_trans()
{
    #ݬөԴ
    if [ "artmp" != "a$format" ]; then
        local ret=`curl -s -o /dev/null -w %{http_code} -m 20 $url`
        local msg
        if [ "200" != $ret ]; then
            #clean file dir
            if [ -e $outdir ] ;then
               rm -rf $outdir
            fi
            #report to csm asym
            msg="no_content_from_url"
            response_csm 0 $msg $taskid ""
            echo "source not found, notify csm" >> $log
            
            return
        fi
    fi

    check_trans  &

    transEngin

    while [ 1 -eq 1 ]
    do
        sleep 2
    done 
}

str_trans

echo "in stream_trans $taskid exit" >>$log

4, stream_delete.sh

#!/bin/sh
taskid=$1
url=$2
chid=$3


logdir=/home/otvcloud/kuaibian/
start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}${start_time}_${taskid}_"del".log
killF=/data/channellist/channel$chid/"status.txt"

if [ ! -e $logdir ] ;then
	mkdir -p $logdir
elif [ ! -d $logdir ];then
	rm -f $logdir
	mkdir -p $logdir
fi

echo "taskid:$taskid" >>$log
echo "url:$url" >>$log
echo "chid:$chid" >>$log



#kill ffmpeg
function killffmpeg()
{
     local hlssrc=$1
     local log=$2
     out=`ps -ef |grep $hlssrc|grep -v grep`
     pid=`echo $out|awk '{print $2}'`
     ppid=`echo $out|awk '{print $3}'`
     echo "to kill stream_trans $ppid and ffmpeg $pid"    >>$log
     echo "1" > $killF
}


flag="/channel$chid/"
echo "flag :$flag" >>$log  

killffmpeg $flag $log


echo "in stream_delete $taskid exit" >>$log

5, compactmp4.sh

src=$1
program_id=$2
root=$3
time=$4
srcnum=`echo $src|grep -o ".mp4"|wc -l`

logdir=/home/otvcloud/compactmp4/
FFMPEG="/usr/local/ffmpeg/bin/ffmpeg"

start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}${start_time}_${program_id}.log
if [ ! -e $logdir ] ;then
	mkdir -p $logdir
elif [ ! -d $logdir ];then
	rm -f $logdir
	mkdir -p $logdir
fi

path=$root$program_id
if [ ! -e $path ] ;then
	mkdir -p $path
fi


if [ ! -e $root ] ;then
	mkdir -p $root
elif [ ! -d $root ];then
	rm -f $root
	mkdir -p $root
fi

echo "***********************************************">>$log

echo "src:$src" >>$log
echo "program_id:$program_id" >>$log
echo "root:$root" >>$log
echo "time:$time" >>$log


srcnum=`echo $src|grep -o ".mp4"|wc -l`
if [ $srcnum == 0 ]; then
   echo "srcnum:$srcnum" >>$log
   exit 0 ;
fi

function compact_mp4(){

 local num=$1
 local src=$2
 local files
    for((i=1;i<=$num;i++)){
         s=`echo $src | cut -d ',' -f $i`
         echo $s
         dst=$path/$i.mp4
         echo $dst
         ts_time=`date -d today +%Y%m%d%H%M%S`
         #down mp4
         if [ -e $dst ];then
	     rm -f $dst
	     fi
	     #download mp4
         curl -L -C -  -s --retry 3 --retry-delay 5 -o $dst $s
         if [ $? -eq 0 ] ; then
             echo "$ts_time $s [success]" >>$log
             if [ -e $dst ];then 
               newts=$path/$i.ts 
	           echo  $dst $newts >>$log
               $FFMPEG -y -i $dst -c copy -bsf:v h264_mp4toannexb $newts >>$log 2>>$log
              # rm -f $dst
		       files="$files|$newts"	
	        fi
	     else 
	       echo "$ts_time $s [faild] $?" >>$log
	     fi
    } 
    
    if [ "a$files" != "a" ];then
       concat="concat:"${files#*|}
       echo $concat >>$log
       local out=$path"/$time.mp4"
       echo out-$out
       #执行ffmpeg命令
       $FFMPEG -y -i $concat -c copy -bsf:a aac_adtstoasc $out >>$log  2>>$log
       rm $path/*.ts -f
    fi
}

compact_mp4 $srcnum $src

6,autoKill.sh

#!/bin/sh

logdir=/home/otvcloud/segmenterKill/
start_time=`date -d today +%Y%m%d%H%M%S`
log=${logdir}${start_time}_"segmenter".log

if [ ! -e $logdir ] ;then
    mkdir -p $logdir
elif [ ! -d $logdir ];then
    rm -f $logdir
    mkdir -p $logdir
fi

#Section configuration(配置部分)
#Task Time ,example:203000(Time 20:30:00);190000(Time 19:00:00);
startTime=030000
#the programs you want to execute(要执行的程序)
program=kill

#Section promgram (程序执行部分)
perDate=$(date "+%Y%m%d")
isNewDay=1

echo 'Task schedule Time: ('$startTime') 3:00 of every week 1 and week 5, Waiting...'
echo 'Task schedule Time: ('$startTime') 3:00 of every week 1 and week 5, Waiting...' >>$log

while true ; do
    curTime=$(date "+%H%M%S")
    curDate=$(date "+%Y%m%d")
    #echo $curTime

    #Check week day(周2周6执行)
    week=`date +%w`
    if [ $week -ne 1 -a $week -ne 5 ];then
        isNewDay=0
        sleep 1
        continue
    else
        #check and run script(周2周6执行)
        if [ "$isNewDay" -eq "1" ];then
            if [ "$curTime" -gt "$startTime" ];then
                ((i=$((10#$curTime))-$((10#$startTime))))
                if [ $i -lt 10 ];then
                    echo 'The program ('$program') Running...' >>$log
                    #$program
                    kill -9 `pgrep segmenter`
                    echo 'The program ('$program') Stopped with return ('$?')...' >>$log
                    isNewDay=0
                fi
            else
                echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...'
            fi

        else
            #new day start(开始新的一天)
            if [ "$curDate" -gt "$perDate" ];then
                #echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...'
                isNewDay=1
                perDate=$curDate
            fi
        fi
        sleep 1
    fi
done


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值