Video Cutter Program in MATLAB
Video Cutter Program in MATLAB

Shocked a bit? Is it really posssible to make a cool video cutter program on your own? Yes, why not? If you know enough of MATLAB coding, it is probably nothing to you. Yes, once again I realized that nothing is there what MATLAB cannot do. It’s the most versatile software language pack I have ever seen. Probably you too.

First of all, you have to know, why do you need this. Suppose you do not have any video cutting software with you, or even you do not have internet connections to download a software, or maybe you have already exceeded your download limit for the running month and you cannot manage to get any software. So, don’t sit like idle. Why don’t you make something by yourself if you know the basic of MATLAB. Anyway to make a working video cutter, you have to know the basic things.

First of all you need to know in what Framerate the actual video was recorded. If you don’t find the answer, don’t worry, Maximum consumer cameras record movies in 30 fps. So you can take that the frame rate is 30 or around 30. Now you need to decide which portion of the video you need to cut. Measure the portion in secs. So, you can calculate exactly from which frame to which frame you need to cut.

Suppose you need to cut a part of the video which starts from 4 sec and ends to 8 sec out of 12 secs span. Now, if the the video was recorded in a 30 fps camera, you need to discard all 4*30=120 frames from the starting. Start from 121st frame and continue upto 8*30=240th frame. Again you need to discard the rest of the frames.

Now the initial calculation is done. Now you have to read the total video frame by frame. Append all the frames which are to be cut, one after one, all together. Save this thing. Well you are done. 🙂

Here I have done a basic program, in which I needed to discard the very first second of the video. So I just cut that portion and kept rest of it. I am giving the sample code. Make proper use of it. All the code lines are self explanatory. So I didn’t thought of including any comment after each of the line. Now if you still have any further problem of understating the code and need any kind of help, do not hesitate to write me. Will be uploading more MATLAB codes. All the best.

MATLAB CODE

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : MATLAB Video Cutter
% Author : Arindam Bose
% Version : 1.0
% Copyright : © Arindam Bose, All rights reserved.
% License : Open Source, Freeware
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vid = VideoReader('samplevideo.avi');
writerObj = VideoWriter('samplevideo1.avi');
writerObj.FrameRate = 30;
open(writerObj);
nFrames = vid.NumberOfFrames;
vidHeight = vid.Height;
vidWidth = vid.Width;
mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap', []);
for k = 31 : nFramesLeft // 31 because I discarded first 30 frames
mov(k).cdata = read(vid, k);
writeVideo(writerObj,mov(k).cdata);
end
hf = figure;
set(hf, 'position', [20 150 vidWidth vidHeight]);
movie(hf, mov, 1, vid.FrameRate);
close(writerObj);

Cool, Isn’t it?

Now, I think you are in a problem, probably you don’t know the fps of the camera, or the camera’s fps is not exactly 30. It may be a kinda fraction as I found it in a Ncon Camera to be 29.93 fps. Don’t worry, My next trick is coming up. You can even fix the fps of the camera by yourself. Try that.

About the author

Scientific History Blog Writer • Art enthusiast and Illustrator • Amateur Photographer • Biker and Hiker • Beer Enthusiast • Electrical Engineer • Chicago

Leave a Reply

Twitter Feed
%d bloggers like this: