Creating MP4 with Audio
조회 수: 16 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Ayush
2025년 8월 18일
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".
You can read more about "ffmpeg" here: https://www.mathworks.com/matlabcentral/fileexchange/42296-ffmpeg-toolbox
Hope it helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!