media.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/bash
  2. Media="$1"
  3. ForceH264="${2:-0}"
  4. Uploader="upload.sh"
  5. M3u8mod="m3u8.sh"
  6. Publish="publish.sh"
  7. BitRadio="1.55"
  8. ForceBitRadio="1.75"
  9. ForceMaxRadio="1.20"
  10. ForceRate="2400000"
  11. MaxSize=5
  12. MaxCheck=10
  13. MaxTime=10
  14. AutoClear=0
  15. QuickMode=1
  16. # Main
  17. if [ -n "${Media}" ] && [ -f "${Media}" ]; then
  18. echo "$ForceH264" |grep -q "^-"
  19. [ "$?" -eq 0 ] && QuickMode=0
  20. ForceH264=`echo "$ForceH264" |grep -o "[0-9]\{1,\}"`
  21. [ -n "$ForceH264" ] || ForceH264=0
  22. echo "media file: '${Media}'."
  23. else
  24. [ -n "${Media}" ] && echo "Not found '${Media}'." || echo "Please input a media file."
  25. exit 1
  26. fi
  27. MediaName=`basename "${Media}" |cut -d'.' -f1 |sed 's/[[:space:]]/_/g'`
  28. ScriptDir=`dirname $0`
  29. CurrentDir=`pwd`
  30. OutPutM3u8="${CurrentDir}/${MediaName}.m3u8"
  31. OutPutM3u8Bak="${CurrentDir}/${MediaName}.m3u8.bak"
  32. OutPutLog="${CurrentDir}/${MediaName}.log"
  33. MediaFolder="${CurrentDir}/${MediaName}.output"
  34. # cache
  35. [ "$QuickMode" == 1 ] && [ -f "${OutPutM3u8Bak}" ] && [ -d "${MediaFolder}" ] || QuickMode=0
  36. if [ "$QuickMode" != 1 ]; then
  37. rm -rf "${OutPutLog}"
  38. rm -rf "${MediaFolder}"
  39. mkdir -p "${MediaFolder}"
  40. fi
  41. ## m3u8
  42. if [ "$QuickMode" != 1 ]; then
  43. BitRate=`ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "${Media}"`
  44. echo "media bitrate: ${BitRate}"
  45. if [ "$ForceH264" -eq 0 ]; then
  46. _ForceH264=`awk 'BEGIN{print '${BitRate}' / ('${ForceBitRadio}' * '${ForceRate}')}' |cut -d'.' -f1`
  47. [ "$_ForceH264" -ne 0 ] && ForceH264=1
  48. fi
  49. if [ "$ForceH264" -ge 2 ]; then
  50. MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
  51. if [ "$MediaCode" != "h264" ]; then
  52. echo "Error: This media file code '${MediaCode}', must 'h264' in this mode."
  53. exit 1
  54. fi
  55. VideoCode="copy"
  56. VideoAddon="-bsf:v h264_mp4toannexb"
  57. [ "$ForceH264" -le 2 ] && VideoTime="2" || VideoTime="$ForceH264"
  58. elif [ "$ForceH264" -eq 1 ]; then
  59. ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
  60. ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
  61. VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
  62. VideoCode="h264"
  63. if [ "$BitRate" -gt "3500000" ]; then
  64. BitRadio="${ForceBitRadio}"
  65. fi
  66. BitRate=3000000
  67. echo "media bitrate(new): ${BitRate}"
  68. else
  69. MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
  70. if [ "$MediaCode" == "h264" ]; then
  71. VideoCode="copy"
  72. else
  73. VideoCode="h264"
  74. fi
  75. if [ "$VideoCode" == "copy" ]; then
  76. VideoAddon="-bsf:v h264_mp4toannexb"
  77. else
  78. if [ "$BitRate" -gt "3500000" ]; then
  79. BitRadio="${ForceBitRadio}"
  80. BitRate=3000000
  81. else
  82. ForceRate="${BitRate}"
  83. BitRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceBitRadio}'}' |cut -d'.' -f1`
  84. fi
  85. echo "media bitrate(new): ${BitRate}"
  86. ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
  87. ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
  88. VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
  89. fi
  90. fi
  91. if [ "$ForceH264" -le 1 ]; then
  92. VideoTime=`awk 'BEGIN{print ('${MaxSize}' * 1024 * 1024 * 8) / ('${BitRate}' * '${BitRadio}') }' |cut -d'.' -f1`
  93. if [ -n "$VideoTime" ]; then
  94. if [ "${BitRate}" -gt 3500000 ]; then
  95. MaxTime=5
  96. elif [ "${BitRate}" -gt 3000000 ]; then
  97. MaxTime=7
  98. fi
  99. if [ "$VideoTime" -gt "$MaxTime" ]; then
  100. VideoTime="$MaxTime"
  101. fi
  102. else
  103. exit 1
  104. fi
  105. fi
  106. echo "media segment time: ${VideoTime}"
  107. 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"
  108. if [ $? -ne 0 ]; then
  109. exit 1
  110. else
  111. cp -rf "${OutPutM3u8}" "${OutPutM3u8Bak}"
  112. fi
  113. else
  114. cp -rf "${OutPutM3u8Bak}" "${OutPutM3u8}"
  115. fi
  116. ## upload
  117. echo "start upload..."
  118. if [ -e "${ScriptDir}/${Uploader}" ]; then
  119. bash "${ScriptDir}/${Uploader}" "${MediaFolder}" |tee -a "${OutPutLog}"
  120. ## mod m3u8
  121. if [ -f "${ScriptDir}/${M3u8mod}" ]; then
  122. bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
  123. fi
  124. fi
  125. # check
  126. echo "check upload..."
  127. function ForceVBR(){
  128. fsName="$1"
  129. [ -f "${fsName}" ] || return
  130. [ `du -s -k "${fsName}" |cut -f1` -le `awk 'BEGIN{print '${MaxSize}' * 1024}' |cut -d'.' -f1` ] && return
  131. NewFsName="${fsName}_New.ts"
  132. cp -rf "${fsName}" "${NewFsName}"
  133. ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
  134. ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
  135. VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
  136. ffmpeg -y -v info -i "${NewFsName}" -vcodec h264 -acodec copy -strict experimental -bsf:v h264_mp4toannexb ${VideoAddon} -f mpegts "${fsName}"
  137. [ -f "${NewFsName}" ] && [ -f "${fsName}" ] && rm "${NewFsName}"
  138. }
  139. for((i=0; i<$MaxCheck; i++)); do
  140. BadCheck=`grep -v "^#\|^https\?://" "${OutPutM3u8}"`
  141. [ -n "$BadCheck" ] || break
  142. for Item in `echo "$BadCheck"`; do
  143. if [ -f "${Item}" ]; then
  144. BadItem="${Item}"
  145. elif [ -f "${MediaFolder}/${Item}" ]; then
  146. BadItem="${MediaFolder}/${Item}"
  147. else
  148. echo "Error: not found '${Item}'."
  149. exit 1
  150. fi
  151. ForceVBR "${BadItem}"
  152. bash "${ScriptDir}/${Uploader}" "${BadItem}" |tee -a "${OutPutLog}"
  153. done
  154. sed -i '/;\ NULL_/d' "${OutPutLog}"
  155. ## mod m3u8
  156. if [ -f "${ScriptDir}/${M3u8mod}" ]; then
  157. bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
  158. fi
  159. done
  160. # publish
  161. if [ -f "${ScriptDir}/${Publish}" ]; then
  162. echo "publish ..."
  163. bash "${ScriptDir}/${Publish}" "${OutPutM3u8}"
  164. fi
  165. # clear
  166. if [ "$AutoClear" != 0 ]; then
  167. rm -rf "${OutPutLog}"
  168. rm -rf "${MediaFolder}"
  169. rm -rf "${OutPutM3u8Bak}"
  170. fi