Create a video from frames taken 5 by 5

3 views (last 30 days)
Hello, I have this function to create a video:
function [video] = writeBufferToFinalVideo(buffer,fps)
video = VideoWriter('prueba','MPEG-4');
video.FrameRate = fps;
open(video)
for i = 1:size(buffer,4)
img = buffer(:,:,:,i);
img(img>1) = 1;
img(img<0) = 0;
writeVideo(video,img);
end
close(video);
end
This function have it has a dimension buffer as input
frameBuffer = zeros (height, width, numChannels, framesPerSymbol)
where framePerSymbol will be the number of frames that my symbol lasts. In my case this number equals 5, so I will have 5 frames.
The idea is that from a video I take 5 by 5 frames as a FIFO queue and once I fill the frame buffer, I decide whether I can encode it or not, and once decided I create a video with those frames (if I know has been able to encode or not). This is the code I run it on:
frameBuffer = zeros(height,width,numChannels,framesPerSymbol);
framesInBuffer = 0;
while hasFrame(videoObject)
frame = double(readFrame(videoObject));
frameBuffer = shiftBuffer(frameBuffer,frame);
% We update the framesInBuffer counter
framesInBuffer = framesInBuffer + 1;
if framesInBuffer > framesPerSymbol
framesInBuffer = framesPerSymbol;
bypassEncoding = false;
else
bypassEncoding = true;
end
if ~bypassEncoding
if canWeEncode(frameBuffer,alpha,threshold) % Frames with code
encodedBuffer = steganographicEncoding(frameBuffer,width,height,codeRows,codeCols,alpha,sigma);
writeBufferToFinalVideo(encodedBuffer,100);
else % Only Frames without code
writeFrameToFinalVideo(squeeze(frameBuffer(:,:,:,1)));
end
end
end
Finally you should get a video with the same length as the original. ShiftBuffer will be in charge of taking the frames from 5 to 5 as a FIFO queue.
  2 Comments
Geoff Hayes
Geoff Hayes on 20 Apr 2020
Alberto - what is your question? Or have you posted code that you think others would be interested in?
Alber
Alber on 20 Apr 2020
Sorry, I thought it was clear, when executing my code it generates a 5-frame video instead of the 50 that the video has and I don't know if the problem is the write function or it is directly my fault in the code.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 20 Apr 2020
Edited: Geoff Hayes on 20 Apr 2020
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;
% create the video writer object
video = VideoWriter('prueba','MPEG-4');
video.FrameRate = 100;
open(video);
while hasFrame(videoObject)
frame = double(readFrame(videoObject));
frameBuffer = shiftBuffer(frameBuffer,frame);
% We update the framesInBuffer counter
framesInBuffer = framesInBuffer + 1;
if framesInBuffer > framesPerSymbol
framesInBuffer = framesPerSymbol;
bypassEncoding = false;
else
bypassEncoding = true;
end
if ~bypassEncoding
if canWeEncode(frameBuffer,alpha,threshold) % Frames with code
encodedBuffer = steganographicEncoding(frameBuffer,width,height,codeRows,codeCols,alpha,sigma);
writeBufferToFinalVideo(encodedBuffer,video);
else % Only Frames without code
writeFrameToFinalVideo(squeeze(frameBuffer(:,:,:,1)));
end
end
end
% close the video writer object
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.
  3 Comments
Geoff Hayes
Geoff Hayes on 21 Apr 2020
What variable does the warning correspond to? (Typically warnings can be ignored.)
Alber
Alber on 21 Apr 2020
It's okay, thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!