我正在编写一个函数,用于在视频中输入mp3中的某些声音,如果我尝试保存mp4
文件,则会出错。如果是avi
文件,则可以。问题是avi
会大得多。错误是:
使用vision.VideoFileWriter / step时出错输入参数太多; 预期1(除了对象句柄),得到2.错误 p10_see_only_positive step(videoFWriter,im,au);
这是我的代码:
function p10_see_only_positive(str)
% function to put audio and video together
video_str = sprintf('./videos/%s.mp4', str);
% found out how many frames does the video have
vid = VideoReader(strcat('./videos/', str, '.mp4'));
% check input
if exist(video_str,'file') == 2
cap = cv.VideoCapture(video_str);
[y, fs] = audioread(sprintf('./videos/%s.mp3', str));
val = fs/vid.FrameRate;
else
error('Input file not exist');
end
% prepare video
videoFWriter = vision.VideoFileWriter(sprintf('./videos/final_%s.mp4', str), ...
'FrameRate', vid.FrameRate, 'FileFormat', 'MPEG4', ...
'AudioInputPort', true);
for ii = 1:10000
im = cap.read;
au = y(val*(ii - 1) + 1: val*ii, :);
if isempty(im)
warning('End of file');
break;
else
step(videoFWriter, im, au);
end
end
fprintf('Video: %d\n', nvid);
release(videoFWriter);
end
这是mp4
中的VideoFileWriter:
val =
Properties:
Filename: './videos/final_jornal_all_1.mp4'
FileFormat: 'MPEG4'
FrameRate: 25
FileColorSpace: 'RGB'
Quality: 75
现在是avi
一个:
val =
Properties:
Filename: './videos/final_jornal_all_1.mp4'
FileFormat: 'AVI'
AudioInputPort: true
FrameRate: 25
AudioCompressor: 'None (uncompressed)'
VideoCompressor: 'None (uncompressed)'
FileColorSpace: 'RGB'
我猜mp4
格式无法识别音频输入。
有任何变通方法吗?