Skip to content

Instantly share code, notes, and snippets.

@StoryStar
Last active December 3, 2015 00:38
Show Gist options
  • Save StoryStar/c6394039ed78fcc0a785 to your computer and use it in GitHub Desktop.
Save StoryStar/c6394039ed78fcc0a785 to your computer and use it in GitHub Desktop.
Process Videos script
#!/bin/bash
#
# Process Vids 2.0
#
# A script to process Snapchat story files and assemble them into
# one file for upload
#
# (c) Matt Saunders, 2015
#
#Instructions:
# - Run SC-API, as many days in a row as you wish
# - Copy downloaded stories to proc
# - Run bash datesorter.sh to sort stories into their respective days
# - Now run bash procvids.sh to build outputs
# - Finally run bash renamer.sh to rename videos to appropriate name - [scusername]-[datecreated]
# VARIABLES
# Directory containing story files
STORY_DIR="/Users/matt/Documents/JACKTHEAUTORIPPER/testvids/proc"
# Variables to store resolutions for each video should be, determined by minimum resolution
VID_WIDTH=720
VID_HEIGHT=1280
#Iterate through subfolders containing date folders
for dir in /$STORY_DIR/* ; do
echo ""
echo "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
echo ""
#Print current folder
echo "$dir"
echo ""
#Iterate through subfolders containing stories
for d in /$dir/* ; do
echo ""
echo "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
echo ""
#Print current folder
echo "$d"
echo ""
#Iterate through files in subfolder to find smallest video dimensions
for f in $d/* ; do
echo "____________________________________________________________"
echo ""
echo "$f"
if [[ $f = *.mp4 ]]
then
#Use ffprobe to check file height and width
eval $(ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width "$f")
#Check that the correct dimension is being assigned - height should always be greater
if [ ${streams_stream_0_width} -gt ${streams_stream_0_height} ]
then
FILE_WIDTH=${streams_stream_0_height}
FILE_HEIGHT=${streams_stream_0_width}
else
FILE_WIDTH=${streams_stream_0_width}
FILE_HEIGHT=${streams_stream_0_height}
fi
echo "Width: $FILE_WIDTH X Height: $FILE_HEIGHT"
#Store smallest height/width in VID_WIDTH/VID_HEIGHT
if [ $FILE_WIDTH -lt $VID_WIDTH ] ;
then VID_WIDTH=$FILE_WIDTH
fi
if [ $FILE_HEIGHT -lt $VID_HEIGHT ] ;
then VID_HEIGHT=$FILE_HEIGHT
fi
fi
done
echo "____________________________________________________________"
echo ""
echo ""
#After checking for minimum video size, print it
echo "Video will be $VID_WIDTH X $VID_HEIGHT"
echo ""
#Begin processing files
echo ""
echo "------------------------------------------------------------"
echo "------------------------PROCESSING--------------------------"
echo ""
#Iterate through files again
for f in $d/* ; do
#---Build videos in desired format from JPGs
if [[ $f = *.jpg ]]
then echo "Found a jpg!"
MP4NAME="${f%.*}"
MP4NAME="$MP4NAME.mp4"
echo "$MP4NAME"
#Copy original jpg to $d/orig
mkdir -p /$d/orig
cp "$f" /$d/orig
#This needs to be the same dimensions as the previous videos in the folder, so it all can concat nicely
ffmpeg -ar 48000 -ac 2 -f s16le -i /dev/zero -loop 1 -i "$f" -vf scale=$VID_WIDTH:$VID_HEIGHT -c:v libx264 -c:a libvo_aacenc -t 5 -r 24 -pix_fmt yuv420p $MP4NAME
#Delete original
rm "$f"
fi
#---Overlay .PNGs on appropriate videos
done
#After processing, concatenate all videos into one file, titled <CC>-Story-<DATE>.mp4
echo ""
echo "------------------------------------------------------------"
echo "-----------------------CONCATENATING------------------------"
echo ""
#Vars to store commands
argsInputs=()
argsFilterText=()
argsFilterTextAudio=()
count=0
for f in $d/* ; do
if [[ $f = *.mp4 ]]
then
echo $f
argsInputs+=(-i "$f")
#argsFilterText+=([$count:v] [$count:a:0])
argsFilterText+=("[$count:v]scale=$VID_WIDTH:-2,pad=$VID_WIDTH:$VID_HEIGHT,setsar=sar=1/1[v$count];")
argsFilterTextAudio+=([v$count] [$count:a:0])
count=$((count + 1))
fi
done
echo ""
echo "Inputs:"
echo ${argsInputs[@]}
echo ""
echo "Filter text 1"
echo ${argsFilterText[@]}
echo ""
echo "Filter text 2"
echo ${argsFilterTextAudio[@]}
echo ""
echo "Total files:"
echo $count
echo ""
echo "Command:"
echo ffmpeg ${argsInputs[@]} -filter_complex "${argsFilterText[@]} ${argsFilterTextAudio[@]} concat=n=$count:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" /$d/output.mp4
echo ""
ffmpeg ${argsInputs[@]} -filter_complex "${argsFilterText[@]} ${argsFilterTextAudio[@]} concat=n=$count:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" /$d/output.mp4
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment