Clear Filters
Clear Filters

How to save video from frame

5 views (last 30 days)
Marco Rossi
Marco Rossi on 1 Feb 2017
Answered: T.Nikhil kumar on 8 Apr 2024
Hi all, how can I save the video in this function? I want to save the video resized as another video (vid_resize.mp4).
vidobj = vision.VideoFileReader('video.mp4')
viewer = vision.DeployableVideoPlayer;
while (1)
A = step(vidobj);
viewframe = imresize(A,0.5);
step(viewer, viewframe);
end
release(vidobj);
release(viewer);

Answers (1)

T.Nikhil kumar
T.Nikhil kumar on 8 Apr 2024
Hello Marco,
It appears that you want to save the resized frames of a video (video.mp4) into another video (vid_resize.mp4).
I would suggest you to use the ‘VideoWriter’ object in MATLAB. This object allows you to write video files. You can create a ‘VideoWriter’ object, specify the desired output file name, and then write each frame to it after resizing:
  1. Create a ‘VideoWriter’ object and specify the file name and format. Open the object for writing.
  2. Loop through the frames of existing video, resize them and write them to the new file. Close the object after writing.
Please refer to the modified code snippet below for your requirement:
vidobj = vision.VideoFileReader('video.mp4');
viewer = vision.DeployableVideoPlayer;
% Create a VideoWriter object to write the resized video
outputVideo = VideoWriter('vid_resize.mp4', 'MPEG-4');
open(outputVideo);
% Write a loop to resize and write video frames
while ~isDone(vidobj)
A = step(vidobj);
viewframe = imresize(A, 0.5);
step(viewer, viewframe);
writeVideo(outputVideo, viewframe); % Write the frame to the output video
end
release(vidobj);
release(viewer);
close(outputVideo); % Close the VideoWriter object
I also would like to point out that you can use a finite loop instead of infinite loop for the frames of the video. I have used ‘isDone’ function of ‘VideoFileReader’ object which denoted the end of the file. This ensures that the loop exits when all frames have been processed.
Refer to the following documentation for understanding more about the MATLAB objects and functions used above:
Hope this helps you with your work!

Tags

Community Treasure Hunt

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

Start Hunting!