Guest User

Untitled

a guest
Dec 9th, 2017
2,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # README
  4. #  
  5. #   ABOUT
  6. #  
  7. #       This script generates WEBM or GIF files, depending on your preference, in the folder where it's saved.
  8. #       It will generate 12 WEBM or 30 GIF files per video, as long as the videos are in the same folder as
  9. #       the script. Example: Imagine your folder contains 3 videos - this script will generate 36 WEBMs or 36 GIFs
  10. #       in the same folder.
  11. #      
  12. #       The script will evenly create WEBMs or GIFs throughout the length of the video, so you can expect
  13. #       to see WEBMs or GIFs from the beginning, middle, and end of the video.
  14. #                      
  15. #   PLATFORM
  16. #
  17. #       This script is for Linux, Mac, or Windows.
  18. #       If you're on Windows, install cygwin with bc math package and type bash in command prompt
  19. #       to enter bash mode.
  20. #
  21. #   TOOLS
  22. #
  23. #       You must have ffmpeg installed with --with-libvpx --with-libvorbis
  24. #       If you're on mac, install homebrew and type: reinstall ffmpeg --with-libvpx --with-libvorbis
  25. #             If you're on Windows, I think your ffmpeg already comes with it, but make sure you have ffmpeg with
  26. #       those tools if it doesn't.
  27. #
  28. #   FILES
  29. #
  30. #       This script should work with mp4, avi, wmv, and mpeg files.
  31. #
  32. #   INSTRUCTIONS
  33. #
  34. #       0. Open notepad or a text editor, and save this script as create.sh (or any other name.sh)
  35. #       1. Put this script in a directory which has videos
  36. #       2. In terminal, navigate to your folder containing the script and videos
  37. #               If you want to generate WEBMs type: bash create.sh webm
  38. #               If you want to generate GIFs type: bash create.sh gif
  39. #
  40. #
  41. # TIPS
  42. #
  43. #   1. This script creates 12 WEBMs or 30 GIFs per video by default.
  44. #        You can change numWEBMs or numGIFs value to generate more or less.
  45. #
  46. #   2. The GIFs this script generates are 500px wide by default.
  47. #      Change variable filters' scale value to modify this.
  48. #
  49. #     3. The WEBMs this script generates are 1000px wide by default.
  50. #      Change webmWidth to modify this.
  51. #
  52. #   4. The WEBMs this script generates are 1500K bitrate by default.
  53. #      Change bitrate variable to modify this.
  54. #
  55. #   5. The duration of a WEBM is 10 seconds by default.
  56. #      The duration of a GIF is 1.2 seconds by default.
  57. #
  58. #     6. If you want to shut down the script, hold down control-c in terminal or command prompt. Or simply close
  59. #     the terminal / command prompt.
  60.  
  61. # Initialize Variables
  62.  
  63. # GIF variables and settings
  64. gifDuration=1.2; gifWidth=500; numGIFs=30; gifFiles=()
  65. filters="fps=20,scale=${gifWidth}:-1:flags=lanczos"
  66.  
  67. # WEBM variables and settings
  68. webmDuration=10; webmWidth=1000; numWEBMs=12; webmFiles=(); bitRate="1500K"
  69.  
  70. # General variables
  71. createCMD=""; videosList=(); mode=$1; numFiles=0; numSegments=0; limit=0; duration=0
  72. cutDeci="| bc | cut -d "." -f1"
  73. numSec="ffprobe -v error -select_streams v:0 -show_entries stream=duration -of \
  74.      default=noprint_wrappers=1:nokey=1"
  75.  
  76. # Sets various variables and options for script, depending on mode
  77. setModeOptions(){
  78.       if [ "${mode}" == "gif" ]; then
  79.             numSegments=$numGIFs; createCMD="createGif"; duration=${gifDuration}
  80.       elif [ "${mode}" == "webm" ]; then
  81.             numSegments=$numWEBMs; createCMD="createWebm"; duration=${webmDuration}
  82.       else
  83.             echo "Invalid mode"; exit 0
  84.       fi  
  85. }
  86.  
  87. # Gets mp4, wmv, avi, mpeg files in current directory into list
  88. composeVidList(){
  89.       shopt -s nullglob #ffprobe complains otherwise
  90.       for vid in *.{mp4,wmv,avi,mpeg}; do
  91.             if [ `eval "${numSec} '${vid}' ${cutDeci}"` -gt 60 ]; then
  92.                   videosList+=("${vid}")
  93.             fi
  94.       done
  95. }
  96.  
  97. createGif(){
  98.       fileName="${1}"; start="${2}"; outputName=${fileName%.*}
  99.  
  100.       echo "Creating ${outputName}_${numFiles}.gif at time: ${start} seconds."
  101.  
  102.       ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
  103.             -vf "$filters,palettegen" \-y palette.png # Create palette for video
  104.       ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
  105.             -i palette.png -lavfi "$filters [x]; [x][1:v] paletteuse" -y \
  106.             "${outputName}_${numFiles}.gif" # Create GIF
  107.  
  108.       rm "palette.png" #Remove palette
  109. }
  110.  
  111. createWebm(){
  112.       fileName="${1}"; start="${2}"; outputName="${fileName%.*}"
  113.  
  114.       echo "Creating ${outputName}_${numFiles}.webm from time: ${start} seconds."
  115.  
  116.       ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
  117.             -vf scale=${webmWidth}:-2 -c:v libvpx -b:v ${bitRate} -c:a \
  118.             libvorbis -an "${outputName}_${numFiles}.webm"
  119. }
  120.  
  121. # Ensures WEBM or GIF of length "t" seconds isn't created at startingTime + t > videoLength
  122. getLenLimit(){
  123.       limit=`echo $totalSeconds - ${duration} | bc | cut -d "." -f1`
  124.       ((limit--))
  125. }
  126.  
  127. processVideo(){
  128.       video="${1}"
  129.       totalSeconds=`eval "${numSec} '${video}' ${cutDeci}"`
  130.       startingTime=$(( (10#$totalSeconds * 13) / 100 )) # Skip first ~13% of video to pass boring intros
  131.       interval=$(( (10#$totalSeconds - 10#$startingTime) / numSegments ))
  132.      
  133.       getLenLimit
  134.  
  135.       for (( k = 0; k < ${numSegments}; k++ )); do
  136.             if [ "${startingTime}" -ge "${limit}" ]; then
  137.                   continue
  138.             fi
  139.             (( numFiles++ ))
  140.            
  141.             "${createCMD}" "${video}" "${startingTime}"
  142.             startingTime=$(( 10#${startingTime} + 10#${interval} ))
  143.       done
  144.  
  145.       echo
  146.       numFiles=0
  147. }
  148.  
  149. main() {
  150.       setModeOptions; composeVidList
  151.       echo
  152.     for (( i= 0; i < ${#videosList[@]}; i++ )); do
  153.         processVideo "${videosList[i]}"
  154.     done
  155.     echo -e "\nComplete.\n"
  156. }
  157.  
  158. main
Add Comment
Please, Sign In to add comment