MATLAB Answers

Use of vision.VideoFileWriter and vision.VideoFileReader

43 views (last 30 days)
Bruno Kempf
Bruno Kempf on 22 Dec 2016
Commented: Saffana Siddiqua on 28 Jan 2020
Hi,
I'm trying to convert a .avi file with audio to a .mp4 file. I wrote this code using the Computer Vision System Toolbox 7.2 (R2016b)
vfr = vision.VideoFileReader('Cris Drift vs Patrick.avi', 'AudioOutputPort',true);
vfw = vision.VideoFileWriter('Cris Drift vs Patrick.mp4', 'FileFormat','MPEG4', 'AudioInputPort',true, ...
'FrameRate',vfr.info.VideoFrameRate, 'Quality',90);
while ~isDone(vfr)
[frame, audio] = vfr(); % [frame, audio] = step(vfr);
vfw(frame, audio); % step(vfw, frame, audio);
end
release(vfr);
release(vfw);
but i get this error:
Error using vision.VideoFileWriter/parenReference
Too many input arguments; expected 1 (in addition to the object handle), got 2.
Error in avi2mp4 (line 16)
vfw(frame, audio);
I don't know why? I have to pass the audio data as an argument to write it with the video data. It's the same syntax as described in the MATLAB Documentation
Thanking you in anticipation! Kind regards Bruno

  2 Comments

Walter Roberson
Walter Roberson on 22 Dec 2016
It appears to me that it might be a bug, and that step(vfw, frame, audio) would probably work. But could you confirm that you are using R2016b or newer? The syntax you used is new in R2016b.
Bruno Kempf
Bruno Kempf on 22 Dec 2016
Hi Walter, yes I'm using R2016b (Computer Vision Toolbox 7.2). I will add that information in my question. Thanks

Sign in to comment.

Answers (2)

vinoth kumar
vinoth kumar on 9 Apr 2018
Edited: Walter Roberson on 23 Dec 2019
vfw = vision.VideoFileWriter('Cris Drift vs Patrick.mp4', 'FileFormat','MPEG4', 'AudioInputPort',true, ...
'FrameRate',vfr.info.VideoFrameRate, 'Quality',90);
if you want to add audio to your video means you should choose .avi FileFormat and try

  4 Comments

Show 1 older comment
vinoth kumar
vinoth kumar on 11 Dec 2019
Hi Saffana Siddiqua, You can get your video frame rate using the vfr object of vision.VideoFileReader()
frameRate=vfr.info.VideoFrameRate;
Refer the code which is in question. You will find that VideoFileReader functon
Emilien GACOGNE
Emilien GACOGNE on 22 Dec 2019
Hello Vinod, or anyone else who could help me,
I am facing the same error...
Why is writing in a MPEG4 file does not work with audio whereas it works when writing in an AVI file...
For both I am using the instruction:
step(videoFWriter, videoFrame,audios)
How then can I just cut an MP4 video into a smaller part of this video (still in MP4) without any quality loss ?
Thanks in advance for your reply ;)
Emilien

Sign in to comment.


Image Analyst
Image Analyst on 23 Dec 2019
You need to set AudioInputPort to true for the writer, not the reader. From the help:
"Write audio data, specified as false or true. Use this property to control whether the object writes audio samples to the video file. Set this value to true to write audio data. To write audio and video to a file, you must use the .avi format ."
So it appears you can only add audio for an .avi format, not an .mp4 format video.

  0 Comments

Sign in to comment.


Translated by