Handling Large Video Files

10 views (last 30 days)
Chloe
Chloe on 11 Jun 2014
Commented: per isakson on 11 Jun 2014
Hello!
I am currently making program that pulls specific frames from a large video. I have found that loading the video file into the Matlab workspace drastically speeds up the process. However, because the files are large, I receive an error stating that there is not enough memory to open all of the frames. The video is 9min 45sec long, but I want to be able to handle even larger files, on the order of an hour+. Is there any way for Matlab to open these kinds of matrices its workspace?
Also, feel free to let me know if I am going about this the wrong way.
The idea is to create a function such that when it is called, it quickly (hopefully >>10sec) opens part of a video and plays a short clip of it,
For your reference, here are 2 versions of the code
Version 1: The file does not need to be in the workspace, but it is limited by the Java Heap Space available.
function [] = videoPlayer(file1, interval, hour, min, sec)
%Plays the video around a selected frame
% file1 denotes the name of the movie file. User MUST include file
% extension (.mp4, .avi, .mov etc)
% interval denotes how many seconds around selected frame to play
% hour, min, sec denote which frame user would like to select
video = VideoReader(file1);
nFrames = video.NumberOfFrames;
vidHeight = video.Height;
vidWidth = video.Width;
fps = round(video.FrameRate);
time = hour*3600 + min*60 + sec;
frameSelected = fps*time;
initial = frameSelected-(interval*fps);
final = frameSelected+(interval*fps);
frameNum = final-initial;
mov(1:frameNum) = struct ('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
for i = 1:frameNum
mov(i).cdata= read(video,((initial-1)+i));
end
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight]);
movie (hf,mov,1,fps);
end
Version 2: Where you take info from the file itself, and from the array loaded into the Matlab workspace.
function [] = videoPlayer2(file1, video1, interval, hour, min, sec)
%Plays the video around a selected frame
% file1 denotes the name of the movie file. User MUST include file
% extension (.mp4, .avi, .mov etc)
% video1 is file1 opened in Matlab workspace
% interval denotes how many seconds around selected frame to play
% hour, min, sec denote which frame user would like to select
video = VideoReader(file1);
nFrames = video.NumberOfFrames;
vidHeight = video.Height;
vidWidth = video.Width;
fps = round(video.FrameRate);
time = hour*3600 + min*60 + sec;
frameSelected = fps*time;
initial = frameSelected-(interval*fps);
final = frameSelected+(interval*fps);
frameNum = final-initial;
mov(1:frameNum) = struct ('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
for i = 1:frameNum
mov(i).cdata= video1 (:,:,:,((initial-1)+i));
end
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight]);
movie (hf,mov,1,fps);
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!