Creating MP4 with Audio

16 views (last 30 days)
Amir-Homayoun Javadi
Amir-Homayoun Javadi on 1 Oct 2017
Answered: Ayush on 18 Aug 2025
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
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!

Community Treasure Hunt

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

Start Hunting!