4dage-ffmpeg 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/bash
  2. version="1.0.4"
  3. videoFile=''
  4. rectspec=''
  5. outputName=''
  6. outputFolder=''
  7. allowCacheStay=0
  8. tempFolder=/tmp/4dage-ffmpeg
  9. time=$(date "+%Y%m%d-%H%M%S")
  10. tempFilePrefix="4dage-source-${time}"
  11. declare -a outputVideoList=('mp4 flv')
  12. red=$(tput setaf 1)
  13. green=$(tput setaf 2)
  14. reset=$(tput sgr0)
  15. command_exists() {
  16. if ! [[ -x $(command -v "$1") ]]; then
  17. return 1
  18. fi
  19. return 0
  20. }
  21. print_usage() {
  22. echo -e "\033[43;34m $package - 4dage ffmpeg v2 custom version for ffmpeg: \033[0m"
  23. echo "${green}If have problem with this, please contact with gemer AKA: 张宇鹏<zhangyupeng@cgaii.com>"
  24. echo "$package [options] application [arguments]"
  25. echo " "
  26. echo "${red}options:"
  27. echo "-h, --help show brief help"
  28. echo "-s, --source=source file path input the video source path"
  29. echo "-r, --rect=rectange size rectange size"
  30. echo "-n, --name=output file name output file name"
  31. echo "-o, --output=DIR specify a directory to store output in"
  32. echo "-v, --version output verison"
  33. echo "--cahche maintain the cahche file for tmp folder"
  34. echo " "
  35. echo -e "\033[0;33m"
  36. cat <<EOF
  37. _oo0oo_
  38. 088888880
  39. 88" . "88
  40. (| -_- |)
  41. 0\ = /0
  42. ___/'---'\___
  43. .' \\\\| |// '.
  44. / \\\\||| : |||// \\
  45. /_ ||||| -:- |||||- \\
  46. | | \\\\\\ - /// | |
  47. | \_| ''\---/'' |_/ |
  48. \ .-\__ '-' __/-. /
  49. ___'. .' /--.--\ '. .'___
  50. ."" '< '.___\_<|>_/___.' >' "".
  51. | | : '- \'.;'\ _ /';.'/ - ' : | |
  52. \ \ '_. \_ __\ /__ _/ .-' / /
  53. ====='-.____'.___ \_____/___.-'____.-'=====
  54. '=---='
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. 佛祖保佑 TTT 万无一失
  57. EOF
  58. echo -e "\033[m"
  59. }
  60. make_temp_dir() {
  61. # # Make a temp folder
  62. if [ ! -d $tempFolder ]; then
  63. mkdir -p $tempFolder && chmod 776 $tempFolder
  64. fi
  65. }
  66. make_temp_dir
  67. while test $# -gt 0; do
  68. case "$1" in
  69. -h | --help)
  70. print_usage
  71. exit 0
  72. ;;
  73. -s)
  74. shift
  75. if test $# -gt 0; then
  76. export videoFile=$1
  77. else
  78. echo "{$red}no video path specified"
  79. exit 1
  80. fi
  81. shift
  82. ;;
  83. --source*)
  84. export videoFile=$(echo $1 | sed -e 's/^[^=]*=//g')
  85. echo $1
  86. shift
  87. ;;
  88. -r)
  89. shift
  90. if test $# -gt 0; then
  91. export rectspec=$1
  92. else
  93. echo "{$red}no rectangle format specified"
  94. exit 1
  95. fi
  96. shift
  97. ;;
  98. --rect*)
  99. export rectspec=$(echo $1 | sed -e 's/^[^=]*=//g')
  100. shift
  101. ;;
  102. -o)
  103. shift
  104. if test $# -gt 0; then
  105. export outputFolder=$1
  106. else
  107. echo "{$red}no output dir folder specified"
  108. exit 1
  109. fi
  110. shift
  111. ;;
  112. --output*)
  113. export outputFolder=$(echo $1 | sed -e 's/^[^=]*=//g')
  114. shift
  115. ;;
  116. -n)
  117. shift
  118. if test $# -gt 0; then
  119. export outputName=$1
  120. else
  121. echo "no output video name specified"
  122. exit 1
  123. fi
  124. shift
  125. ;;
  126. --name*)
  127. export outputName=$(echo $1 | sed -e 's/^[^=]*=//g')
  128. shift
  129. ;;
  130. --cache)
  131. shift
  132. echo "allow cahche remain"
  133. export allowCacheStay=1
  134. shift
  135. ;;
  136. -v)
  137. echo "$version"
  138. shift
  139. ;;
  140. *)
  141. print_usage
  142. # echo "111"
  143. break
  144. ;;
  145. esac
  146. done
  147. mkVerison() {
  148. touch "$outputFolder/4dage-ffmpeg-v2-$version.version"
  149. }
  150. takeVideoCap() {
  151. FFREPORT=file="$outputFolder/$outputName.cap.txt":level=48 ffmpeg -hide_banner -loglevel error -y -i "$outputFolder/$outputName.mp4" -r 1 -ss 00:00:01 -vframes 1 -vf "transpose=1,scale=iw/5:ih/5,setsar=1:1" -f image2 "$outputFolder/$outputName.jpg"
  152. }
  153. covertVideoTask() {
  154. videoFileType=${videoFile##*.}
  155. FFREPORT=file="$outputFolder/$outputName.$videoFileType.txt":level=48 ffmpeg -hide_banner -y -i $videoFile \
  156. -filter_complex "[0:v]crop=$rectspec,scale=iw/2:ih/2,pad=ceil(iw/2)*2:ceil(ih/2)*2,split=2[out1][out2]" \
  157. -preset superfast -threads 2 -b 1.8M -minrate 1.8M -maxrate 1.8M -bufsize 2M \
  158. -map '[out1]' -map 0:a? -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.mp4" \
  159. -map '[out2]' -map 0:a? -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.flv"
  160. takeVideoCap
  161. mkVerison
  162. }
  163. clearDist() {
  164. rm -f $outputFolder/*.json
  165. rm -f $outputFolder/*.version
  166. }
  167. concatAllLogFile() {
  168. cat $outputFolder/*.txt >>"$outputFolder/$outputName.full.log"
  169. rm $outputFolder/*.txt
  170. }
  171. if [ ! -z $videoFile ] && [ ! -z $rectspec ] && [ ! -z $outputName ] && [ ! -z $outputFolder ]; then
  172. if [ ! -d $outputFolder ]; then
  173. mkdir -p $outputFolder
  174. else
  175. clearDist
  176. fi
  177. covertVideoTask
  178. concatAllLogFile
  179. if command_exists "mediainfo"; then
  180. mediainfo --fullscan "$outputFolder/$outputName.mp4" --Output=JSON >"$outputFolder/$outputName.json"
  181. mediainfo --fullscan "$outputFolder/$outputName.flv" --Output=JSON >"$outputFolder/$outputName.flv.json"
  182. fi
  183. echo -e "\033[0;32m"
  184. cat <<EOF
  185. _oo0oo_
  186. 088888880
  187. 88" . "88
  188. (| -_- |)
  189. 0\ = /0
  190. ___/'---'\___
  191. .' \\\\| |// '.
  192. / \\\\||| : |||// \\
  193. /_ ||||| -:- |||||- \\
  194. | | \\\\\\ - /// | |
  195. | \_| ''\---/'' |_/ |
  196. \ .-\__ '-' __/-. /
  197. ___'. .' /--.--\ '. .'___
  198. ."" '< '.___\_<|>_/___.' >' "".
  199. | | : '- \'.;'\ _ /';.'/ - ' : | |
  200. \ \ '_. \_ __\ /__ _/ .-' / /
  201. ====='-.____'.___ \_____/___.-'____.-'=====
  202. '=---='
  203. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  204. 佛祖保佑 TTT 顺利转换
  205. EOF
  206. echo -e "\033[m"
  207. echo "covert done!"
  208. else
  209. if [ ! -n "$1" ]; then
  210. print_usage
  211. else
  212. if [ -z $videoFile ]; then
  213. echo "${red}miss video file path: 缺视频文件路径"
  214. fi
  215. if [ -z $rectspec ]; then
  216. echo "${red}miss rect spec : 缺少rect的技术参数"
  217. fi
  218. if [ -z $outputName ]; then
  219. echo "${red}miss file name : 缺少输出文件名称"
  220. fi
  221. if [ -z $outputFolder ]; then
  222. echo "${red}miss output path : 缺少输出路径"
  223. fi
  224. fi
  225. fi