Creating MP4 with Audio

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

5 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);
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
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
Alex
Alex on 5 May 2024
Moved: Adam Danz on 11 Sep 2024
this is just a disaster and not only for you, I make two arrays of sound and video, and then connect them, now I’m learning how to do this
Alex
Alex on 5 May 2024
Moved: Adam Danz on 11 Sep 2024
функцией ffmpreg

Sign in to comment.

Answers (1)

Ayush
Ayush on 18 Aug 2025
I understand you are having difficulty in creating MP4 videos with audio using "vision.VideoFileWriter" and looking for alternate methods for achieving the same.
Before jumping towards alternative methods, let me identify the core issue of why the above method is not working:
  • In MATLAB R2017b, "vision.VideoFileWriter" does support audio, but only for AVI files. To know more about this function, run the below command on the MATLAB command window:
>> doc vision.VideoFileWriter
  • When you use " 'FileFormat','MPEG4' ", the system object only accepts video, so "AudioInputPort" is ignored and hence the warning.
  • The 'VideoCompressor' property is only relevant for AVI output. For MPEG4, the codec is fixed internally (H.264), and MATLAB doesn’t allow attaching audio to it in that release.
You can try the following alternate methods to achieve the same functionality:
1. Write AVI with audio, then try converting it to MP4 using "ffmpeg".
  • Save the file as AVI with audio using "vision.VideoFileWriter" and then run in system command:
system('ffmpeg -i Test.avi -vcodec libx264 -acodec aac Test.mp4');
Note: Before this, do make sure that "ffmpeg" is installed in your system.
2. You can also try writing audio and video separately in MATLAB, then mux with "ffmpeg".
Hope it helps!

Answered:

on 18 Aug 2025

Community Treasure Hunt

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

Start Hunting!