Skip to content

Instantly share code, notes, and snippets.

@Komsomol
Last active January 22, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Komsomol/7eecec5066620c977dd8 to your computer and use it in GitHub Desktop.
Save Komsomol/7eecec5066620c977dd8 to your computer and use it in GitHub Desktop.
This bash script will take a video, trim it, create a image sequence and create JS file with Base64 versions of the images.
#!/bin/bash
# Use > 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use > 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it such
# as in the --default example).
# note: if this is set to > 0 the /etc/hosts part is not recognized ( may be a bug )
while [[ $# > 1 ]]
do
key="$1"
echo "usage is -o ORIGNALVIDEO -f FILETOMAKE -s start -e end";
# ./cutVideoToImg.sh -o trailer.mp4 -f carlos.mp4 -s 00:00:30 -e 1
rm carlos.mp4;
rm vidData.js;
rm -rf img/;
case $key in
-o|--original)
ORIGINAL="$2"
shift # past argument
;;
-f|--file)
FILE="$2"
shift # past argument
;;
-s|--start)
START="$2"
shift # past argument
;;
-e|--end)
END="$2"
shift # past argument
;;
--default)
DEFAULT=YES
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
echo ORIGINAL = "${ORIGINAL}"
echo FILE = "${FILE}"
echo START = "${START}"
echo END = "${END}"
# trim a clip of video
`ffmpeg -i "${ORIGINAL}" -ss "${START}" -t "${END}" -vcodec libx264 -crf 24 -vf scale=1024:512 -async 1 "${FILE}"`
# `ffmpeg -i "${ORIGINAL}" -ss "${START}" -t "${END}" -vcodec libx264 -crf 24 -vf scale=1024:-1 -async 1 "${FILE}"`
# make img directory
`mkdir img`
# use trimed file to make image sequence
`ffmpeg -i "${FILE}" -q:v 9 img/image%03d.jpg`
# count how many images there are in /img
NUM=`ls -l img* | wc -l`
d=2;
TOTAL="$((NUM - d))";
echo "number of images create was ${TOTAL}";
FILES=img/*
start='"';
end='",';
find . -iname "*.jpg" -exec jpegoptim --strip-all -m100 -o -p {} \
echo "/////////////////////////////===================";
# done
echo "var info=[{start:0,end:${TOTAL},total:${TOTAL}}]; var imgs2 = [" >> vidData.js
for f in `ls $FILES | sort -g`
do
# take action on each file. $f store current file name
echo "Processing $f file..."
# echo ""[data:image/jpeg;base64,"`openssl base64 -in $f` '\n' ]"
TEMP="data:image/jpeg;base64,"`base64 -in $f`;
PAYLOAD="${start}${TEMP}${end}"
echo ${PAYLOAD} >> vidData.js
done
echo "];" >> vidData.js
cp vidData.js base64demo/json/
echo "Finished creating Base64 JS file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment