Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # README
- #
- # ABOUT
- #
- # This script generates WEBM or GIF files, depending on your preference, in the folder where it's saved.
- # It will generate 12 WEBM or 30 GIF files per video, as long as the videos are in the same folder as
- # the script. Example: Imagine your folder contains 3 videos - this script will generate 36 WEBMs or 90 GIFs
- # in the same folder.
- #
- # The script will evenly create WEBMs or GIFs throughout the length of the video, so you can expect
- # to see WEBMs or GIFs from the beginning, middle, and end of the video.
- #
- # PLATFORM
- #
- # This script is for Linux, Mac, or Windows.
- # If you're on Windows, install cygwin with bc math package and type bash in command prompt
- # to enter bash mode.
- #
- # TOOLS
- #
- # You must have ffmpeg installed with --with-libvpx --with-libvorbis
- # If you're on mac, install homebrew and type: reinstall ffmpeg --with-libvpx --with-libvorbis
- # If you're on Windows, I think your ffmpeg already comes with it, but make sure you have ffmpeg with
- # those tools if it doesn't.
- #
- # FILES
- #
- # This script should work with mp4, avi, wmv, and mpeg files.
- #
- # INSTRUCTIONS
- #
- # 0. Open notepad or a text editor, and save this script as create.sh (or any other name.sh)
- # 1. Put this script in a directory which has videos
- # 2. In terminal, navigate to your folder containing the script and videos
- # If you want to generate WEBMs type: bash create.sh webm
- # If you want to generate GIFs type: bash create.sh gif
- #
- #
- # TIPS
- #
- # 1. This script creates 12 WEBMs or 30 GIFs per video by default.
- # You can change numWEBMs or numGIFs value to generate more or less.
- #
- # 2. The GIFs this script generates are 500px wide by default.
- # Change variable filters' scale value to modify this.
- #
- # 3. The WEBMs this script generates are 1000px wide by default.
- # Change webmWidth to modify this.
- #
- # 4. The WEBMs this script generates are 1500K bitrate by default.
- # Change bitrate variable to modify this.
- #
- # 5. The duration of a WEBM is 10 seconds by default.
- # The duration of a GIF is 1.2 seconds by default.
- #
- # 6. If you want to shut down the script, hold down control-c in terminal or command prompt. Or simply close
- # the terminal / command prompt.
- # Initialize Variables
- # GIF variables and settings
- gifDuration=1.2; gifWidth=500; numGIFs=30; gifFiles=()
- filters="fps=20,scale=${gifWidth}:-1:flags=lanczos"
- # WEBM variables and settings
- webmDuration=10; webmWidth=1000; numWEBMs=12; webmFiles=(); bitRate="1500K"
- # General variables
- createCMD=""; videosList=(); mode=$1; numFiles=0; numSegments=0; limit=0; duration=0
- cutDeci="| bc | cut -d "." -f1"
- numSec="ffprobe -v error -select_streams v:0 -show_entries stream=duration -of \
- default=noprint_wrappers=1:nokey=1"
- # Sets various variables and options for script, depending on mode
- setModeOptions(){
- if [ "${mode}" == "gif" ]; then
- numSegments=$numGIFs; createCMD="createGif"; duration=${gifDuration}
- elif [ "${mode}" == "webm" ]; then
- numSegments=$numWEBMs; createCMD="createWebm"; duration=${webmDuration}
- else
- echo "Invalid mode"; exit 0
- fi
- }
- # Gets mp4, wmv, avi, mpeg files in current directory into list
- composeVidList(){
- shopt -s nullglob #ffprobe complains otherwise
- for vid in *.{mp4,wmv,avi,mpeg}; do
- if [ `eval "${numSec} '${vid}' ${cutDeci}"` -gt 60 ]; then
- videosList+=("${vid}")
- fi
- done
- }
- createGif(){
- fileName="${1}"; start="${2}"; outputName=${fileName%.*}
- echo "Creating ${outputName}_${numFiles}.gif at time: ${start} seconds."
- ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
- -vf "$filters,palettegen" \-y palette.png # Create palette for video
- ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
- -i palette.png -lavfi "$filters [x]; [x][1:v] paletteuse" -y \
- "${outputName}_${numFiles}.gif" # Create GIF
- rm "palette.png" #Remove palette
- }
- createWebm(){
- fileName="${1}"; start="${2}"; outputName="${fileName%.*}"
- echo "Creating ${outputName}_${numFiles}.webm from time: ${start} seconds."
- ffmpeg -ss "${start}" -t "${duration}" -v error -i "${fileName}" \
- -vf scale=${webmWidth}:-2 -c:v libvpx -b:v ${bitRate} -c:a \
- libvorbis "${outputName}_${numFiles}.webm"
- }
- # Ensures WEBM or GIF of length "t" seconds isn't created at startingTime + t > videoLength
- getLenLimit(){
- limit=`echo $totalSeconds - ${duration} | bc | cut -d "." -f1`
- ((limit--))
- }
- processVideo(){
- video="${1}"
- totalSeconds=`eval "${numSec} '${video}' ${cutDeci}"`
- startingTime=$(( (10#$totalSeconds * 13) / 100 )) # Skip first ~13% of video to pass boring intros
- interval=$(( (10#$totalSeconds - 10#$startingTime) / numSegments ))
- getLenLimit
- for (( k = 0; k < ${numSegments}; k++ )); do
- if [ "${startingTime}" -ge "${limit}" ]; then
- continue
- fi
- (( numFiles++ ))
- "${createCMD}" "${video}" "${startingTime}"
- startingTime=$(( 10#${startingTime} + 10#${interval} ))
- done
- echo
- numFiles=0
- }
- main() {
- setModeOptions; composeVidList
- echo
- for (( i= 0; i < ${#videosList[@]}; i++ )); do
- processVideo "${videosList[i]}"
- done
- echo -e "\nComplete.\n"
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement