Creating MP4 with Audio

15 visualizaciones (últimos 30 días)
Amir-Homayoun Javadi
Amir-Homayoun Javadi el 1 de Oct. de 2017
Respondida: Ayush el 18 de Ag. de 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 comentarios
Alex
Alex el 5 de Mayo de 2024
Movida: Adam Danz el 11 de Sept. de 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 el 5 de Mayo de 2024
Movida: Adam Danz el 11 de Sept. de 2024
функцией ffmpreg

Iniciar sesión para comentar.

Respuestas (1)

Ayush
Ayush el 18 de Ag. de 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!

Translated by