media.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. Media="$1"
  3. ForceH264="${2:-0}"
  4. Uploader="upload_yuque.sh"
  5. M3u8mod="m3u8.sh"
  6. Publish="publish.sh"
  7. MaxSize=20
  8. MaxCheck=10
  9. MaxTime=15
  10. BitRadio="1.55"
  11. ForceBitRadio="1.75"
  12. ForceMaxRadio="1.20"
  13. ForceRate="2400000"
  14. # Main
  15. if [ -n "${Media}" ] && [ -f "${Media}" ]; then
  16. echo "media file: '${Media}'."
  17. else
  18. echo "Not found '${Media}'."
  19. exit 1
  20. fi
  21. MediaName=`basename "${Media}" |cut -d'.' -f1 |sed 's/[[:space:]]/_/g'`
  22. ScriptDir=`dirname $0`
  23. CurrentDir=`pwd`
  24. OutPutM3u8="${CurrentDir}/${MediaName}.m3u8"
  25. OutPutLog="${CurrentDir}/${MediaName}.log"
  26. MediaFolder="${CurrentDir}/${MediaName}.output"
  27. # cache
  28. rm -rf "${OutPutLog}"
  29. rm -rf "${MediaFolder}"
  30. mkdir -p "${MediaFolder}"
  31. ## m3u8
  32. BitRate=`ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "${Media}"`
  33. echo "media bitrate: ${BitRate}"
  34. if [ "$ForceH264" -eq 0 ]; then
  35. ForceH264=`awk 'BEGIN{print '${BitRate}' / ('${ForceBitRadio}' * '${ForceRate}')}' |cut -d'.' -f1`
  36. fi
  37. if [ "$ForceH264" -ne 0 ]; then
  38. ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
  39. ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
  40. VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
  41. VideoCode="h264"
  42. if [ "$BitRate" -gt "3500000" ]; then
  43. BitRadio="${ForceBitRadio}"
  44. fi
  45. BitRate=3000000
  46. echo "media bitrate(new): ${BitRate}"
  47. else
  48. MediaCode=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${Media}" |sort |uniq`
  49. if [ "$MediaCode" == "h264" ]; then
  50. VideoCode="copy"
  51. else
  52. VideoCode="h264"
  53. fi
  54. if [ "$VideoCode" == "copy" ]; then
  55. VideoAddon="-bsf:v h264_mp4toannexb"
  56. else
  57. if [ "$BitRate" -gt "3500000" ]; then
  58. BitRadio="${ForceBitRadio}"
  59. BitRate=3000000
  60. else
  61. ForceRate="${BitRate}"
  62. BitRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceBitRadio}'}' |cut -d'.' -f1`
  63. fi
  64. echo "media bitrate(new): ${BitRate}"
  65. ForceMaxRate=`awk 'BEGIN{print '${ForceRate}' * '${ForceMaxRadio}'}' |cut -d'.' -f1`
  66. ForceBuf=`awk 'BEGIN{print '${ForceRate}' / '${ForceMaxRadio}'}' |cut -d'.' -f1`
  67. VideoAddon="-b:v ${ForceRate} -maxrate ${ForceMaxRate} -bufsize ${ForceBuf}"
  68. fi
  69. fi
  70. VideoTime=`awk 'BEGIN{print ('${MaxSize}' * 1024 * 1024 * 8) / ('${BitRate}' * '${BitRadio}') }' |cut -d'.' -f1`
  71. if [ -n "$VideoTime" ]; then
  72. if [ "${BitRate}" -gt 3500000 ]; then
  73. MaxTime=5
  74. elif [ "${BitRate}" -gt 3000000 ]; then
  75. MaxTime=10
  76. fi
  77. if [ "$VideoTime" -gt "$MaxTime" ]; then
  78. VideoTime="$MaxTime"
  79. fi
  80. else
  81. exit 1
  82. fi
  83. echo "media segment time: ${VideoTime}"
  84. 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"
  85. if [ $? -ne 0 ]; then
  86. exit 1
  87. fi
  88. ## upload
  89. echo "start upload..."
  90. if [ -f "${ScriptDir}/${Uploader}" ]; then
  91. bash "${ScriptDir}/${Uploader}" "${MediaFolder}" |tee -a "${OutPutLog}"
  92. ## mod m3u8
  93. if [ -f "${ScriptDir}/${M3u8mod}" ]; then
  94. bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
  95. fi
  96. fi
  97. # check
  98. echo "check upload..."
  99. for((i=0; i<$MaxCheck; i++)); do
  100. BadCheck=`grep -v "^#\|^https\?://" "${OutPutM3u8}"`
  101. [ -n "$BadCheck" ] || break
  102. for Item in `echo "$BadCheck"`; do
  103. bash "${ScriptDir}/${Uploader}" "${Item}" |tee -a "${OutPutLog}"
  104. sed -i '/;\ NULL_/d' "${OutPutLog}"
  105. ## mod m3u8
  106. if [ -f "${ScriptDir}/${M3u8mod}" ]; then
  107. bash "${ScriptDir}/${M3u8mod}" "${OutPutLog}" "${OutPutM3u8}"
  108. fi
  109. done
  110. done
  111. # publish
  112. if [ -f "${ScriptDir}/${Publish}" ]; then
  113. echo "publish ..."
  114. bash "${ScriptDir}/${Publish}" "${OutPutM3u8}"
  115. fi