Video compression code working - Please explain the code

ImFolder=uigetdir;
pngFile = dir(strcat(ImFolder,'\*.png'));
S = [pngFile(:).datenum];
[~,S] = sort(S);
pngFiles = pngFile(S);
VideoFile=strcat(ImFolder,'\Video');
writeObj = VideoWriter(VideoFile);
fps= 15;
writeObj.FrameRate = fps;
open(writeObj);
for t= 1:length(pngFiles)
Frame=imread(strcat(ImFolder,'\',pngFiles(t).name));
writeVideo(writeObj,im2frame(Frame));
end
close(writerObj);
Above is the code i have used to compress the video,
Execution Steps:
1) Initially i recorded a video of 10seconds(Resolution - 160*120) size of video 8mb
2) I extracted all the frames from the video and saved in a folder
3) In this step i used above code
After executing above code i selected the directory where all the frames had been stored
4) After execution i got a video file of size 550Kb and of the same length as the original video
So original video is reduced to 550kb from 8mb, code is working good for my project, please help me the analyze the above code, how actually the compression is done by the code.
Thank You

11 件のコメント

prem
prem 2014 年 5 月 5 日
If I copied the same code and tried, I got the compressed video is of size 0 bytes.How did u get it correctly? please help me
Image Analyst
Image Analyst 2014 年 5 月 6 日
Did you get any error messages? Do you have a folder of PNG files like he did? Is your folder correct? What is length(pngFiles) for you? Maybe you should start your own question after reading this: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
siva p
siva p 2017 年 4 月 5 日
If I copied the same code and tried, I got the compressed video is of size 17 mb but original video size is 6 mb and also not playing compressed video
Walter Roberson
Walter Roberson 2017 年 4 月 5 日
The original video was probably compressed with a different method. You should try passing in a "profile" as the second argument to videowriter. videowriter() is not designed for high compression.
"and also not playing compressed video"
Please explain that further? What is not playing compressed video?
raahul anand
raahul anand 2017 年 5 月 4 日
I now want to decompress the compressed video. Can anyone send me the code or link?
Walter Roberson
Walter Roberson 2017 年 5 月 4 日
"I now want to decompress the compressed video"
VideoReader and readFrame each frame and do whatever with it.
P B ARUNRAJ
P B ARUNRAJ 2017 年 5 月 4 日
Can you send me the code? I have frames of the video ,I can compress but dont know how to decompress.
Walter Roberson
Walter Roberson 2017 年 5 月 4 日
Reading the frames with VideoReader or Vision.VideoFileReader automatically decompresses the frames. There is sample reading code in the documentation for those routines.
dhrgham hani
dhrgham hani 2020 年 6 月 9 日
HI
HOW ARE YOU?
"I now want the compressed video"
Can you send me the code?
Thank you.
ei
ei 2022 年 11 月 26 日
hi please i want to video compression code
Walter Roberson
Walter Roberson 2022 年 11 月 26 日
For video compression,
writeObj = VideoWriter(VideoFile, 'MPEG-4');
now writeVideo the frames.

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2014 年 5 月 5 日

0 投票

writerObj = VideoWriter(filename) constructs a VideoWriter object to write video data to an AVI file with Motion JPEG compression.
So it is going to use Motion JPEG in an AVI container.
kushagra parashar
kushagra parashar 2016 年 7 月 24 日

0 投票

??? Undefined function or method 'VideoWriter' for input arguments of type 'char'.
Error in ==> Untitled at 7 writeObj=VideoWriter(VideoFile);
i am getting this error...what should i do

1 件のコメント

Walter Roberson
Walter Roberson 2016 年 7 月 24 日
VideoWriter was introduced in R2010b. Which MATLAB release are you using?

サインインしてコメントする。

chandra sekhar uppu
chandra sekhar uppu 2017 年 3 月 27 日
編集済み: Walter Roberson 2017 年 3 月 27 日

0 投票

my video after expansion its size increases from 3 mb to 56 mb and its length increases from 1 min to 2 min the video has slow down.. i have used code
vid=VideoReader('w.mp4');
no=vid.NumberOfFrames;
for i=1:1:no
frames=read(vid,i);
base=sprintf('Image%d.png',i);
ful=fullfile(pwd,base);
imwrite(frames,ful);
im(i)=image(frames);
end
after i got frames i saved in a folder and apply this codee.. but i got 2 min and size of 50 mb

2 件のコメント

Walter Roberson
Walter Roberson 2017 年 3 月 27 日
Notice that the code posted in the Question has
fps= 15;
writeObj.FrameRate = fps;
You should adjust that 15 to the rate you need.
With regards to the size: if you are creating a .mp4 output file then you should consider
writeObj = VideoWriter(VideoFile, 'MPEG-4');
Sliver Gaming
Sliver Gaming 2021 年 10 月 4 日
How do i code it :/

サインインしてコメントする。

質問済み:

2014 年 5 月 5 日

コメント済み:

2022 年 11 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by