How can I write a set number of frames to a file using vision.VideoFileWriter and MATLAB R2015a?

6 visualizzazioni (ultimi 30 giorni)
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)));
end
close(videoreader);
close(videowriter);

Risposta accettata

Robyn Hunt
Robyn Hunt il 15 Lug 2015
Modificato: Robyn Hunt il 15 Lug 2015
After consulting with some contacts, we've discovered that this is an appropriate solution to the problem:
i = 1;
while ~isDone(videoreader)
videoframe = step(videoreader);
if i >= a && i<b;
step(videowriter, videoframe, y((nSamples*(i-a)+1):nSamples*(i-a+1)));
end
i = i + 1;
end
  1 Commento
Walter Roberson
Walter Roberson il 16 Lug 2015
I don't see how that could make a difference compared to my suggestion, unless for some data flushing reason one further step() was needed after reading frame #b.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 13 Lug 2015
I might be misunderstanding, but possibly
for i = 1:b;
videoframe = step(videoreader);
if i >= a
step(videowriter, videoframe, y((nSamples*(i-a)+1):nSamples*(i-a+1)));
end
end
  3 Commenti
Walter Roberson
Walter Roberson il 15 Lug 2015
That does not seem possible. videowriter for Computer Vision uses system objects that overwrite the buffers each time. The first frame is going to be gone by the time "i" becomes as large as "a" for the first step() of the videowriter object.
Robyn Hunt
Robyn Hunt il 15 Lug 2015
My thought process was the same as yours but it doesn't work. I can put the entirety of my code up if it'll help so you can run it yourself but I can assure you it definitely writes from the beginning of the video clip regardless of the index. I can't explain it at all.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by