Advertisement
Guest User

Untitled

a guest
Aug 9th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Script to generate WebM video for 4Chan using ffmpeg
  3.  
  4. author="bp"
  5. title="Encoded for 4Chan"
  6. resolution="-1"
  7. slices=4
  8. lag=16
  9. cpu=2
  10. mbLimit=3
  11. timeLimit=45
  12. beginTime="00:00:00"
  13. outFile="out.webm"
  14.  
  15. usage()
  16. {
  17. cat << EOF
  18. usage: $0 options
  19. This script will create WebM videos for 4Chan requirements.
  20. OPTIONS:
  21. -h Show this message
  22. -i Input media file (required)
  23. -o Output media file (defaults to out.webm)
  24. -r Output Resolution (240 for 240p, 720 for 720p, etc... Defaults to -1 (original))
  25. -m Target output size in megabytes (default is 3)
  26. -b Begin time (ex: 00:32.65.342, defaults to 0)
  27. -e Time length of video (in seconds, defaults to 45)
  28. -s Slices (1-3 lowdef, 5-8 highdef, defaults 4)
  29. -l Lag in frames (frames to read ahead, 16 default, max 25)
  30. -c Total cpu's to use (default is 2)
  31. -u Specify alternate subtitle file (defaults to source filename but with a .ass extension)
  32. -f Optional ffmpeg filters you would like apply ex: "crop=100:200:10:20, edgedetect"
  33. -p Optional ffmpeg paramaters you would like to apply ex: "-loglevel quiet"
  34. -x Speedplay end time - speedup source video to fit within time frame
  35. (truncate 1 hr into 2 mins, etc. Must use end time of source as parameter ex: 01:23:17)
  36. -a Author ex: "John Doe"
  37. -t Title ex: "My Title"
  38. -v View 4ChanWebM history (show previous commands used)
  39. For any parameter which is more than one word, use quotes to surround it.
  40. EOF
  41. }
  42.  
  43. getBeginTimeSecs()
  44. {
  45. read hours mins secs <<<$(IFS=":"; echo $beginTime)
  46.  
  47. hours=$(($hours*60*60))
  48. mins=$(($mins*60))
  49. beginTimeSecs=$(($hours+$mins+$secs))
  50.  
  51. if [[ $beginTimeSecs -gt 9 ]];
  52. then
  53. beginTimeSecs=$(($beginTimeSecs-5))
  54. beginTimeSecsSlow=5
  55. else
  56. beginTimeSecsSlow=$beginTimeSecs
  57. beginTimeSecs=0
  58. fi
  59.  
  60. echo Begin Time: $(($beginTimeSecs+5))
  61. }
  62.  
  63. getEndSpeedplaySecs()
  64. {
  65. read spHours spMins spSecs <<<$(IFS=":"; echo $spEndTime)
  66.  
  67. spHours=$(($spHours*60*60))
  68. spMins=$(($spMins*60))
  69. spEndTimeSecs=$(($spHours+$spMins+$spSecs))
  70.  
  71. echo Speed End Time: $spEndTime
  72. echo End Time Secs: $spEndTimeSecs
  73. }
  74.  
  75. speedPlay()
  76. {
  77. getBeginTimeSecs
  78. getEndSpeedplaySecs
  79.  
  80. speedLength=$(($spEndTimeSecs-$beginTimeSecs-5))
  81. pts=`echo $speedLength*60/$timeLimit|bc -l`
  82.  
  83. speedplay=", setpts=(60/$pts)*PTS"
  84. filters=$filters$speedplay
  85. }
  86.  
  87. while getopts "hi:o:r:m:b:e:s:l:c:u:f:p:x:a:t:v" OPTION; do
  88. case $OPTION in
  89. h)
  90. usage
  91. exit 1
  92. ;;
  93. i)
  94. inFile=$OPTARG
  95. ;;
  96. o)
  97. outFile=$OPTARG
  98. ;;
  99. r)
  100. resolution=$OPTARG
  101. ;;
  102. m)
  103. mbLimit=$OPTARG
  104. ;;
  105. b)
  106. beginTime=$OPTARG
  107. ;;
  108. e)
  109. timeLimit=$OPTARG
  110. ;;
  111. s)
  112. slices=$OPTARG
  113. ;;
  114. l)
  115. lag=$OPTARG
  116. ;;
  117. c)
  118. cpu=$OPTARG
  119. ;;
  120. u)
  121. subtitle=$OPTARG
  122. ;;
  123. f)
  124. filters=", "$OPTARG
  125. ;;
  126. p)
  127. params=" "$OPTARG
  128. ;;
  129. x)
  130. spEndTime=$OPTARG
  131. ;;
  132. a)
  133. author=$OPTARG
  134. ;;
  135. t)
  136. title=$OPTARG
  137. ;;
  138. v)
  139. cat ~/.4ChanWebMHistory
  140. exit 1
  141. ;;
  142. ?)
  143. usage
  144. exit
  145. ;;
  146. esac
  147. done
  148.  
  149. getBeginTimeSecs
  150.  
  151. if [[ ! -z $spEndTime ]];
  152. then
  153. speedPlay
  154. fi
  155.  
  156. if [[ -z $inFile ]] || [[ ! -r $inFile ]];
  157. then
  158. echo You must specify a readable media file to convert. Ex: -i movie.mkv
  159. exit 2
  160. fi
  161.  
  162. path="${inFile%/*}"
  163. if [[ $path == $inFile ]];
  164. then
  165. path="";
  166. else
  167. path=$path"/"
  168. fi
  169.  
  170. filename=$(basename "$inFile")
  171. extension="${filename##*.}"
  172. filename="${filename%.*}"
  173.  
  174. if [[ -z $subtitle ]];
  175. then
  176. subtitle="$path$filename.ass"
  177. fi
  178.  
  179. bitrate=$(($mbLimit*8192/$timeLimit))"K"
  180. history="4ChanWebM -i \"$inFile\" -o \"$outFile\" -r $resolution -m $mbLimit -b $beginTime -e $timeLimit -s $slices -l $lag -c $cpu -u $subtitle -a \"$author\" -t \"$title\""
  181. echo $history>>~/.4ChanWebMHistory
  182.  
  183. if [ -r "$subtitle" ];
  184. then
  185. ffmpeg -ss $beginTimeSecs -i "$inFile" -vf "scale=-1:$resolution, ass=$subtitle$filters" -ss $beginTimeSecsSlow -t $timeLimit$params -an -c:v libvpx -b:v $bitrate -quality best -slices $slices -auto-alt-ref 1 -lag-in-frames $lag -threads $cpu -pass 1 -f webm /dev/null -y
  186. ffmpeg -ss $beginTimeSecs -i "$inFile" -vf "scale=-1:$resolution, ass=$subtitle$filters" -ss $beginTimeSecsSlow -t $timeLimit$params -an -c:v libvpx -b:v $bitrate -quality best -slices $slices -auto-alt-ref 1 -lag-in-frames $lag -threads $cpu -pass 2 -metadata title="($author) $title" "$outFile" -y
  187. else
  188. ffmpeg -ss $beginTimeSecs -i "$inFile" -vf "scale=-1:$resolution$filters" -ss $beginTimeSecsSlow -t $timeLimit$params -an -c:v libvpx -b:v $bitrate -quality best -slices $slices -auto-alt-ref 1 -lag-in-frames $lag -threads $cpu -pass 1 -f webm /dev/null -y
  189. ffmpeg -ss $beginTimeSecs -i "$inFile" -vf "scale=-1:$resolution$filters" -ss $beginTimeSecsSlow -t $timeLimit$params -an -c:v libvpx -b:v $bitrate -quality best -slices $slices -auto-alt-ref 1 -lag-in-frames $lag -threads $cpu -pass 2 -metadata title="($author) $title" "$outFile" -y
  190. fi
  191.  
  192. if [[ -r "ffmpeg2pass-0.log" ]];
  193. then
  194. rm ffmpeg2pass-0.log
  195. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement