How to combine audio and video?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
it will run.. but not play the video... please tell me what the problem and how to resolve that?
videoFReader = vision.VideoFileReader('video1.avi');
[AUDIO,Fs] = audioread('audio1.wav');
videoFWriter = vision.VideoFileWriter('newvideo.avi','AudioInputPort',true);
videoFrame = step(videoFReader);
step(videoFWriter,videoFrame,AUDIO);
release(videoFReader);
release(videoFWriter);
채택된 답변
Walter Roberson
2018년 12월 10일
4 개 추천
You are writing all of the audio for the first frame and only reading and writing one frame.
you need to loop reading one input frame and writing that frame out together with the audio that matches it. For example if your audio output rate were 44100 and your video output were 22 frames per second then you should emit roughly 2000 audio samples for each video frame .
You might want to buffer() your audio to make it easier to write the appropriate amount of audio per frame .
댓글 수: 15
keshav poojari
2018년 12월 10일
편집: keshav poojari
2018년 12월 10일
videoFReader = vision.VideoFileReader('video1.avi');
[AUDIO,Fs] = audioread('audio1.wav');
videoFWriter = vision.VideoFileWriter('newvideo.avi','AudioInputPort',true);
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoFWriter,videoFrame,AUDIO);
end
release(videoFReader);
release(videoFWriter);
Now it play full video without audio!!!
help me...
video_filename = 'video1.avi';
audio_filename = 'audio1.wav';
out_filename = 'newvideo.avi';
info = aviinfo(video_filename);
FR = info.FramesPerSecond;
videoFReader = vision.VideoFileReader(audio_filename);
[AUDIO,Fs] = audioread(filename);
SamplesPerFrame = floor(Fs/FR);
videoFWriter = vision.VideoFileWriter(out_filename, 'AudioInputPort', true, 'FrameRate', FR);
framenum = 0;
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
this_audio = AUDIO(framenum*SamplesPerFrame + 1 : min(end, (framenum+1)*SamplesPerFrame), :);
step(videoFWriter,videoFrame, this_audio);
framenum = framenum + 1;
end
release(videoFReader);
release(videoFWriter);
Note: with this code it would be common for the last few samples of the audio to not be written. This is due to the fact that the audio sampling rate will not typically be an exact multiple of the video frame rate, but the code allocates a fixed number of samples per video frame. For example if the audio is 22100 samples per second and the video is 24 frames per second, that should be an average of 920.833333333333 audio samples per video frame. The code takes the floor, 920 samples per video frame. You can see that after 100 video frames (about 4 seconds) it would be about 83 audio samples behind. You would need to improve the algorithm for selecting out of AUDIO in order to get around this problem.
... ideally you should also take into account that video might be variable frame rate.
keshav poojari
2018년 12월 17일
I run above code but error!
Warning: AVIINFO will be removed in a future release. Use VIDEOREADER instead.
> In aviinfo at 67
In audvidmix2 at 4
Error using vision.VideoFileWriter/step
Changing the size on input 2 is not allowed without first calling the release() method.
Error in audvidmix2 (line 14)
step(videoFWriter,videoFrame, this_audio);
How can i solve this error!!!....
Walter Roberson
2018년 12월 17일
I suspect that the message is arising out of the final partial frame of audio due to the integer issues I highlighted above.
Walter Roberson
2018년 12월 17일
편집: Walter Roberson
2018년 12월 21일
I just noticed that I named the wrong file for the video reader.
The below also avoids using aviinfo().
video_filename = 'video1.avi';
audio_filename = 'audio1.wav';
out_filename = 'newvideo.avi';
videoFReader = VideoReader(video_filename);
FR = videoFReader.FrameRate;
[AUDIO,Fs] = audioread(audio_filename);
SamplesPerFrame = floor(Fs/FR);
videoFWriter = vision.VideoFileWriter(out_filename, 'AudioInputPort', true, 'FrameRate', FR);
framenum = 0;
while hasFrame(videoFReader)
videoFrame = readFrame(videoFReader);
this_audio = AUDIO(framenum*SamplesPerFrame + 1 : min(end, (framenum+1)*SamplesPerFrame), :);
step(videoFWriter,videoFrame, this_audio);
framenum = framenum + 1;
end
delete(videoFReader);
release(videoFWriter);
keshav poojari
2018년 12월 20일
Sir.. Any other method to do this?
Walter Roberson
2018년 12월 20일
yes there are other ways. you could use system() to invoke vlc.exe to combine the files. you could find an appropriate java library and call it from MATLAB . you could develop aa complete h.264 package written in MATLAB that used fread and fwrite.
Walter Roberson
2018년 12월 21일
편집: Walter Roberson
2018년 12월 21일
Corrected code:
video_filename = 'video1.avi';
audio_filename = 'audio1.wav';
out_filename = 'newvideo.avi';
videoFReader = VideoReader(video_filename);
FR = videoFReader.FrameRate;
[AUDIO,Fs] = audioread(audio_filename);
SamplesPerFrame = floor(Fs/FR);
videoFWriter = vision.VideoFileWriter(out_filename, 'AudioInputPort', true, 'FrameRate', FR);
framenum = 0;
while hasFrame(videoFReader)
videoFrame = readFrame(videoFReader);
this_audio = AUDIO(framenum*SamplesPerFrame + 1 : min(end, (framenum+1)*SamplesPerFrame), :);
if size(this_audio,1) < SamplesPerFrame
this_audio(SamplesPerFrame,:) = 0; %zero pad short frames
end
step(videoFWriter,videoFrame, this_audio);
framenum = framenum + 1;
end
delete(videoFReader);
release(videoFWriter);
However, my experiments are having a bit of difficulty in playing the result on Mac. aviinfo() says there is incorrect CHUNK information. Apple Quicktime did not include the sound when playing it. But VLC had no problem.
I notice that vision.VideoFileReader() reads the audio in 'native' format -- int16 rather than the floating point that audioreader() automatically converts to.
keshav poojari
2018년 12월 21일
편집: keshav poojari
2018년 12월 21일
I used above code.. But some Error!! please solve this...
>> audvidmix
Undefined function 'hasFrame' for input arguments of type 'VideoReader'.
Error in audvidmix (line 10)
while hasFrame(videoFReader)
Walter Roberson
2018년 12월 21일
Which MATLAB release are you using? That function was introduced in R2014b.
When you are not using a fairly current release it is important to state your release somewhere so that people do not waste time suggesting solutions that cannot work for you.
keshav poojari
2018년 12월 22일
편집: keshav poojari
2018년 12월 22일
sir,, i using R2013a. In this release any solution???? please
Barely. Your original comment was that you were using R2010a: it is not possible in that version as there was no support for vision.VideoFileWriter or equivalent before R2012a.
video_filename = 'video1.avi';
audio_filename = 'audio1.wav';
out_filename = 'newvideo.avi';
videoFReader = VideoReader(video_filename);
FR = videoFReader.FrameRate;
[AUDIO,Fs] = audioread(audio_filename);
SamplesPerFrame = floor(Fs/FR);
videoFWriter = vision.VideoFileWriter(out_filename, 'AudioInputPort', true, 'FrameRate', FR);
videoframes = read(videoFReader); %reads all frames, not recommended in current MATLAB
for framenum = 0 : size(videoframes, 4)-1
videoFrame = videoframes(:,:,:,framenum+1)
this_audio = AUDIO(framenum*SamplesPerFrame + 1 : min(end, (framenum+1)*SamplesPerFrame), :);
if size(this_audio,1) < SamplesPerFrame
this_audio(SamplesPerFrame,:) = 0; %zero pad short frames
end
step(videoFWriter, videoFrame, this_audio);
end
delete(videoFReader);
release(videoFWriter);
keshav poojari
2018년 12월 23일
THANKS A LOT sir... it worked
keshav poojari
2019년 2월 24일
When i used above code to combine audio and video, The output video take much Storage space... can i compress this??? How??
Walter Roberson
2019년 2월 24일
keshav poojari, you might need to post-process the video.
But have a look at the description of the AudioCompressor and VideoCompressor properties that you can set for vision.VideoFileWriter. The choices depend upon what you have installed on your system, so I cannot tell you what the choices are.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Computer Vision Toolbox에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
