Watch preview while recording snapshots?
5 views (last 30 days)
Show older comments
Hello! I am trying to watch a video from my IP camera live while also recording the output. My code, below, is successful at showing the first frame, and will record for my specified duration. I have just removed the IP address out of caution. The preview screen and recording will end when I hit the quit key.
Is this watching the preview and recording snapshots in real time possible? How would I implement this?
Thanks!
cam = ipcam('http://<URL>','USER','PASS'); % calls the camera, obscured for privacy :)
KbName('UnifyKeyNames');
quit = KbName('q');
preview(cam) % how to continue viewing live footage until closePreview?
frames = 200;
videoRecord = VideoWriter('camera_test', 'MPEG-4');
open(videoRecord)
for i =1:frames
img = snapshot(cam);
writeVideo(videoRecord,img)
[keyIsDown,~,keyCode]=KbCheck(-1);
if (keyIsDown==1 && keyCode(quit))
KbReleaseWait();
break
end
frames = frames+1;
end
closePreview(cam);
close(videoRecord)
0 Comments
Accepted Answer
Abhiram
on 9 Jun 2025
To watch a live preview of the IP camera while recording frames in real time in MATLAB, you can create a custom live preview & recording loop which manually shows each frame in a figure using “imshow” or “image” and update it in real time. A sample implementation is mentioned below:
cam = ipcam('http://<URL>','USER','PASS'); % your IP cam
KbName('UnifyKeyNames');
quit = KbName('q');
framesToCapture = 200;
videoRecord = VideoWriter('camera_test', 'MPEG-4');
open(videoRecord);
% Create a figure window for live preview
hFig = figure('Name', 'Live Camera Feed', 'NumberTitle', 'off');
set(hFig, 'KeyPressFcn', @(obj, evt) assignin('base','keyPressed',evt.Key));
assignin('base','keyPressed','');
for i = 1:framesToCapture
if ~isvalid(hFig) % Exit if figure is closed
break;
end
img = snapshot(cam);
if i == 1
hImage = imshow(img); % Show first frame
else
set(hImage, 'CData', img); % Update image data
end
writeVideo(videoRecord, img); % Record frame
drawnow; % Force update of GUI
% Check for 'q' key to quit early
keyPressed = evalin('base','keyPressed');
if strcmp(keyPressed, 'q')
break;
end
end
close(videoRecord);
clear cam;
close(hFig);
disp('Recording finished.');
0 Comments
More Answers (0)
See Also
Categories
Find more on Video capture and display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!