Hi,
is it possible to customize matlabs inbuilt video player? I would like to hide the toolbar and the menu.
Something like:
h=implay('Video.mp4','Menubar','off','toolbar','off')
I am asking, because I need a comfortable way to display and play/pause/ff video files. Or do I need to code it myself?
Thank you!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Dic. de 2020

0 votos

3 comentarios

William Thielicke
William Thielicke el 8 de Dic. de 2020
Editada: William Thielicke el 8 de Dic. de 2020
Thank you, I modified the file and got exactly what I want (standalone script below):
function videofig_mod
% based on João Filipe Henriques, 2010
videofile='xylophone.mp4'; %default video file, available in image processing toolbox
%videofile='VOK Animation.24.avi';
big_scroll = 30;
click = 0;
f = 1; %current frame
fig_handle = figure('Color',[.3 .3 .3], 'MenuBar','none', 'Toolbar','none', 'Units','norm', ...
'WindowButtonDownFcn',@button_down, 'WindowButtonUpFcn',@button_up, ...
'WindowButtonMotionFcn', @on_click,'KeyPressFcn', @key_press,'Name','Video preview','numbertitle','off');
v = VideoReader(videofile);
play_fps = v.FrameRate;
num_frames=v.NumFrames;
%axes for scroll bar
scroll_axes_handle = axes('Parent',fig_handle, 'Position',[0 0 1 0.04], ...
'Visible','off', 'Units', 'normalized');
axis([0 1 0 1]);
axis off
%scroll bar
scroll_bar_width = max(1 / num_frames, 0.02);
scroll_handle = patch([0 1 1 0] * scroll_bar_width, [0 0 1 1], [.8 .8 .8], ...
'Parent',scroll_axes_handle, 'EdgeColor','none', 'ButtonDownFcn', @on_click);
%timer to play video
play_timer = timer('TimerFcn',@play_timer_callback, 'ExecutionMode','fixedRate');
%main drawing axes for video display
axes_handle = axes('Position',[0 0.07 1 0.9]);
%return handles
scroll_bar_handles = [scroll_axes_handle; scroll_handle];
scroll_func = @scroll;
h_fig=imshow(read(v,1));drawnow
set(fig_handle,'units','characters')
set(scroll_axes_handle,'units','characters')
%framenrdisplay
frametext = uicontrol(fig_handle,'Style','text','units', 'characters','Horizontalalignment', 'left','position',[0,1.1,20,1],'String',['Frame Nr.: 1/' num2str(num_frames)]);
function key_press(src, event) %#ok, unused arguments
switch event.Key %process shortcut keys
case 'leftarrow'
scroll(f - 1);
case 'rightarrow'
scroll(f + 1);
case 'pageup'
if f - big_scroll < 1 %scrolling before frame 1, stop at frame 1
scroll(1);
else
scroll(f - big_scroll);
end
case 'pagedown'
if f + big_scroll > num_frames %scrolling after last frame
scroll(num_frames);
else
scroll(f + big_scroll);
end
case 'home'
scroll(1);
case 'end'
scroll(num_frames);
case 'return'
play(1/play_fps)
case 'backspace'
play(5/play_fps)
end
end
%mouse handler
function button_down(src, event) %#ok, unused arguments
set(src,'Units','norm')
click_pos = get(src, 'CurrentPoint');
if click_pos(2) <= 0.03 %only trigger if the scrollbar was clicked
click = 1;
on_click([],[]);
end
end
function button_up(src, event) %#ok, unused arguments
click = 0;
end
function on_click(src, event) %#ok, unused arguments
if click == 0, return; end
%get x-coordinate of click
set(fig_handle, 'Units', 'normalized');
click_point = get(fig_handle, 'CurrentPoint');
set(fig_handle, 'Units', 'pixels');
x = click_point(1);
%get corresponding frame number
new_f = floor(1 + x * num_frames);
if new_f < 1 || new_f > num_frames, return; end %outside valid range
if new_f ~= f %don't redraw if the frame is the same (to prevent delays)
if new_f<num_frames
try
scroll(new_f);
catch
end
end
end
end
function play(period)
%toggle between stoping and starting the "play video" timer
if strcmp(get(play_timer,'Running'), 'off')
set(play_timer, 'Period', period);
start(play_timer);
else
stop(play_timer);
end
end
function play_timer_callback(src, event) %#ok
%executed at each timer period, when playing the video
if f < num_frames
scroll(f + 1);
elseif strcmp(get(play_timer,'Running'), 'on')
stop(play_timer); %stop the timer if the end is reached
end
end
function scroll(new_f)
if nargin == 1 %scroll to another position (new_f)
if new_f < 1 || new_f > num_frames
return
end
f = new_f;
end
%convert frame number to appropriate x-coordinate of scroll bar
scroll_x = (f - 1) / num_frames;
%move scroll bar to new position
set(scroll_handle, 'XData', scroll_x + [0 1 1 0] * scroll_bar_width);
%set to the right axes and call the custom redraw function
set(fig_handle, 'CurrentAxes', axes_handle);
h_fig.CData = read(v,f);
set (frametext,'String', ['Frame Nr.: ' int2str(f) '/' int2str(num_frames)])
%used to be "drawnow", but when called rapidly and the CPU is busy
%it didn't let Matlab process events properly (ie, close figure).
pause(0.001)
%drawnow limitrate
end
end
Walter Roberson
Walter Roberson el 8 de Dic. de 2020
Sometimes video_fig is a very convenient contribution for customizing !
William Thielicke
William Thielicke el 8 de Dic. de 2020
I uploaded the modification here, because I think this is useful for other people too (and I think the original videofig is less straight forward for people to use):
https://de.mathworks.com/matlabcentral/fileexchange/84028-preview-and-play-video-files

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing Toolbox en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by