Thursday, May 5, 2011

Assignment 4.3

Here is my finished script for the test last Thursday. It takes in five arguments. The first is the file name of the video you want, the second is the name you want the output video to be, the third is the framerate, and the fifth and sixth allow you to add frame numbers and/or added contrast to the frames. You have to type "frames" to add frame numbers and "effect" to add the contrast effect. The instructions are also included in the script. An example would look like this:

./script.sh input.mov output.mp4 24 frames effect

My script is below. I should mention that it takes a long time to render, so you might want to try it out on a very short video clip.

#!/bin/bash

#arguments
MOVIE=$1
OUTPUT=$2
FRAMERATE=$3
TEXT=$4
EFFECT=$5
echo "script: $0 - Arguments: << input file name >> << output file name >> << framerate >> << print frames. must type 'frames>> <>'"
echo "arguments used: $@"

#make temp directory
mkdir Frames_TEMP
mv $MOVIE Frames_TEMP
cd Frames_TEMP

#extract frames from input file
ffmpeg -i $MOVIE -r $FRAMERATE -f image2 %05d_frame.png

IMAGES=`ls *frame.png`
FRAME=0

if [ "$EFFECT" == "effect" ]; then
echo "You chose to add the effect."
else
echo" You chose to NOT add the effect."
fi

if [ "$TEXT" == "frames" ]; then
echo "You chose to add frame numbers."
else
echo "you chose to NOT add frame numbers."
fi

#apply effect and/or frame numbers

for IMG in $IMAGES
do
if [ "$EFFECT" == "effect" ]; then
echo "$IMG rendering..."
convert $IMG -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" $IMG
#convert -fill 'rgb(50, 0, 100)' -colorize 50% $IMG $IMG
fi
if [ "$TEXT" == "frames" ]; then
echo "$IMG adding frame numbers…"
convert $IMG +repage -fill White -pointsize 50 -gravity "South" -draw "text 1,0 'frame $FRAME'" $IMG
fi
FRAME=$(expr $FRAME + 1)
done

#make video from new frames
ffmpeg -i %05d_frame.png -r 24 -b 2000000 -vcodec libx264 -vpre hq -vpre normal $OUTPUT

#get rid of temp directory
mv $MOVIE ..
mv $OUTPUT ..
cd ..
rm -rf Frames_TEMP