MATLAB Answers

How to convert 4K mp4 video into frames?

7 views (last 30 days)
Vimal Raj
Vimal Raj on 8 Jan 2021
Commented: Vimal Raj on 20 Jan 2021
How do i convert 4K video into frames..
video recorded in gopro camera and stored in mp4 format.

Accepted Answer

Nitin Kapgate
Nitin Kapgate on 11 Jan 2021
Edited: Nitin Kapgate on 11 Jan 2021
You can refer to the code snippet below which demonstrates extraction of frames and export of the frames to a folder:
% import the in-built video file, replace 'xylophone.mp4' with your file name
vidObj = VideoReader('xylophone.mp4');
% read the total number of frames
frames = vidObj.NumFrames;
% file format of the frames to be saved in
imageFormat ='.png';
% create a new folder for frames
mkdir frames
% reading and writing the frames
for x = 1 : frames
% converting integer to string
frameIndex = num2str(x);
% concatenating 2 strings to get indexed frame name
frameName = strcat(frameIndex, imageFormat);
% read x'th frame from video
vidFrame = read(vidObj, x);
cd frames
% exporting the frames to the folder
imwrite(vidFrame, frameName);
cd ..
end
You can refer to the documentation for read function here.
  5 Comments
Vimal Raj
Vimal Raj on 20 Jan 2021
Thank you sir... Thank you soo Much. it works..
One more clarification sir, after convering the format, the quality seems tobe lowered than the original video.
The reason why i go for 4K is its quality only. but, after conversion process, the video and frames look like a compressed one (diminished colors). Size of the video also reduced to half of the original.
Any way to rectify that sir.. Also, at the end of frame conversion, again getting this error sir
>> gopro4k
Error using pngwritec
PNG library failed: Write Error.
Error in writepng (line 280)
pngwritec(data, map, filename, colortype, bitdepth, ...
Error in imwrite (line 546)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in gopro4k (line 27)
imwrite(vidFrame, frameName );
An error was encountered while saving the command history

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!