4dage-ffmpeg-img2video 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/bash
  2. version="0.0.2"
  3. picsFolder=''
  4. audioFile=''
  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-img2video 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 "-p, --source=pic file folder path input the picture source path"
  29. echo "-a, --audio=audio file path audio file path"
  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 " "
  34. echo -e "\033[0;33m"
  35. cat <<EOF
  36. _oo0oo_
  37. 088888880
  38. 88" . "88
  39. (| -_- |)
  40. 0\ = /0
  41. ___/'---'\___
  42. .' \\\\| |// '.
  43. / \\\\||| : |||// \\
  44. /_ ||||| -:- |||||- \\
  45. | | \\\\\\ - /// | |
  46. | \_| ''\---/'' |_/ |
  47. \ .-\__ '-' __/-. /
  48. ___'. .' /--.--\ '. .'___
  49. ."" '< '.___\_<|>_/___.' >' "".
  50. | | : '- \'.;'\ _ /';.'/ - ' : | |
  51. \ \ '_. \_ __\ /__ _/ .-' / /
  52. ====='-.____'.___ \_____/___.-'____.-'=====
  53. '=---='
  54. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  55. 佛祖保佑 TTT 万无一失
  56. EOF
  57. echo -e "\033[m"
  58. }
  59. make_temp_dir() {
  60. # # Make a temp folder
  61. if [ ! -d $tempFolder ]; then
  62. mkdir -p $tempFolder && chmod 776 $tempFolder
  63. fi
  64. }
  65. make_temp_dir
  66. while test $# -gt 0; do
  67. case "$1" in
  68. -h | --help)
  69. print_usage
  70. exit 0
  71. ;;
  72. -p)
  73. shift
  74. if test $# -gt 0; then
  75. export picsFolder=$1
  76. else
  77. echo "{$red}no video path specified"
  78. exit 1
  79. fi
  80. shift
  81. ;;
  82. --pic*)
  83. export picsFolder=$(echo $1 | sed -e 's/^[^=]*=//g')
  84. echo $1
  85. shift
  86. ;;
  87. -a)
  88. shift
  89. if test $# -gt 0; then
  90. export audioFile=$1
  91. else
  92. echo "{$red}no rectangle format specified"
  93. exit 1
  94. fi
  95. shift
  96. ;;
  97. --audio*)
  98. export audioFile=$(echo $1 | sed -e 's/^[^=]*=//g')
  99. shift
  100. ;;
  101. -o)
  102. shift
  103. if test $# -gt 0; then
  104. export outputFolder=$1
  105. else
  106. echo "{$red}no output dir folder specified"
  107. exit 1
  108. fi
  109. shift
  110. ;;
  111. --output*)
  112. export outputFolder=$(echo $1 | sed -e 's/^[^=]*=//g')
  113. shift
  114. ;;
  115. -n)
  116. shift
  117. if test $# -gt 0; then
  118. export outputName=$1
  119. else
  120. echo "no output video name specified"
  121. exit 1
  122. fi
  123. shift
  124. ;;
  125. --name*)
  126. export outputName=$(echo $1 | sed -e 's/^[^=]*=//g')
  127. shift
  128. ;;
  129. --cache)
  130. shift
  131. echo "allow cahche remain"
  132. export allowCacheStay=1
  133. shift
  134. ;;
  135. -v)
  136. echo "$version"
  137. shift
  138. ;;
  139. *)
  140. print_usage
  141. # echo "111"
  142. break
  143. ;;
  144. esac
  145. done
  146. mkVerison() {
  147. touch "$outputFolder/4dage-ffmpeg-img2video-$version.version"
  148. }
  149. takeVideoCap() {
  150. 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"
  151. }
  152. covertVideoTask() {
  153. picsFolderType=${picsFolder##*.}
  154. allImages="$picsFolder/%d.jpg"
  155. FFREPORT=file="$outputFolder/$outputName.$picsFolderType.txt":level=48 ffmpeg -hide_banner -y -i $allImages \
  156. -preset superfast -r 30 -q:v 2 -pix_fmt yuv420p -c:a copy -shortest "$outputFolder/$outputName.origin.mp4"
  157. FFREPORT=file="$outputFolder/$outputName.$picsFolderType.txt":level=48 ffmpeg -hide_banner -y -i "$outputFolder/$outputName.origin.mp4" \
  158. -i $audioFile -filter_complex "[0:v]split=2[out1][out2]" \
  159. -preset superfast -threads 2 -b 1.8M -minrate 1.8M -maxrate 1.8M -bufsize 2M \
  160. -map '[out1]' -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.mp4" \
  161. -map '[out2]' -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.flv"
  162. rm "$outputFolder/$outputName.origin.mp4"
  163. # FFREPORT=file="$outputFolder/$outputName.$picsFolderType.txt":level=48 ffmpeg -hide_banner -y -i $picsFolder \
  164. # -filter_complex "[0:v]crop=$audioFile,scale=iw/2:ih/2,pad=ceil(iw/2)*2:ceil(ih/2)*2,split=2[out1][out2]" \
  165. # -preset superfast -threads 2 -b 1.8M -minrate 1.8M -maxrate 1.8M -bufsize 2M \
  166. # -map '[out1]' -map 0:a? -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.mp4" \
  167. # -map '[out2]' -map 0:a? -profile:v high -vcodec libx264 -ab 128k "$outputFolder/$outputName.flv"
  168. takeVideoCap
  169. mkVerison
  170. }
  171. clearDist() {
  172. rm -f $outputFolder/*.json
  173. rm -f $outputFolder/*.version
  174. }
  175. concatAllLogFile() {
  176. cat $outputFolder/*.txt >>"$outputFolder/$outputName.full.log"
  177. rm $outputFolder/*.txt
  178. }
  179. if [ ! -z $picsFolder ] && [ ! -z $audioFile ] && [ ! -z $outputName ] && [ ! -z $outputFolder ]; then
  180. if [ ! -d $outputFolder ]; then
  181. mkdir -p $outputFolder
  182. else
  183. clearDist
  184. fi
  185. covertVideoTask
  186. concatAllLogFile
  187. if command_exists "mediainfo"; then
  188. mediainfo --fullscan "$outputFolder/$outputName.mp4" --Output=JSON >"$outputFolder/$outputName.json"
  189. mediainfo --fullscan "$outputFolder/$outputName.flv" --Output=JSON >"$outputFolder/$outputName.flv.json"
  190. fi
  191. echo -e "\033[0;32m"
  192. cat <<EOF
  193. _oo0oo_
  194. 088888880
  195. 88" . "88
  196. (| -_- |)
  197. 0\ = /0
  198. ___/'---'\___
  199. .' \\\\| |// '.
  200. / \\\\||| : |||// \\
  201. /_ ||||| -:- |||||- \\
  202. | | \\\\\\ - /// | |
  203. | \_| ''\---/'' |_/ |
  204. \ .-\__ '-' __/-. /
  205. ___'. .' /--.--\ '. .'___
  206. ."" '< '.___\_<|>_/___.' >' "".
  207. | | : '- \'.;'\ _ /';.'/ - ' : | |
  208. \ \ '_. \_ __\ /__ _/ .-' / /
  209. ====='-.____'.___ \_____/___.-'____.-'=====
  210. '=---='
  211. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  212. 佛祖保佑 TTT 顺利转换
  213. EOF
  214. echo -e "\033[m"
  215. echo "covert done!"
  216. else
  217. if [ ! -n "$1" ]; then
  218. print_usage
  219. else
  220. if [ -z $picsFolder ]; then
  221. echo "${red}miss pics file folder path: 缺图像文件路径"
  222. fi
  223. if [ -z $audioFile ]; then
  224. echo "${red}miss rect spec : 缺少rect的技术参数"
  225. fi
  226. if [ -z $outputName ]; then
  227. echo "${red}miss file name : 缺少输出文件名称"
  228. fi
  229. if [ -z $outputFolder ]; then
  230. echo "${red}miss output path : 缺少输出路径"
  231. fi
  232. fi
  233. fi