Random Video Part Extraction with vision.Video

4 次查看(过去 30 天)
Hello,
I'm trying to extract random parts from many (random) videos for a stage act. I thought it might be easier to do this on Matlab, but I am stuck on code:
Main parts/procedure I wanted to have
1. Pick a random video from directory
2. Choose a random video duration between 1 to 10 seconds.
3. Choose a random video crop starting point within selected video
4. Crop the video staring from crop point (3) with duration (2) (duration is in video frames count)
5. Export
I wrote below code, but I couldnt start the export from the random crop point, It exports from start with randomly selected duration.
Asa summary in the code below, Cstart is the random start point within video and Cend is the end point. Code crops the duration between Cstart and Cend correct, but from the beginning not random mid point.
Thanks in advance.
All
cd('C:\Users\Alper\Desktop\BamBamBam\vids\');
tmp = dir('*.mp4'); % all video clips
VideoList = {tmp.name}'; %list
MXS = 10 %%max crop duration in second
k = numel(VideoList) %%video count
for j = 1:1000 %%i.e. 1000 video samples
filename=strcat(num2str(j),'.mp4')
VR = randi([1 k],1,1) %%input video randomization
VT = VideoReader(VideoList{VR}) %%videoread
DR = VT.Duration %%video duration
FR = VT.FrameRate %%framerate
DRL = randi([1 round((DR-MXS)*999)],1,1)/1000 %%video start point randomization
FRL = randi([1 MXS],1,1) %%video crop length randomization
Cstart = FR*round(DRL)
Cend = Cstart + FR*round(FRL)
videoFReader = vision.VideoFileReader(VideoList{VR});
videoFWriter = vision.VideoFileWriter(filename,'FileFormat','MPEG4','FrameRate',...
videoFReader.info.VideoFrameRate);
y = vision.VideoFileWriter
y.VideoCompressor='MJPEG Compressor'
for i=Cstart:Cend
videoFrame = step(videoFReader);
step(videoFWriter,videoFrame);
end
release(videoFReader);
release(videoFWriter);
end

采纳的回答

Walter Roberson
Walter Roberson 2018-3-20
for i=Cstart:Cend
Make that start at 1. But after you have step() the input, only step the output if i is at least as large as Cstart
Your code does not quite do what you intended to because you did not take into account that the input might be variable frame rate. It would be more robust to step() from the beginning of the movie and compare the VideoReader CurrentTime property to your start and end times expressed in seconds.
  1 个评论
Al Onen
Al Onen 2018-3-21
I realized I over complicated simpler tasks on my code after reading your comment and changed it, thank you :).

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by