Alberto - whenever you call writeBufferToFinalVideo the code creates a new VideoWriter, writes five frames to file, and then closes the file. I think that what is happening is that every time when the code creates the VideoWriter object for the prueba file, the file is being re-created and so all information previously written to it is lost. I was able to reproduce this by writing 20 frames to a file, then re-opening it to write five more frames...once close, the video file was smaller than before. What you may want to do is to create the video writer in the main file and then pass it as a parameter to the writeBufferToFinalVideo method.
frameBuffer = zeros(height,width,numChannels,framesPerSymbol);
framesInBuffer = 0;
video = VideoWriter('prueba','MPEG-4');
video.FrameRate = 100;
open(video);
while hasFrame(videoObject)
frame = double(readFrame(videoObject));
frameBuffer = shiftBuffer(frameBuffer,frame);
framesInBuffer = framesInBuffer + 1;
if framesInBuffer > framesPerSymbol
framesInBuffer = framesPerSymbol;
bypassEncoding = false;
else
bypassEncoding = true;
end
if ~bypassEncoding
if canWeEncode(frameBuffer,alpha,threshold)
encodedBuffer = steganographicEncoding(frameBuffer,width,height,codeRows,codeCols,alpha,sigma);
writeBufferToFinalVideo(encodedBuffer,video);
else
writeFrameToFinalVideo(squeeze(frameBuffer(:,:,:,1)));
end
end
end
close(video);
with
function [video] = writeBufferToFinalVideo(buffer,videoWriterObj)
for i = 1:size(buffer,4)
img = buffer(:,:,:,i);
img(img>1) = 1;
img(img<0) = 0;
writeVideo(videoWriterObj,img);
end
end
You will probably need to do the same for writeFrameToFinalVideo.
Best Answer