Browse Source

Add files via upload

MoeClub 5 years ago
parent
commit
667b1dad21
1 changed files with 94 additions and 68 deletions
  1. 94 68
      ffmpeg/media.sh

+ 94 - 68
ffmpeg/media.sh

@@ -5,19 +5,27 @@ ForceH264="${2:-0}"
 Uploader="upload.sh"
 M3u8mod="m3u8.sh"
 Publish="publish.sh"
-MaxSize=20
-MaxCheck=10
-MaxTime=15
 BitRadio="1.55"
 ForceBitRadio="1.75"
 ForceMaxRadio="1.20"
 ForceRate="2400000"
+MaxSize=10
+MaxCheck=10
+MaxTime=10
+AutoClear=0
+QuickMode=1
+IsQuick=0
+
 
 # Main
 if [ -n "${Media}" ] && [ -f "${Media}" ]; then
   echo "media file: '${Media}'."
+  echo "$ForceH264" |grep -q "^-"
+  [ "$?" -eq 0 ] && QuickMode=0
+  ForceH264=`echo "$ForceH264" |grep -o "[0-9]\{1,\}"`
+  [ -n "$ForceH264" ] || ForceH264=0
 else
-  echo "Not found '${Media}'."
+  [ -n "${Media}" ] && echo "Not found '${Media}'." || echo "Please input a media file."
   exit 1
 fi
 
@@ -25,83 +33,94 @@ MediaName=`basename "${Media}" |cut -d'.' -f1 |sed 's/[[:space:]]/_/g'`
 ScriptDir=`dirname $0`
 CurrentDir=`pwd`
 OutPutM3u8="${CurrentDir}/${MediaName}.m3u8"
+OutPutM3u8Bak="${CurrentDir}/${MediaName}.m3u8.bak"
 OutPutLog="${CurrentDir}/${MediaName}.log"
 MediaFolder="${CurrentDir}/${MediaName}.output"
 
 # cache
-rm -rf "${OutPutLog}"
-rm -rf "${MediaFolder}"
-mkdir -p "${MediaFolder}"
+[ "$QuickMode" == 1 ] && [ -f "${OutPutM3u8Bak}" ] && [ -d "${MediaFolder}" ] && IsQuick=1
+if [ "$IsQuick" != 1 ]; then
+  rm -rf "${OutPutLog}"
+  rm -rf "${MediaFolder}"
+  mkdir -p "${MediaFolder}"
+fi
 
 ## m3u8
-BitRate=`ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "${Media}"`
-echo "media bitrate: ${BitRate}"
-if [ "$ForceH264" -eq 0 ]; then
-  _ForceH264=`awk 'BEGIN{print '${BitRate}' / ('${ForceBitRadio}' * '${ForceRate}')}' |cut -d'.' -f1`
-  [ "$_ForceH264" -ne 0 ] && ForceH264=1
-fi
-if [ "$ForceH264" -ge 2 ]; then
-  MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
-  if [ "$MediaCode" != "h264" ]; then
-    echo "Error: This media file code '${MediaCode}', must 'h264' in this mode."
-    exit 1
+if [ "$IsQuick" != 1 ]; then
+  BitRate=`ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "${Media}"`
+  echo "media bitrate: ${BitRate}"
+  if [ "$ForceH264" -eq 0 ]; then
+    _ForceH264=`awk 'BEGIN{print '${BitRate}' / ('${ForceBitRadio}' * '${ForceRate}')}' |cut -d'.' -f1`
+    [ "$_ForceH264" -ne 0 ] && ForceH264=1
   fi
-  VideoCode="copy"
-  VideoAddon="-bsf:v h264_mp4toannexb"
-  [ "$ForceH264" -le 3 ] && VideoTime="3" || VideoTime="$ForceH264"
-elif [ "$ForceH264" -eq 1 ]; then
-  ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
-  ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
-  VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
-  VideoCode="h264"
-  if [ "$BitRate" -gt "3500000" ]; then
-    BitRadio="${ForceBitRadio}"
-  fi
-  BitRate=3000000
-  echo "media bitrate(new): ${BitRate}"
-else
-  MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
-  if [ "$MediaCode" == "h264" ]; then
+  if [ "$ForceH264" -ge 2 ]; then
+    MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
+    if [ "$MediaCode" != "h264" ]; then
+      echo "Error: This media file code '${MediaCode}', must 'h264' in this mode."
+      exit 1
+    fi
     VideoCode="copy"
-  else
-    VideoCode="h264"
-  fi
-  if [ "$VideoCode" == "copy" ]; then
     VideoAddon="-bsf:v h264_mp4toannexb"
