Creating MP4 with Audio

45 views (last 30 days)
Amir-Homayoun Javadi
Amir-Homayoun Javadi on 1 Oct 2017
Answered: Amir-Homayoun on 14 Oct 2018
Hello
I want to create an MP4 file which contains audio. VideoWriter is not suitable, as it doesn't input any audio data. So, I used vision.VideoFileWriter and step. But I have some problems.
When running the code below:
videoFWriter = vision.VideoFileWriter('Test.mp4', 'FileFormat', 'MPEG4', 'FrameRate', 30, 'AudioInputPort', true);
videoFWriter.VideoCompressor = 'MJPEG Compressor';
I receive the following warning message:
Warning: The AudioInputPort property is not relevant in this configuration of the System object.
When I change the code to the following, the video format is uncompressed AVI, which leads to huge file sizes.
videoFWriter = vision.VideoFileWriter('Test.avi', 'FileFormat', 'AVI', 'FrameRate', 30, 'AudioInputPort', true);
videoFWriter.VideoCompressor = 'MJPEG Compressor';
step(videoFWriter, FrameScreen, FrameAudio);
FrameAudio contains audio data corresponding to one video frame. It seems that it ignores 'MJPEG Compressor'.
I would love to be able to directly create .mp4 files, but if not possible, it is fine with me to create good quality compressed .avi files, and use a 3rd party software to convert to .mp4. I would appreciate any suggestions. Thanks.
My OS is Windows 7, and MATLAB r2017b.
Cheers
Amir-Homayoun
  2 Comments
Nick Choi
Nick Choi on 5 Oct 2017
Edited: Nick Choi on 5 Oct 2017
Regarding the warning message, I think that this is occurring because the 'AudioInputPort' property is not supported for that file configuration.
If you write to a file using this configuration,
videoFWriter = vision.VideoFileWriter('Test.avi', 'FileFormat', 'AVI', 'VideoCompressor' , 'MJPEG Compressor', 'FrameRate', 30, 'AudioInputPort', true);
does it create a smaller file than this configuration?
videoFWriter = vision.VideoFileWriter('Test.avi', 'FileFormat', 'AVI', 'FrameRate', 30, 'AudioInputPort', true);
Amir-Homayoun Javadi
Amir-Homayoun Javadi on 12 Oct 2017
Hello Nick
Thanks for your comment. Sorry for my delayed reply.
I tried it and it again created a very huge file. It is a 3 minute MP4 file and the file is 612MB. For some reason the video compression is not applied. Any suggestion?
Thanks

Sign in to comment.

Answers (1)

Amir-Homayoun
Amir-Homayoun on 14 Oct 2018
Hello,
I come back to this problem again and again. Every time I found a quick and dirty solution (such as creating big .avi files and then convert them to .mp4 using third party applications). But it would be great to be able to create .mp4 files that include audio directly using MATLAB. Would you please let me know if there is any way to do it? This is a sample code that I use:
VideoFrameRate = 24;
TempImage = imread('Sample.jpg');
[TempAudio, Fs] = audioread('Test.mp3');
TempAudio = TempAudio(1:4 * Fs, :); % select the first 4s
AudioStep = Fs / VideoFrameRate;
videoFWriter = vision.VideoFileWriter('Temp.mp4', 'FileFormat', 'MPEG4', 'FrameRate', VideoFrameRate, 'AudioInputPort', true);
videoFWriter.VideoCompressor = 'MJPEG Compressor';
for i = 1:(VideoFrameRate * length(TempAudio) / Fs)
TempIndexBeginning = floor((i - 1) * AudioStep) + 1;
TempIndexEnding = ceil(TempIndexBeginning + AudioStep - 1);
step(videoFWriter, TempImage, TempAudio(TempIndexBeginning:TempIndexEnding));
end
release(videoFWriter);
Thanks
Amir-Homayoun

Community Treasure Hunt

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

Start Hunting!