MATLAB Answers

0

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

sotiraw sotiroglou さんによって質問されました 2019 年 5 月 5 日
最新アクティビティ Murugan C
さんによって コメントされました 2019 年 10 月 15 日
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 件のコメント

Murugan C
2019 年 5 月 8 日
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

サインイン to comment.

2 件の回答

回答者: Murugan C
2019 年 5 月 8 日

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

  0 件のコメント

サインイン to comment.


回答者: sotiraw sotiroglou 2019 年 10 月 1 日

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

  1 件のコメント

Murugan C
2019 年 10 月 15 日
Can you please upload your video. I wil look into.

サインイン to comment.



Translated by