How can I prevent image compression/degradation on repeated read/write cycles to/from mp4 files?

3 views (last 30 days)
When writing an image in RGB data to an MPEG-4 video file, the RGB data are compressed to leave headroom/footroom in the file data. E.g. a white image of RGB=255 is reduced to 235 in the MP4 file. However, this compression is not undone when reading in from an MP4 file, so if I repeatedly read in from, and write to an MP4, the image is degraded on every iteration. Please see the code below for an example of this. You can see the output mp4 files become darker and darker, despite the data read in being written straight out with no change. Is there any way to prevent this?
writerObj = VideoWriter('Test1','MPEG-4');
open(writerObj);
for frame=1:100%InputVid.NumberOfFrames
InputImage=uint8(255*ones(100,100,3));
InputImage(:,:,2)=0;
InputImage(:,:,3)=0;
writeVideo(writerObj,InputImage);
end
close(writerObj);
InputVid=VideoReader('Test1.mp4');
fps=InputVid.framerate;
writerObj = VideoWriter('Test2','MPEG-4');
writerObj.FrameRate = fps;
open(writerObj);
for frame=1:InputVid.NumberOfFrames
InputImage=read(InputVid, frame);
writeVideo(writerObj,InputImage);
end
close(writerObj);
InputVid=VideoReader('Test2.mp4');
fps=InputVid.framerate;
writerObj = VideoWriter('Test3','MPEG-4');
writerObj.FrameRate = fps;
open(writerObj);
for frame=1:InputVid.NumberOfFrames
InputImage=read(InputVid, frame);
writeVideo(writerObj,InputImage);
end
close(writerObj);

Answers (0)

Community Treasure Hunt

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

Start Hunting!