Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ASSUMPTIONS
  4. #
  5. # Read to make sure you can use this script
  6. #
  7. # Platform:
  8. #   This script is for Linux or Mac
  9. #   If you're on Windows, install cygwin with bc math package and type bash in command prompt
  10. #   to enter bash mode.
  11. #
  12. # Tools:
  13. #   You must have ffmpeg installed with --with-libvpx --with-libvorbis
  14. #   If you're on mac, install homebrew and type reinstall ffmpeg --with-libvpx --with-libvorbis
  15. #   If you're on Windows, I think your ffmpeg already comes with it, but make sure you have ffmpeg wiith
  16. #   those tools.
  17. #
  18. # Files:
  19. #   This script should work with mp4, avi, wmv, and mpeg files
  20.  
  21.  
  22. # INSTRUCTIONS
  23. #
  24. # Steps to use this bash script to generate WEBMs
  25. #
  26. # 0. Open notepad or a text editor, and save this script as create.sh (or any other name.sh)
  27. # 1. Put this script in a directory which has videos
  28. # 2. In terminal, navigate to your folder containing the script and videos and type: bash create.sh webm
  29. # 3. ???
  30. # 4. Profit
  31. #
  32. # Steps to use this script to generate GIFs
  33. # 0. Open notepad or a text editor, and save this script as create.sh (or any other name.sh)
  34. # 1. Put this script in a directory which has videos
  35. # 2. In terminal, navigate to your folder with the script and videos and type: bash create.sh gif
  36. # 3. ???
  37. # 4. Profit
  38.  
  39. # TIPS
  40. #
  41. #   1. This script creates 12 webm or gifs by default. You can change numSegements value for more or less.
  42. #   2. The gifs this script generates are 400px wide by default. Change variable filters' scale value to modify this.
  43. #   3. The webms this script generates are 1000px wide by default. Change webmWidth to modify this.
  44. #   4. The webms this script generates are 1500K bitrate by default. Change bitrate to modify this.
  45. #   4. The duration of a webm or gif is 15 seconds by default. You must change this value to be smaller for GIF
  46. # files
  47. #   5. If you want to shut down the script, hold down control-c in terminal or command prompt. Or simply close
  48. #   the terminal / command prompt
  49.  
  50.  
  51.  
  52. # Initialize Variables
  53.  
  54. # GIF variables and settings
  55. gifDuration="00:00:02.000"
  56. gifWidth=400
  57. filters="fps=25,scale=${gifWidth}:-1:flags=lanczos"
  58. gifFiles=()
  59.  
  60. # WEBM variables and settings
  61. webmDuration="00:00:15.000"
  62. webmWidth=1000
  63. bitRate="1500K"
  64. webmFiles=()
  65.  
  66. # General variables
  67. numSegments=12
  68. videosList=()
  69. numFiles=0
  70. mode=$1
  71.  
  72. checkNumbers(){
  73.  
  74.     for webm in *.webm; do
  75.         [ -e "${webm}" ] || continue
  76.         webmFiles+=("${webm}")
  77.     done
  78.  
  79.     for gif in *.gif; do
  80.         [ -e "${gif}" ] || continue
  81.         gifFiles+=("${gif}")
  82.     done
  83.  
  84.     num_gif_Files=${#gifFiles[@]}
  85.     num_webm_Files=${#webmFiles[@]}
  86.  
  87.     numFiles=$(( num_gif_Files + num_webm_Files ))
  88.  
  89. }
  90.  
  91. composeVidList(){
  92.  
  93.     for video in *.mp4; do
  94.         [ -e "${video}" ] || continue
  95.         videosList+=("${video}")
  96.     done
  97.  
  98.     for video in *.wmv; do
  99.         [ -e "${video}" ] || continue
  100.         videosList+=("${video}")
  101.     done
  102.  
  103.     for video in *.avi; do
  104.         [ -e "${video}" ] || continue
  105.         videosList+=("{$video}")
  106.     done
  107.  
  108.     for video in *.mpeg; do
  109.         [ -e "${video}" ] || continue
  110.         videosList+=("{$video}")
  111.     done
  112.  
  113. }
  114.  
  115. dirGifGen(){
  116.  
  117.     fileName=$1
  118.     start=$2
  119.  
  120.     outputMinMP4=${fileName//.mp4/}
  121.     outputMinAVI=${outputMinMP4//.avi/}
  122.     outputMinWMV=${outputMinAVI//.wmv/}
  123.     outputMinMPEG=${outputMinWMV//.mpeg/}
  124.     outputName=${outputMinMPEG}
  125.  
  126.     if [ "${mode}" == "gif" ]; then
  127.  
  128.         echo "Creating ${outputName}_${numFiles}.gif from time: ${start}"
  129.  
  130.         # Extract video segment
  131.         ffmpeg -ss "${start}" -hide_banner -loglevel panic -i "${fileName}" -t "${gifDuration}" "${outputName}_${numFiles}.mp4"
  132.  
  133.         # Create palette for video
  134.         ffmpeg -hide_banner -loglevel panic -i "${outputName}_${numFiles}.mp4" -vf "$filters,palettegen" -y palette.png
  135.        
  136.         # Create the gif from video segment
  137.         ffmpeg -hide_banner -loglevel panic -i "${outputName}_${numFiles}.mp4" -i palette.png -lavfi "$filters [x]; [x][1:v] paletteuse" -y "${outputName}_${numFiles}.gif"
  138.        
  139.         # Delete palette
  140.         rm "palette.png"
  141.  
  142.     elif [ "${mode}" == "webm" ]; then
  143.  
  144.         echo "Creating ${outputName}_${numFiles}.webm from time: ${start}"
  145.  
  146.         # Extract video segment
  147.         ffmpeg -ss "${start}" -hide_banner -loglevel panic -i "${fileName}" -t "${webmDuration}" "${outputName}_${numFiles}.mp4"
  148.  
  149.         # Generate webm
  150.         ffmpeg -hide_banner -loglevel panic -i "${outputName}_${numFiles}.mp4" -vf scale=${webmWidth}:-2 -c:v libvpx -b:v ${bitRate} -c:a libvorbis "${outputName}_${numFiles}.webm"
  151.  
  152.     else
  153.  
  154.         echo "Invalid mode"
  155.  
  156.         exit 0
  157.    
  158.     fi
  159.  
  160.     # Delete video segment if it exists
  161.     [ -e "${outputName}_${numFiles}.mp4" ] || continue
  162.     rm "${outputName}_${numFiles}.mp4"
  163.    
  164.     ((numFiles++))
  165.    
  166. }
  167.  
  168. main() {
  169.  
  170.     if [ "${mode}" != "webm" ] && [ "${mode}" != "gif" ]; then
  171.  
  172.         echo "Invalid mode"
  173.         exit 0
  174.  
  175.     else
  176.  
  177.         echo
  178.         echo "The current mode is: ${mode}"
  179.         echo
  180.        
  181.         checkNumbers
  182.         composeVidList
  183.  
  184.     fi
  185.  
  186.     for (( i= 0; i < ${#videosList[@]}; i++ )); do
  187.  
  188.         video="${videosList[i]}"
  189.  
  190.         videoDuration=`ffprobe -i "${video}" -show_entries format=duration -v quiet -of csv="p=0" -sexagesimal`
  191.         videoDuration=`echo $videoDuration | cut -d "." -f1`
  192.        
  193.         if [ ${videoDuration:1:1} == ":" ]; then
  194.             videoDuration="0${videoDuration}"
  195.         fi
  196.  
  197.         hours=${videoDuration:0:2}
  198.         minutes=${videoDuration:3:2}
  199.         seconds=${videoDuration:6:2}
  200.  
  201.         totalSeconds=$(( (10#$hours * 3600) + (10#$minutes * 60) + 10#$seconds ))
  202.        
  203.         maxSeconds=$totalSeconds
  204.  
  205.         interval=$(( 10#$totalSeconds / numSegments ))
  206.  
  207.         # Starting time in seconds. Let's start at little later in the video, at interval * 2
  208.         startingTime=$(( 10#$interval * 2 ))
  209.  
  210.         for (( k = 0; k < $numSegments; k++ )); do
  211.  
  212.             hours="$(( 10#$startingTime / 3600 ))"
  213.  
  214.             if [ ${hours} -gt "0" ]; then
  215.                 minutes="$(( (10#$startingTime - (10#$hours * 3600)) / 60 ))"
  216.             else
  217.                 minutes="$(( 10#$startingTime / 60 ))"
  218.             fi
  219.  
  220.             seconds="$(( 10#$startingTime % 60 ))"
  221.  
  222.             if [ ${hours} -lt "10" ]; then
  223.                 hours="0${hours}"
  224.             fi
  225.  
  226.             if [ ${seconds} -lt "10" ]; then
  227.                 seconds="0${seconds}"
  228.             fi
  229.  
  230.             if [ ${minutes} -lt "10" ]; then
  231.                 minutes="0${minutes}"
  232.             fi
  233.  
  234.             gifTime="${hours}:${minutes}:${seconds}.000"
  235.  
  236.             if [ ${startingTime} -ge ${maxSeconds} ]; then
  237.                 echo
  238.                 echo "Complete."
  239.                 echo
  240.                 exit 0
  241.             fi
  242.  
  243.             dirGifGen "${video}" "${gifTime}"
  244.  
  245.             startingTime=$(( 10#$startingTime + 10#$interval ))
  246.            
  247.         done
  248.  
  249.     done
  250.  
  251.     echo
  252.     echo "Complete."
  253.     echo
  254.  
  255. }
  256.  
  257. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement