"Quality" property of VideoWriter object doesn't do anything. How to get higher quality mp4 video?

조회 수: 237 (최근 30일)
I'm trying to encode an animation as a video, using MATLAB 2017b. The frames of my animation are in struct F, which is produced earlier with getframe inside a loop.
The documentation for VideoWriter says, under the quality property,
Video quality, specified as an integer in the range, [0,100]. Higher quality numbers result in higher video quality and larger file sizes. Lower quality numbers result in lower video quality and smaller file sizes.
Quality is available only for objects associated with the MPEG-4 or Motion JPEG AVI profile. After you call open, you cannot change the Quality value.
Fair enough. This is what I'm trying:
V = VideoWriter(myfilename, 'MPEG-4');
V.FrameRate = 5;
V.Quality = 85;
open(V);
writeVideo(V,F);
close(V);
However, regardless of what value I set V.Quality to, it produces exactly the same (heavily compressed) video with exactly the same filesize. This setting seems to be ignored.
Is this a bug? If not, then what am I doing wrong? Either way, how can I produce a higher-quality compressed video? (ideally using mp4/H.264, not M-JPEG etc, as I want it to be widely playable)
Thanks.

채택된 답변

Lucademicus
Lucademicus 2019년 12월 30일
A solution would be to write the video as uncompressed avi, and invoke ffmpeg to compress it to mp4.
I've tested this code in R2019b on Windows and on Ubuntu
%% some example from VideoWriter doc
Z = peaks;
surf(Z);
axis tight manual
set(gca,'nextplot','replacechildren');
%% video file
if isunix % for linux
pathVideoAVI = '~/someVideo.avi'; % filename, used later to generate mp4
elseif ispc % fow windows
pathVideoAVI = 'd:\someVideo.avi'; % filename, used later to generate mp4
end
writerObj = VideoWriter(pathVideoAVI,'Uncompressed AVI');
open(writerObj);
%% animate and write AVI
for k = 1:20
surf(sin(2*pi*k/20)*Z,Z)
frame = getframe(gcf);
writeVideo(writerObj,frame);
end
close(writerObj); % Close the movie file
%% convert AVI to MP4
pathVideoMP4 = regexprep(pathVideoAVI,'\.avi','.mp4'); % generate mp4 filename
if isunix % for linux
[~,~] = system(sprintf('ffmpeg -i %s -y -an -c:v libx264 -crf 0 -preset slow %s',pathVideoAVI,pathVideoMP4)); % for this to work, you should have installed ffmpeg and have it available on PATH
elseif ispc % for windows
[~,~] = system(sprintf('ffmpeg.exe -i %s -y -an -c:v libx264 -crf 0 -preset slow %s',pathVideoAVI,pathVideoMP4)); % for this to work, you should have installed ffmpeg and have it available on PATH
end
Of course you could finish this by removing the AVI file.
  댓글 수: 4
Simon
Simon 2020년 5월 6일
This is a ridiculous solution - in that it's ridiculous that it's necessary! (I don't know if it still is, I haven't tested for a year or more). But thank you for posting it ;-)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Sudeep Das
Sudeep Das 2020년 2월 20일
I have the same issue, how do I get a 1080p quality video?
  댓글 수: 2
Avery Guild-Bingham
Avery Guild-Bingham 2020년 5월 6일
figure('units','pixels','position',[0 0 1920 1080])
For me it was a resolution issue. I build the figures at higher resolution and it works
Simon
Simon 2020년 5월 6일
As noted about six times so far above, this question is not about resolution. It's about compression level. If you want to know how to get a video at a certain resolution, please ask a different question.

댓글을 달려면 로그인하십시오.


Bjorn Gustavsson
Bjorn Gustavsson 2019년 1월 29일
It used to be so that movie2avi and VideoWriter didn't bother with the quality setting on Linux-machines - perhaps that's what you're running into. In my opinion the way to produce the movies with the best control on all settings is to write each frame as an image and then use something like mencoder, ffmpeg or some other standalone program to do that. Another problem might be that the frames grabed with getframe were too small - getframe just grabs a pix-map so its quality is sensitive to the physical size of the frame on-screen. Not much help, but maybe something...
HTH
  댓글 수: 4
Andrew Davies
Andrew Davies 2019년 10월 4일
I didn't find altering the video resoluton changes th eamount of compression used. Increasing the resolution did make things look a bit better, but the high level of compression still produces very poor results.
Simon
Simon 2019년 10월 5일
@sagar This is not a resolution issue. It's about the compression level.

댓글을 달려면 로그인하십시오.


Andrew Davies
Andrew Davies 2019년 10월 4일
I have the same issue. The default quality is terrible, which makes the MPEG-4 format unusable for me.

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by