name = 'walking1';

mov = aviread(name);




numFrames = size(mov,2)
width = size(mov(1).cdata,2)
height = size(mov(1).cdata,1)

t = 1;
while (t < numFrames)
    fprintf('frame %i of %i\n', t, numFrames);

    frame1 = mov(t).cdata;
    frame2 = mov(t+1).cdata;
    
    if (frame1 == frame2)
        fprintf('\t->ding!\n');
        mov(t) = [];
        numFrames = numFrames - 1;
    end
    t = t + 1;
end

outputStr = sprintf('%s_cleaned.avi', name);
movie2avi(mov, outputStr);

%{
data = cat(4, mov(:).cdata);
% re-order our dimensions into y, x, t, c
data = permute(data, [1 2 4 3]);

movi = mov(1);

t = 1;
while (t < numFrames)
    fprintf('frame %i of %i\n', t, numFrames);

    frame1 = squeeze(data(:,:,t,:));
    frame2 = squeeze(data(:,:,t+1,:));
    
    if (frame1 == frame2)
        fprintf('\t->ding!\n');
        data(:,:,t,:) = [];
        numFrames = numFrames - 1;
    end
    t = t + 1;
end


% copy our video data back into a playable movie format
fprintf('Packing video data for output...\n');
data = permute(data, [1 2 4 3]);
resFrames = size(data,4);
clear mov2;
for i = 1 : resFrames
    mov2(i) = movi;
    mov2(i).cdata = data(:,:,:,i);
end
fprintf('finished packing!\n');


outputStr = sprintf('%s_cleaned.avi', name);
movie2avi(mov2, outputStr);
%}




