Updated 24 Jul 2021
This script converts movies into gif file. This demo was created based on MATLAB official document.
What you can do in this script are
- Convert your movie files into GIF
isCropis set at 1, you can crop the region of interst to save. If you set isCrop=0, you can run this program without the
Image Processing Toolbox.
- The parameter
ratecontrols the number of frames used for the GIF files. Increase this value if you would like to decrease the GIF file. However, the number of frames used is limited, accordingly, the GIF quality might be decreased.
- imwrite: MATLAB official documentation: Write image to graphics file
movieName is the movie name to input. Other file types like
mov are also fine.
filename is gif name to save
If you would like to crop the movie, please set
isCrop at true.
DelayTime represents delay before displaying next image, in seconds
A value of
0 displays images as fast as your hardware allows.
The video frame is used for gif for every "
rate" frames. For example, if you set at 2, the second, forth, sixth, eighth, ... frames are saved as gif. Increasing this parameter decreases the file size of the gif.
video object to read frames from the movie
vidObj = VideoReader(movieName);
Please specify the area to crop if you want
if isCrop==1 frame = read(vidObj,round(vidObj.NumFrames/2)); figure;imshow(frame);title('please select the area to save') rect=getrect; % reset the video object vidObj = VideoReader(movieName); end
Loop over the movie to convert into gif
idx=1; while hasFrame(vidObj) im = readFrame(vidObj); if mod(idx,rate)==1 if isCrop==1 im=imcrop(im,rect); end [A,map] = rgb2ind(im,256); if idx == 1 imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',DelayTime) else imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',DelayTime); end end idx=idx+1; end
Kenta (2021). movie2gif (https://github.com/KentaItakura/movie2gif-using-MATLAB/releases/tag/1.0), GitHub. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!