Display video and save frames in the same time !

I want to display a video on GUI and in the same time I want it to convert video to frames and save on another folder automatically !
It's create a folder but the folder is empty ! also, I have some errors after I closed the window !
Code:
function Display_Callback(hObject, eventdata, handles)
filename = get(handles.edit1, 'String');
if ~exist(filename, 'file')
warndlg( 'Text in edit box is not the name of a file');
return
end
try
obj = VideoReader(filename);
catch
warndlg( 'File named in edit box does not appear to be a usable movie file');
return
end
ax = handles.ax1;
while hasFrame(obj)
vidFrame = readFrame(obj);
image(vidFrame, 'Parent', ax);
set(ax, 'Visible', 'off');
pause(1/obj.FrameRate);
end
OutVideoDir = 'Frames';
mkdir(OutVideoDir);
for i = 1:obj.NumberOfFrames
img = read(obj,i);
baseFileName = sprintf('%d.png', i);
fullFileName = fullfile(OutVideoDir, baseFileName);
imwrite(img, fullFileName);
end

1 Comment

If you get some errors and want us to help you to fix them, it is a good idea to post the messages. Please do not let the readers guess, which problem you have and what you observe.

Sign in to comment.

Answers (1)

What does "in the same time" mean? Currently you run two loops after each other. What about inserting the code in one loop?
OutVideoDir = 'Frames';
mkdir(OutVideoDir);
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
ImageH = image([], 'Parent', ax);
while hasFrame(obj)
vidFrame = readFrame(obj);
set(ImageH, 'CData', vidFrame);
baseFileName = sprintf('%d.png', i);
fullFileName = fullfile(OutVideoDir, baseFileName);
imwrite(img, fullFileName);
pause(1/obj.FrameRate);
end
It is cheaper to create one image and update its CData afterwards.

2 Comments

Sorry, I meant when the video display in user interface, the video will convert to frames simultaneously (user will display video and in the background of the GUI the video will convert to frames). I hope now it's clear and I'm really Sorry :/
Code:
function Display_Callback(hObject, eventdata, handles)
filename = get(handles.edit1, 'String');
if ~exist(filename, 'file')
warndlg( 'Text in edit box is not the name of a file');
return
end
try
obj = VideoReader(filename);
catch
warndlg( 'File named in edit box does not appear to be a usable movie file');
return
end
OutVideoDir = 'Frames';
mkdir(OutVideoDir);
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
ImageH = image([], 'Parent', ax);
while hasFrame(obj)
vidFrame = readFrame(obj);
set(ImageH, 'CData', vidFrame);
baseFileName = sprintf('%d.png', i);
fullFileName = fullfile(OutVideoDir, baseFileName);
imwrite(img, fullFileName);
pause(1/obj.FrameRate);
end
I get errors:
Index exceeds matrix dimensions.
Error in trackEmpl>Display_Callback (line 153)
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in trackEmpl (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)trackEmpl('Display_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I appreciate your help, thank you..
If handles.ax1 is the handle of an axes object, this line:
ax = handles.ax1('NextPlot', 'add', 'Visible', 'off');
tries to convert the character arrays 'NextPlot', 'add' and so on to numerical indices. You mean:
ax = handles.ax1;
set(ax, 'NextPlot', 'add', 'Visible', 'off');

Sign in to comment.

Asked:

Han
on 3 Feb 2018

Commented:

Jan
on 4 Feb 2018

Community Treasure Hunt

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

Start Hunting!