I understand that you are trying to insert a sound burst into the audio of an ‘.mp4’ video file and then re-attach this modified audio back to the original video. Then try to write the video with the new audio using the ‘step’ function in the ‘vision.VideoFileWriter’ object., that throws an error.
This happens because ‘vision.VideoFileWriter’ does not support audio input when writing to the MPEG-4 (.mp4) file format, even if one sets 'AudioInputPort' to true.
Here is what is happening:
When you create the object of ‘vision.VideoFileWriter’ class like this:
videoFWriter = vision.VideoFileWriter('output.mp4', ...
'FileFormat', 'MPEG4', ...
'AudioInputPort', true, ...
'FrameRate', FR);
MATLAB does not throw an error, but it silently sets ‘AudioInputPort’ back to false. You can confirm this by running the following:
get(videoFWriter)
You will get an output like this:
struct with fields:
Filename: 'output.mp4'
FileFormat: 'MPEG4'
AudioCompressor: 'None (uncompressed)'
VideoCompressor: 'None (uncompressed)'
FrameRate: 30
AudioDataType: 'int16'
FileColorSpace: 'RGB'
Quality: 75
CompressionFactor: 10
AudioInputPort: 0
As you can see, the value for ‘AudioInputPort’ is set to 0(false). This means when later you call the ‘step(videoFWriter,frame,audio)’, it will throw an error because the object doesn’t expect audio at all.
So unfortunately, even if you request audio input for ‘.mp4’, it will not be honoured, and you will hit a mismatch in argument count when calling the ‘step’ method.
As a workaround, you can use the ‘AVI’ format, instead of a ‘.mp4’ format, along with a supported compressor, like ‘MJPEG Compressor’. Please refer the below code to implement the workaround:
videoFWriter = vision.VideoFileWriter('output.avi', ...
'FileFormat', 'AVI', ...
'AudioInputPort', true, ...
'VideoCompressor', 'MJPEG Compressor');
Hope this workaround helps. You may refer to the ‘vision.VideoFileWriter’ documentation for more details: https://www.mathworks.com/help/releases/R2021a/vision/ref/vision.videofilewriter-system-object.html