How to read a video and then write it, resulting to exact same video file ( same file size , same pixel values )

32 vues (au cours des 30 derniers jours)
I am using some code to read a video and then write the video again to a file. I will do some manipulation before i write it, but for now lets say we only want to write it back to disk.
But the video i am writing, whatever setting i use for videowriter, i can never get the same size of video ( size increases in disk or changes, and it also changes in pixel values. I can get the exact same pixel values, but only when writing uncompressed avi and this is multiplying the file size of the written file )
My original video is jpeg compressed but i dont know any more details about it, so i will use here the default traffic.avi matlab video which is also compressed.
If i use option Uncompressed avi i will get exact same pixel values, but times x the original filesize. If i use Motion JPEG AVI i will get a bit bigger filesize than original and also no matching pixel values...
Is there a way to write video using VideoWriter and some combination of properties to get the exact same pixel values and exact same file size as the original input?
I cant believe that this cannot happen in matlab.
This is and example code i am using with a default matlab file ( avi file can be also find at toolbox - images -imdata to check the size if you want )
i use this function ( its an official example matlab function ) to read video and the code below to do what i am trying to do
function video = readVideo(filename)
vr = VideoReader(filename);
H = vr.Height;
W = vr.Width;
C = 3;
% Preallocate video array
numFrames = floor(vr.Duration * vr.FrameRate);
video = zeros(H,W,C,numFrames);
% Read frames
i = 0;
while hasFrame(vr)
i = i + 1;
video(:,:,:,i) = readFrame(vr);
end
% Remove unallocated frames
if size(video,4) > i
video(:,:,:,i+1:end) = [];
end
end
Vptr = VideoReader('traffic.avi');
writer = VideoWriter('delete_me.avi', 'Motion JPEG AVI');
writer.FrameRate = Vptr.FrameRate;
open(writer);
% Read and write each frame.
frameCounter = 1;
while hasFrame(Vptr)
thisFrame = readFrame(Vptr); % This is a uint8 variable.
writeVideo(writer,thisFrame);
frameCounter = frameCounter + 1;
end
close(writer);
video = readVideo('traffic.avi');
video2 = readVideo('delete_me.avi');
isequal(video,video2)%
  1 commentaire
Murugan C
Murugan C le 8 Mai 2019
Use this below code.
Vptr = VideoReader('traffic.avi');
writer = VideoWriter('delete_me.avi', 'Motion JPEG AVI');
writer.FrameRate = Vptr.FrameRate;
open(writer);
% Read and write each frame.
frameCounter = 1;
thisFrame = read(Vptr); % This is a uint8 variable.
numFrames = get(Vptr, 'NumberOfFrames');
for k = 1 : numFrames
read_frame = thisFrame(:,:,:,k); % reading frame by frame
writeVideo(writer,read_frame);
end
close(writer);
video = VideoReader('traffic.avi');
video2 = VideoReader('delete_me.avi');
if (get(video,'BitsPerPixel') && get(video2,'BitsPerPixel')) && ...
(get(video,'FrameRate') && get(video2,'FrameRate'))
disp('Both are equal data');
else
error('Both are not equal data : Please check your write data');
end

Connectez-vous pour commenter.

Réponses (2)

Murugan C
Murugan C le 8 Mai 2019
Use this below code.
Vptr = VideoReader('traffic.avi');
writer = VideoWriter('delete_me.avi', 'Motion JPEG AVI');
writer.FrameRate = Vptr.FrameRate;
open(writer);
% Read and write each frame.
frameCounter = 1;
thisFrame = read(Vptr); % This is a uint8 variable.
numFrames = get(Vptr, 'NumberOfFrames');
for k = 1 : numFrames
read_frame = thisFrame(:,:,:,k); % reading frame by frame
writeVideo(writer,read_frame);
end
close(writer);
video = VideoReader('traffic.avi');
video2 = VideoReader('delete_me.avi');
if (get(video,'BitsPerPixel') && get(video2,'BitsPerPixel')) && ...
(get(video,'FrameRate') && get(video2,'FrameRate'))
disp('Both are equal data');
else
error('Both are not equal data : Please check your write data');
end

sotiraw sotiroglou
sotiraw sotiroglou le 1 Oct 2019
I do this and the file size is still different between the two files ( 149 kb initial and 209 final for a sample video ) AND pixels also has different values in the two files. The code reports "both are equal data" but falsly ofcourse

Community Treasure Hunt

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

Start Hunting!

Translated by