-  else
+    [ "$ForceH264" -le 2 ] && VideoTime="2" || VideoTime="$ForceH264"
+  elif [ "$ForceH264" -eq 1 ]; then
+    ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
+    ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
+    VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
+    VideoCode="h264"
     if [ "$BitRate" -gt "3500000" ]; then
       BitRadio="${ForceBitRadio}"
-      BitRate=3000000
-    else
-      ForceRate="${BitRate}"
-      BitRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceBitRadio}'}' |cut -d'.' -f1`      
     fi
+    BitRate=3000000
     echo "media bitrate(new): ${BitRate}"
-    ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
-    ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
-    VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
-  fi
-fi
-if [ "$ForceH264" -le 1 ]; then
-  VideoTime=`awk 'BEGIN{print ('${MaxSize}' * 1024 * 1024 * 8) / ('${BitRate}' * '${BitRadio}') }' |cut -d'.' -f1`
-  if [ -n "$VideoTime" ]; then
-    if [ "${BitRate}" -gt 3500000 ]; then
-      MaxTime=5
-    elif [ "${BitRate}" -gt 3000000 ]; then
-      MaxTime=10
+  else
+    MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
+    if [ "$MediaCode" == "h264" ]; then
+      VideoCode="copy"
+    else
+      VideoCode="h264"
     fi
-    if [ "$VideoTime" -gt "$MaxTime" ]; then
-      VideoTime="$MaxTime"
+    if [ "$VideoCode" == "copy" ]; then
+      VideoAddon="-bsf:v h264_mp4toannexb"
+    else
+      if [ "$BitRate" -gt "3500000" ]; then
+        BitRadio="${ForceBitRadio}"
+        BitRate=3000000
+      else
+        ForceRate="${BitRate}"
+        BitRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceBitRadio}'}' |cut -d'.' -f1`      
+      fi
+      echo "media bitrate(new): ${BitRate}"
+      ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
+      ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
+      VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
     fi
-  else
+  fi
+  if [ "$ForceH264" -le 1 ]; then
+    VideoTime=`awk 'BEGIN{print ('${MaxSize}' * 1024 * 1024 * 8) / ('${BitRate}' * '${BitRadio}') }' |cut -d'.' -f1`
+    if [ -n "$VideoTime" ]; then
+      if [ "${BitRate}" -gt 3500000 ]; then
+        MaxTime=5
+      elif [ "${BitRate}" -gt 3000000 ]; then
+        MaxTime=7
+      fi
+      if [ "$VideoTime" -gt "$MaxTime" ]; then
+        VideoTime="$MaxTime"
+      fi
+    else
+      exit 1
+    fi
+  fi
+  echo "media segment time: ${VideoTime}"
+  ffmpeg -v info -i "${Media}" -vcodec ${VideoCode} -acodec aac -strict experimental ${VideoAddon} -map 0:v:0 -map 0:a? -f segment -segment_list "${OutPutM3u8}" -segment_time "${VideoTime}" "${MediaFolder}/output_%04d.ts"
+  if [ $? -ne 0 ]; then
     exit 1
+  else
+    cp -rf "${OutPutM3u8}" "${OutPutM3u8Bak}"
   fi
+else
+  cp -rf "${OutPutM3u8Bak}" "${OutPutM3u8}"
 fi
-echo "media segment time: ${VideoTime}"
-ffmpeg -v info -i "${Media}" -vcodec ${VideoCode} -acodec aac -strict experimental ${VideoAddon} -map 0:v:0 -map 0:a? -f segment -segment_list "${OutPutM3u8}" -segment_time ${VideoTime} "${MediaFolder}/output_%04d.ts"
-if [ $? -ne 0 ]; then
-  exit 1
-fi
+exit 1
 
 ## upload
 echo "start upload..."
@@ -128,12 +147,12 @@ for((i=0; i<$MaxCheck; i++)); do
       exit 1
     fi
     bash "${ScriptDir}/${Uploader}" "${BadItem}" |tee -a "${OutPutLog}"
-    sed -i '/;\ NULL_/d' "${OutPutLog}"
-    ## mod m3u8
-    if [ -f "${ScriptDir}/${M3u8mod}" ]; then
-      bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
-    fi
   done
+  sed -i '/;\ NULL_/d' "${OutPutLog}"
+  ## mod m3u8
+  if [ -f "${ScriptDir}/${M3u8mod}" ]; then
+    bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
+  fi
 done
 
 # publish
@@ -141,3 +160,10 @@ if [ -f "${ScriptDir}/${Publish}" ]; then
   echo "publish ..."
   bash "${ScriptDir}/${Publish}" "${OutPutM3u8}"
 fi
+
+# clear
+if [ "$AutoClear" != 0 ]; then
+  rm -rf "${OutPutLog}"
+  rm -rf "${MediaFolder}"
+  rm -rf "${OutPutM3u8Bak}"
+fi