Created
September 2, 2017 18:12
-
-
Save tristansokol/0331104c811eefc76e25df85eae6414e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# a script to make great time lapses from the narrative clip. | |
# copy files to safe place, iterate through with ls-v, use spinners | |
# find a .mp4, rename the files, make a timelapse with the files (use spinners and headless ffmpeg) | |
# And then turn into ts files? delete videos in progress? Less ram/storage better! | |
#convert videos to better files with ffmpeg | |
# concatenate all the ts files, be sure to specify codec | |
# clean up. | |
RATE=8 | |
OUTPUTNUM=0 | |
function verboseprint { | |
if [[ $VERBOSE == 1 ]]; then | |
echo $1 | |
fi | |
} | |
function spinner { | |
pid=$2 # Process Id of the previous running command | |
spin='-\|/' | |
i=0 | |
while kill -0 $pid 2>/dev/null | |
do | |
i=$(( (i+1) %4 )) | |
printf "\r$1${spin:$i:1}" | |
sleep .1 | |
done | |
echo '' | |
} | |
function makemoviefromframes { | |
verboseprint "makemoviefromframes called with lastfile of $1" | |
i=0 | |
for file2 in `ls -v ${FILES}-tmp/`; do | |
verboseprint "comparing $file2 to $1" | |
printf -v j "%05d" $i | |
if [[ $VERBOSE == 1 ]]; then | |
mv -v ${FILES}-tmp/$file2 ${FILES}-tmp/`printf frame%05d.jpg $((10#$j))` | |
else | |
mv ${FILES}-tmp/$file2 ${FILES}-tmp/`printf frame%05d.jpg $((10#$j))` | |
fi | |
if [[ $file2 == $1 ]]; then | |
verboseprint "BREAKING ON LAST FILE" | |
break; | |
fi | |
if [[ -d ${FILES}-tmp/$file2 ]]; then | |
verboseprint "BREAKING ON directory" | |
break; | |
fi | |
i=$((i+1)) | |
done | |
if [ ! -d "${FILES}-tmp/output/" ]; then | |
mkdir ${FILES}-tmp/output/ | |
fi | |
if [[ $VERBOSE == 1 ]]; then | |
ffmpeg -r $RATE -i $FILES-tmp/frame%05d.jpg -f lavfi -i anullsrc=r=48000 -s 2448x3264 -vcodec libx264 -vf "transpose=2" -pix_fmt yuv420p -shortest ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 | |
rm ${FILES}-tmp/frame*.jpg | |
ffmpeg -i ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts ${FILES}-tmp/output/output${OUTPUTNUM}.ts | |
else | |
ffmpeg -loglevel panic -hide_banner -r $RATE -i $FILES-tmp/frame%05d.jpg -f lavfi -i anullsrc=r=48000 -s 2448x3264 -vcodec libx264 -vf "transpose=2" -pix_fmt yuv420p -shortest ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 & | |
spinner "making movie $OUTPUTNUM from frames" $! | |
rm ${FILES}-tmp/frame*.jpg | |
ffmpeg -loglevel panic -hide_banner -i ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts ${FILES}-tmp/output/output${OUTPUTNUM}.ts & | |
spinner "converting video $OUTPUTNUM to .ts" $! | |
rm ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 | |
fi | |
OUTPUTNUM=$((OUTPUTNUM+1)) | |
} | |
function makemoviefrommovie { | |
if [[ $VERBOSE == 1 ]]; then | |
ffmpeg -i ${FILES}-tmp/${1} -vf "scale=(iw*sar)*min(2448/(iw*sar)\,3264/ih):ih*min(2448/(iw*sar)\,3264/ih), pad=2448:3264:(2448-iw*min(2448/iw\,3264/ih))/2:(3264-ih*min(2448/iw\,3264/ih))/2" ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 | |
ffmpeg -i ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts ${FILES}-tmp/output/output${OUTPUTNUM}.ts | |
rm -v ${FILES}-tmp/${1} | |
rm -v ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 | |
else | |
ffmpeg -loglevel panic -hide_banner -i ${FILES}-tmp/${1} -vf "scale=(iw*sar)*min(2448/(iw*sar)\,3264/ih):ih*min(2448/(iw*sar)\,3264/ih), pad=2448:3264:(2448-iw*min(2448/iw\,3264/ih))/2:(3264-ih*min(2448/iw\,3264/ih))/2" ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 & | |
spinner "making movie $OUTPUTNUM from $1" $! | |
ffmpeg -loglevel panic -hide_banner -i ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts ${FILES}-tmp/output/output${OUTPUTNUM}.ts & | |
spinner "making ts $OUTPUTNUM from $1" $! | |
rm ${FILES}-tmp/${1} | |
rm ${FILES}-tmp/output/output${OUTPUTNUM}.mp4 | |
fi | |
OUTPUTNUM=$((OUTPUTNUM+1)) | |
} | |
while getopts ":p:vr:" opt; do | |
case $opt in | |
p) | |
verboseprint "-p: working in $OPTARG" >&2 | |
FILES=$OPTARG | |
;; | |
v) | |
echo "-v:turning on verbose mode" | |
VERBOSE=1 | |
;; | |
r) | |
verboseprint "-r:Setting rate to $OPTARG" | |
RATE=$OPTARG | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [ -d "${FILES}-tmp/" ]; then | |
verboseprint 'deleting old work' | |
rm -r ${FILES}-tmp/ | |
fi | |
mkdir ${FILES}-tmp/ | |
if [[ $VERBOSE == 1 ]]; then | |
# cp -v $FILES/* ${FILES}-tmp/ & | |
(find $FILES/ -exec cp -v {} ${FILES}-tmp/ \;) | |
else | |
# cp $FILES/* ${FILES}-tmp/ & | |
(find $FILES/ -exec cp {} ${FILES}-tmp/ \;) & | |
spinner "copying files to temporary directory $FILES-tmp/\b" $! | |
fi | |
FIRSTFILE=null | |
LASTFILE=null | |
for file in `ls -v ${FILES}-tmp/`; do | |
if [[ $file == 'output' ]]; then | |
break | |
fi | |
verboseprint "Evaluating $file" | |
if [[ $file == *.jpg ]]; then | |
# if [[ $FIRSTFILE == null]]; then | |
# $FIRSTFILE | |
# fi | |
LASTFILE=$file | |
#statements | |
fi | |
if [[ $file != *.jpg ]]; then | |
echo "$file is not an image" | |
verboseprint "stopeed on $file" | |
if [[ $LASTFILE != null ]]; then | |
makemoviefromframes $LASTFILE | |
LASTFILE=null | |
fi | |
if [[ $file == *.mp4 ]]; then | |
makemoviefrommovie $file | |
fi | |
continue; | |
fi | |
done | |
if [[ $LASTFILE != null ]]; then | |
makemoviefromframes $LASTFILE | |
fi | |
# echo 'concatenating' | |
# echo $FILES | |
# echo "concat:$(ls -v -1 ${FILES}-tmp/output/*.ts | perl -0pe 's/\n/|/g;s/\|${FILES}-tmp/output/$//g')" | |
ffmpeg -loglevel panic -hide_banner -i "concat:$(ls -v -1 ${FILES}-tmp/output/*.ts | sort -n -k1.71 | perl -0pe 's/\n/|/g;s/\|${FILES}-tmp\/output\/$//g')" -c:v copy -c:a libfdk_aac -bsf:a aac_adtstoasc ${FILES}-tmp/output/outputfile.mp4 & | |
spinner "concatenating files" $! | |
echo "All Done!" | |
# echo cleaning up output files | |
# rm ${FILES}-tmp/output/*.ts | |
#ffmpeg -loglevel panic -hide_banner -i "concat:$(ls -v -1 /Users/tristans/Desktop/video-scratch/megajapanlapse-tmp/output/*.ts | sort -n -k1.71 | perl -0pe 's/\n/|/g;s/\|\/Users\/tristans\/Desktop\/video-scratch\/megajapanlapse-tmp\/output\/$//g')" -c:v copy -c:a libfdk_aac -bsf:a aac_adtstoasc /Users/tristans/Desktop/video-scratch/megajapanlapse-tmp/output/outputfile2.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
make sure to use the -p (path) flag or else it won't work at all. perl doesn't seem to work on zsh or new macbooks, so
ffmpeg -i "concat:/Users/tristansokol/Pictures/laborlapse-tmp/output/output1.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output2.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output3.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output4.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output5.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output6.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output7.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output8.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output9.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output10.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output11.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output12.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output13.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output14.ts|/Users/tristansokol/Pictures/laborlapse-tmp/output/output15.ts" -c:v copy -c:a aac -bsf:a aac_adtstoasc /Users/tristansokol/Pictures/laborlapse-tmp/output/outputfile.mp4