The code involves reading a video file and rewriting the same video image into a new file with new audio samples (the total audio file has already been trimmed to the correct length). 'start_time' and 'total_time' are defined as inputs to the function, in seconds.
The audio part of the file writes perfectly, but I can't get the video output to match the indexed values I've specified. The video output is the correct length but always starts from index 0. Can anyone help? The relevant parts of my code are shown below.
videoreader = vision.VideoFileReader('input.mp4');videowriter = vision.VideoFileWriter('output.avi', 'FileFormat', 'AVI',... 'AudioInputPort', true, 'FrameRate', videoreader.info.VideoFrameRate);[y, fs] = audioread('input.wav');a = round(start_time * videoreader.info.VideoFrameRate, 0);b = round((start_time + total_time) * videoreader.info.VideoFrameRate, 0);nSamples = round(fs/videoreader.info.VideoFrameRate, 0);for i = a:b; videoframe = step(videoreader); step(videowriter, videoframe, y((nSamples*(i-a)+1):nSamples*(i-a+1)));endclose(videoreader);close(videowriter);
Best Answer