public
Description: Take a series of images and turn them into a movie. Good for time lapse and stop motion.
Homepage: http://carbonsilk.com
Clone URL: git://github.com/kulor/images2movie.git
images2movie / images2movie
100755 85 lines (70 sloc) 1.993 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
MOVIE_FILENAME_EXTENSION=.mov;
MOVIE_FILENAME=timelapse;
TEMP_SERIES_DIR=/tmp/timelapse_series;
SRC_DIR=$1;
FPS=$2;
 
 
#
# Owing to ffmpeg requiring a file format like 001.jpg 089.jpg we need to copy all the files we have to this format in a tempoary location
#
 
serialise_files(){
    COUNTER=0;
if [ ! -d $TEMP_SERIES_DIR ]
     then
echo 'Making temp directory for images '$TEMP_SERIES_DIR;
mkdir $TEMP_SERIES_DIR;
fi
    for SRC_FILE in `find $SRC_DIR -iregex '.*jp[e]*g'` ;
        do
            #Write the filename to be friendly with ffmpeg's odd filename input
            FILENAME=`printf '%05d.jpg' $COUNTER`
            cp $SRC_FILE $TEMP_SERIES_DIR'/'$FILENAME;
            let COUNTER=COUNTER+1;
        done
}
 
 
#
# Remove all the tempoarily generated files needed for ffmpeg
#
 
remove_serialised_files(){
    echo "Deleting temp directory" $TEMP_SERIES_DIR;
    rm -rf $TEMP_SERIES_DIR;
}
 
 
#
# Check if the movie already exists, if so append a date to it to disambiguate.
#
 
check_movie_exists(){
if [ -f $MOVIE_FILENAME$MOVIE_FILENAME_EXTENSION ]
then
MOVIE_FILENAME=$MOVIE_FILENAME'_'`date +"%m_%d_%Y-%H_%S"`;
fi
}
 
 
#
# Make the movie from a list of correctly formatted filenames
#
 
make_movie(){
    serialise_files
check_movie_exists;
    echo "Trying to make movie" $MOVIE_FILENAME$MOVIE_FILENAME_EXTENSION;
if [ ! $FPS ]
then
FPS=30;
fi
if [ -f $TEMP_SERIES_DIR/00000.jpg ]
then
     nice ffmpeg -r $FPS -s hd720 -vcodec copy -i $TEMP_SERIES_DIR/%05d.jpg $MOVIE_FILENAME$MOVIE_FILENAME_EXTENSION;
else
echo 'No images found to process'
fi
    remove_serialised_files
}
 
#
# Only if we have a working source directory run the make_movie function
#
 
if [ -d $SRC_DIR ]
    then
        make_movie
echo '--------------------------------------';
echo 'Movie generated: '$MOVIE_FILENAME$MOVIE_FILENAME_EXTENSION
echo '--------------------------------------';
else
echo 'You have not specified a valid directory to work from.'
fi