How to change video format from AVI to MP4

159 Ansichten (letzte 30 Tage)
Joni Afria
Joni Afria am 12 Jul. 2019
Kommentiert: Image Analyst am 22 Nov. 2021
Hi.
I have a project about video compression (video processing).
How to change video format from AVI to MP4 or YUV to MP4?
Thank's

Antworten (3)

Yash Totla
Yash Totla am 12 Jul. 2019
Convert any video format to .mp4 video format. It does not support the MKV format.
% clc;
% clear all;
% close all;
% Browse Video File :
[ video_file_name,video_file_path ] = uigetfile({'*.mov'});
if(video_file_path==0)
return;
end
% Output path
output_image_path = fullfile(video_file_path,[video_file_name(1:strfind(video_file_name,'.')-1),'.mp4']);
% mkdir(output_image_path);
input_video_file = [video_file_path,video_file_name];
% Read Video
videoFReader = VideoReader(input_video_file);
% Write Video
videoFWrite = VideoWriter(output_image_path,'MPEG-4');
open(videoFWrite);
for count = 1:abs(videoFReader.Duration*videoFReader.FrameRate)
disp(count);
key_frame = read(videoFReader,count);
writeVideo(videoFWrite,key_frame);
end
% Release video object
close(videoFReader);
close(videoFWrite);
disp('COMPLETED');
  3 Kommentare
Rachel Hafner
Rachel Hafner am 4 Jun. 2020
Hi @Walter (or anyone who may be able to help me out with this question!),
I'm wondering how using hasFrame rather than the duration would change the code. Would you replace the videoFReader.FrameRate with hasFrame instead, and still multiply it by the duration? I'm a bit of a newbie to Matlab and coding in general, but I'd love a better explanation on why this is the case if possible.
Thank you!
Walter Roberson
Walter Roberson am 4 Jun. 2020
  • variable frame-rate video has a single nominal frame rate. The actual frame rate encoded in the file could be lower or higher depending how much change there is in the scene. No one frame rate in the header will be sufficient to be able to multiple the frame rate by duration to give an exact frame count
  • Depending on the encoder, NTSC frame rates are typically indicated in the header as either 30.0 fps or 29.97 frames per second, but neither of those are completely correct, and over long enough films the difference between the recorded frame rate and actual frame rate will add up
  • hardware decoders do not use double precision arithmetic: they use integer ratio approximations or at most they use single precision, so there will be different round-off in the counts and frame-rates, so numbers are not likely to exactly match what you get with double precision calculations.
The safe way is to loop over the frames.

Melden Sie sich an, um zu kommentieren.


Karan Mahe
Karan Mahe am 22 Nov. 2021
HI THere,
Would you know how I can convert from .mp4 to .AVI?
Thank you,
Karan

Image Analyst
Image Analyst am 22 Nov. 2021
See my attached demo.
Adapt the input and output formats as needed.
  2 Kommentare
Karan Mahe
Karan Mahe am 22 Nov. 2021
This is amazing. Thank you so much. I have actually been using this:
filename = '/Users/...../';
v = VideoReader(filename);
w = VideoWriter('.....');
open(w);
while hasFrame(v)
frame = readFrame(v);
writeVideo(w,frame);
end
close(w)
But for some reason, i am getting the following errors:
Error using VideoReader/initReader (line 734)
Could not open the file for reading. Permission denied.
Error in audiovideo.internal.IVideoReader (line 136)
initReader(obj, fileName, currentTime);
Error in VideoReader (line 104)
obj@audiovideo.internal.IVideoReader(varargin{:});
Error in ThrowMeInTrash (line 2)
v = VideoReader(filename);
I am very new to matlab and was wondering why I am getting these errors? Could it be because I updated my software to r2021b and I need to install some packages?
Thank you so much,
Karan
Image Analyst
Image Analyst am 22 Nov. 2021
What is filename? Do you have permission to write to that folder? The output file is also not already existing and open/playing is it?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by