Matlab code to add white noise to a movie clip

27 Views Asked by At

I am coding to add white noise to a movie clip and save the new one. The following is the code, but it did not work. Actually, the sound is slowed, which does not match the video. The "audio_with_noise.wav" is normal as expected, but the sound in the 'final_output_video_with_audio.avi' is slowed down.

% Input and output file names
inputVideoFileName = 'original.mp4';
outputAudioFileName = 'audio_with_noise.wav';
outputVideoFileName = 'final_output_video_with_audio.avi';

% sound
% Read the audio from the video
[y, fs] = audioread(inputVideoFileName);
% Generate white noise
noiseVolume = 0.5;
yn = noiseVolume * randn(size(y));
% Add white noise to the audio
y_with_noise = y + yn;
% Save the audio with noise to a WAV file
audiowrite(outputAudioFileName, y_with_noise, fs);

% video
% Read the original video
v = VideoReader(inputVideoFileName);
nfr = v.NumberofFrames;
audioR = dsp.AudioFileReader(outputAudioFileName);
% default of VideoOutDataType is 'single', converting it to a similar format is essential)
fr = v.NumFrames;

% Create a VideoWriter object for the output video
vW = vision.VideoFileWriter(outputVideoFileName, 'AudioInputPort', true, 'FrameRate', v.FrameRate);

while hasFrame(v)
    I = readFrame(v);
    audio = audioR();
    vW(I, audio);
end

release(audioR);
release(vW);

What can I do? I could not finish it.

0

There are 0 best solutions below