Clear Filters
Clear Filters

Random Video Part Extraction with vision.Video

4 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 20 Mar 2018
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 Comment
Al Onen
Al Onen on 21 Mar 2018
I realized I over complicated simpler tasks on my code after reading your comment and changed it, thank you :).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!