Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ASSUMPTIONS
- #
- # Read to make sure you can use this script
- #
- # Platform:
- # This script is for Linux or Mac
- # 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 wiith
- # those tools.
- #
- # Files:
- # This script should work with mp4, avi, wmv, and mpeg files
- # INSTRUCTIONS
- #
- # Steps to use this bash script to generate WEBMs
- #
- # 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 and type: bash create.sh webm
- # 3. ???
- # 4. Profit
- #
- # Steps to use this script to generate GIFs
- # 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 with the script and videos and type: bash create.sh gif
- # 3. ???
- # 4. Profit
- # TIPS
- #
- # 1. This script creates 12 webm or gifs by default. You can change numSegements value for more or less.
- # 2. The gifs this script generates are 400px 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 to modify this.
- # 4. The duration of a webm or gif is 15 seconds by default. You must change this value to be smaller for GIF
- # files
- # 5. 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="00:00:02.000"
- gifWidth=400
- filters="fps=25,scale=${gifWidth}:-1:flags=lanczos"
- gifFiles=()
- # WEBM variables and settings
- webmDuration="00:00:15.000"
- webmWidth=1000
- bitRate="1500K"
- webmFiles=()
- # General variables
- numSegments=12
- videosList=()
- numFiles=0
- mode=$1
- checkNumbers(){
- for webm in *.webm; do
- [ -e "${webm}" ] || continue
- webmFiles+=("${webm}")
- done
- for gif in *.gif; do
- [ -e "${gif}" ] || continue
- gifFiles+=("${gif}")
- done
- num_gif_Files=${#gifFiles[@]}
- num_webm_Files=${#webmFiles[@]}
- numFiles=$(( num_gif_Files + num_webm_Files ))
- }
- composeVidList(){
- for video in *.mp4; do
- [ -e "${video}" ] || continue
- videosList+=("${video}")
- done
- for video in *.wmv; do
- [ -e "${video}" ] || continue
- videosList+=("${video}")
- done
- for video in *.avi; do
- [ -e "${video}" ] || continue
- videosList+=("{$video}")
- done
- for video in *.mpeg; do
- [ -e "${video}" ] || continue
- videosList+=("{$video}")
- done
- }
- dirGifGen(){
- fileName=$1
- start=$2
- outputMinMP4=${fileName//.mp4/}
- outputMinAVI=${outputMinMP4//.avi/}
- outputMinWMV=${outputMinAVI//.wmv/}
- outputMinMPEG=${outputMinWMV//.mpeg/}
- outputName=${outputMinMPEG}
- if [ "${mode}" == "gif" ]; then
- echo "Creating ${outputName}_${numFiles}.gif from time: ${start}"
- # Extract video segment
- ffmpeg -ss "${start}" -hide_banner -loglevel panic -i "${fileName}" -t "${gifDuration}" "${outputName}_${numFiles}.mp4"
- # Create palette for video
- ffmpeg -hide_banner -loglevel panic -i "${outputName}_${numFiles}.mp4" -vf "$filters,palettegen" -y palette.png
- # Create the gif from video segment
- ffmpeg -hide_banner -loglevel panic -i "${outputName}_${numFiles}.mp4" -i palette.png -lavfi "$filters [x]; [x][1:v] paletteuse" -y "${outputName}_${numFiles}.gif"
- # Delete palette
- rm "palette.png"
- elif [ "${mode}" == "webm" ]; then
- echo "Creating ${outputName}_${numFiles}.webm from time: ${start}"
- # Extract video segment
- ffmpeg -ss "${start}" -hide_banner -loglevel panic -i "${fileName}" -t "${webmDuration}" "${outputName}_${numFiles}.mp4"
- # Generate webm
- 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"
- else
- echo "Invalid mode"
- exit 0
- fi
- # Delete video segment if it exists
- [ -e "${outputName}_${numFiles}.mp4" ] || continue
- rm "${outputName}_${numFiles}.mp4"
- ((numFiles++))
- }
- main() {
- if [ "${mode}" != "webm" ] && [ "${mode}" != "gif" ]; then
- echo "Invalid mode"
- exit 0
- else
- echo
- echo "The current mode is: ${mode}"
- echo
- checkNumbers
- composeVidList
- fi
- for (( i= 0; i < ${#videosList[@]}; i++ )); do
- video="${videosList[i]}"
- videoDuration=`ffprobe -i "${video}" -show_entries format=duration -v quiet -of csv="p=0" -sexagesimal`
- videoDuration=`echo $videoDuration | cut -d "." -f1`
- if [ ${videoDuration:1:1} == ":" ]; then
- videoDuration="0${videoDuration}"
- fi
- hours=${videoDuration:0:2}
- minutes=${videoDuration:3:2}
- seconds=${videoDuration:6:2}
- totalSeconds=$(( (10#$hours * 3600) + (10#$minutes * 60) + 10#$seconds ))
- maxSeconds=$totalSeconds
- interval=$(( 10#$totalSeconds / numSegments ))
- # Starting time in seconds. Let's start at little later in the video, at interval * 2
- startingTime=$(( 10#$interval * 2 ))
- for (( k = 0; k < $numSegments; k++ )); do
- hours="$(( 10#$startingTime / 3600 ))"
- if [ ${hours} -gt "0" ]; then
- minutes="$(( (10#$startingTime - (10#$hours * 3600)) / 60 ))"
- else
- minutes="$(( 10#$startingTime / 60 ))"
- fi
- seconds="$(( 10#$startingTime % 60 ))"
- if [ ${hours} -lt "10" ]; then
- hours="0${hours}"
- fi
- if [ ${seconds} -lt "10" ]; then
- seconds="0${seconds}"
- fi
- if [ ${minutes} -lt "10" ]; then
- minutes="0${minutes}"
- fi
- gifTime="${hours}:${minutes}:${seconds}.000"
- if [ ${startingTime} -ge ${maxSeconds} ]; then
- echo
- echo "Complete."
- echo
- exit 0
- fi
- dirGifGen "${video}" "${gifTime}"
- startingTime=$(( 10#$startingTime + 10#$interval ))
- done
- done
- echo
- echo "Complete."
- echo
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement