Recording Videos using two webcam simultaneously for 60sec

3 views (last 30 days)
It takes 90sec to record video of 60sec when I am using two webcam simultaneously whereas it takes 60sec to record 60sec video when only one webcam is used. I want to record 60sec video in 60sec only using two webcams simultaneously. Can anyone help me with this The frame rate of both the webcam is 15fps. Here is my code.
if true
clear all; %%clear all privious variables
clc;
%warning('off','all'); %.... diable warining msg ...;
vid1 = videoinput('winvideo',1, 'YUY2_320x240');
vid2 = videoinput('winvideo',2, 'YUY2_320x240');
set(vid1, 'FramesPerTrigger', Inf);
set(vid2, 'FramesPerTrigger', Inf);
set(vid1, 'ReturnedColorspace', 'rgb');
set(vid2, 'ReturnedColorspace', 'rgb');
vid1.FrameGrabInterval = 1;
vid2.FrameGrabInterval = 1;% distance between captured frames
start(vid1)
start(vid2)
aviObject1 = avifile('v7.avi'); % Create a new AVI file
aviObject2=avifile('v8.avi')
for iFrame = 1:900 % Capture 900 frames
% ...
% You would capture a single image I from your webcam here
% ...
subplot(1,2,1);
I=getsnapshot(vid1);
imshow(I);
subplot(1,2,2);
J=getsnapshot(vid2);
imshow(J);
F = im2frame(I); % Convert I to a movie frame
G = im2frame(J);
aviObject1 = addframe(aviObject1,F); % Add the frame to the AVI file
aviObject2 = addframe(aviObject2,G);
end
aviObject1 = close(aviObject1); % Close the AVI file
aviObject2 = close(aviObject2);
stop(vid1);
stop(vid2);
flushdata(vid1);
flushdata(vid2);
end

Answers (1)

Geoff Hayes
Geoff Hayes on 16 Dec 2014
Rohan - try to figure out where the delay is coming from. If you were to comment out the lines that convert the images to frames and then add them to the aviobject, what happens? Does it still take 90 seconds to write the 900 frames to file? For example,
clear all; %%clear all privious variables
clc;
vid1 = videoinput('winvideo',1, 'YUY2_320x240');
vid2 = videoinput('winvideo',2, 'YUY2_320x240');
set(vid1, 'FramesPerTrigger', Inf);
set(vid2, 'FramesPerTrigger', Inf);
set(vid1, 'ReturnedColorspace', 'rgb');
set(vid2, 'ReturnedColorspace', 'rgb');
vid1.FrameGrabInterval = 1;
vid2.FrameGrabInterval = 1;% distance between captured frames
start(vid1);
start(vid2);
% create cell arrays for the images from the two cameras
vid1Images = cell(900,1);
vid2Images = cell(900,1);
for iFrame = 1:900 % Capture 900 frames
subplot(1,2,1);
I=getsnapshot(vid1);
imshow(I);
subplot(1,2,2);
J=getsnapshot(vid2);
imshow(J);
% save the images
vid1Images{iFrame} = I;
vid2Images{iFrame} = J;
end
% stop the video inputs
stop(vid1);
stop(vid2);
What is the performance like now? And since you have saved all of the images to the cell arrays, you can now convert them to frames and save them to file either using addframe or consider using the videowriter instead.
  6 Comments
rohan gupta
rohan gupta on 18 Dec 2014
Edited: rohan gupta on 18 Dec 2014
Sir it taking less than one minute to capture the video. It now takes around 55sec to capture 900 frames eventhough the frame rate of both the webcam is 15 frames/sec. What could be the reason for this and how should I resolve it?
I have tried recording video using one webcam and it is taking only 45sec to record 1 min video ie 900 frames at the frame rate of 15 frames/sec.
Geoff Hayes
Geoff Hayes on 18 Dec 2014
Rohan - you are capturing the same frame/image more than once. So either pause a certain fraction of a second between frame captures (1/15th of a second) or check the MATLAB docs to see if you can set up a trigger to fire every time a new frame is ready.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!