%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Scott Rick           %%%
%%% CS 534 Final Project %%%
%%% 5/13/2011            %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;

% the number of movies to use as input
numMovies = 1;

% maximum total images to use as input (300 is PhotoSynth's suggested max)
maxTotalFrames = 300;

% maximum total images to use per movie (181 is the maximum memory allowed
% in the CS starsky lab % for 848 x 480 resolution)
maxFrames = 181;
if(maxFrames > maxTotalFrames/numMovies)
    maxFrames = round(maxTotalFrames/numMovies);
end

% where to put images for Photosynth
outputFolder = 'images4';
if ~exist(outputFolder, 'dir')
    mkdir(outputFolder);
end 

for i =1:numMovies
    % read .avi movie file
    xyloObj = mmreader(strcat('v',int2str(i),'.avi'));

    % number of frames in the video clip
    nFrames = xyloObj.NumberOfFrames;

    % set the number of frames to skip between each image we save
    if(nFrames > maxFrames)
        frameInterval = floor(nFrames/maxFrames);
    else
        maxFrames = nFrames;
        frameInterval = 1;
    end

    % save video height and width
    vidHeight = xyloObj.Height;
    vidWidth = xyloObj.Width;

    % read and save one frame at a time
    for k = 1:maxFrames
        % read current frame
        frame = read(xyloObj, k*frameInterval);

        % save current frame
        name = fullfile(outputFolder, strcat('video', int2str(i), 'image', int2str(k), '.jpg'));
        imwrite(frame, name, 'jpg');
    end
end