Add the audio of the original video to the video generated by matlab code.

6 次查看(过去 30 天)
After running the following code which is trying to generate a new video based on a given video, i lost the original audio in the output video and i want to add this audio to the output, is there any solution for this that i can add it to my code, in other words i want for each frame of the video i want to add the coresponding audio sample.
The code (Just the useful part of the code):
while i1 < v1.NumberOfFrames && i2 < v2.NumberOfFrames && i3< v3.NumberOfFrames && i4 < v4.NumberOfFrames
if i1 < v1.NumberOfFrames
i1 = i1+1;
im = v1.read(i1);
im = imresize(im, [290 290]);
im = imrotate(im,-90);
iim1 = image(im,'XData',[0 0],'YData',[height1/2 - 290/2 height1/2 - 290/2]);
end
if i2 < v2.NumberOfFrames
i2 = i2+1;
im = v2.read(i2);
im = imresize(im, [290 290]);
im = imrotate(im,180);
iim2 = image(im,'XData',[width1/2-290/2 width1/2-290/2],'YData',[0 0]);
end
if i3 < v3.NumberOfFrames
i3 = i3+1;
im = v3.read(i1);
im = imresize(im, [290 290]);
im = imrotate(im,90);
iim3 = image(im,'XData',[width1-290 width1-290],'YData',[height1/2 - 290/2 height1/2 - 290/2]);
end
if i4 < v4.NumberOfFrames
i4 = i4+1;
im = v4.read(i4);
im = imresize(im, [290 290]);
iim4 = image(im,'XData',[width1/2-290/2 width1/2-290/2],'YData',[height1 - 290 height1 - 290]);
end
%frame=im2frame(gcf);
frame=getframe(gcf);
writeVideo(writerObj,frame);
drawnow

回答(1 个)

Charu
Charu 2025-6-19
Hi Ala, 
To copy the audio from your original input video to the newly created video, you can use the “FFmpeg” command line tool. 
To copy the video from “input_0.mp4” and audio from “input_1.mp4”, use the “-map” option in “ffmpeg” to specify what streams you want as shown in the given example: 
ffmpeg -i input_0.mp4 -i input_1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4 
Refer to the given page for detailed information on how to copy audio streams from one video to another: 
An advantage with this method is as the tool simply copies streams, there is no re-encoding involved, and hence the process is lossless and very fast.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by