How to play a video with synced audio, maybe using the Computer Vision System Toolbox but have no idea how to?

10 visualizaciones (últimos 30 días)
Hello, I am trying to play mp4 files or any other video file type in Matlab with synced audio but all I am see is that I should use the Computer Vision System Toolbox but no explanation on how to use that toolbox to do what I want to do. How do you use the toolbox to play video with audio? I have tried without the toolbox and got it to have video but no audio, audio but no video and having both audio and video but out of sync with each other using a loop. Below is the loop with the audio and video out of sync.
v = VideoReader('Guided Box Breathing.mp4');
currAxes = axes;
[y,Fs] = audioread('Guided Box Breathing.mp4');
player = audioplayer(y, Fs);
play(player)
while hasFrame(v)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', currAxes);
currAxes.Visible = 'off';
play(player)
pause(0.5/v.FrameRate);
end
Anyone know what I need to do in order to play videos with synced audio in Matlab? Thanks

Respuestas (1)

Rahul
Rahul el 20 de Mzo. de 2023
vision.VideoFileReader support reading audio only for some file formats such as AVI, WMV, some MPEG-1/2 formats. It does not support reading audio from MP4 files.
Similarly, VideoFileWriter supports writing audio only to AVI and WMV files.
Howeve, I did not find any player in MATLAB which plays images and sound together. "implay" function only plays video and not audio. You can confirm this at implay.
VidObj=VideoReader('some_video.mp4');
n=VidObj.NumFrames;
videoFReader = vision.VideoFileReader('some_video.mp4');
videoFWriter = vision.VideoFileWriter('vid_new_compressed_ffd5.avi',...
'AudioInputPort',1,'AudioDataType','int16','VideoCompressor',"None (uncompressed)",'FileFormat','avi',...
'FrameRate',videoFReader.info.VideoFrameRate);
[audio,fs]=audioread('some_video.mp4');
op=floor(fs/videoFReader.info.VideoFrameRate);
for i=1:n
videoFrame = step(videoFReader);
audios=audio( (i-1)*op + 1 : i*op , : );
step(videoFWriter, videoFrame,audios);
end
release(videoFReader);
release(videoFWriter);
videoFReader = vision.VideoFileReader('vid_new_compressed_ffd5.avi');
videoFReader.VideoOutputDataType = "uint8";
videoFReader.ImageColorSpace = "RGB";
videoFReader.AudioOutputPort = true;
videoFReader.AudioOutputDataType = "double";
fs = 44100;
op=floor(fs/videoFReader.info.VideoFrameRate);
ii = 1;
while ~isDone(videoFReader)
[img(:,:,:,ii), aud((ii-1)*op + 1 : ii*op,:)] = step(videoFReader);
imshow(img(:,:,:,ii))
sound(aud, fs)
ii = ii+1;
end
Please accept the answer if it resolves your issue.

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by