-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoCropper.m
58 lines (54 loc) · 1.92 KB
/
videoCropper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% VIDEO CROPPER -- video editing tools
%%% Spencer Seiler
%%% Miroculus Inc.
%%% 07-28-2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while 1
[fileName, path] = uigetfile('*.*', 'Select Video to Crop');
filenameSplit = strsplit(fileName,'.');
switch char(filenameSplit{2})
case 'avi'
fileType = 'Motion JPEG AVI';
break
case 'mj2'
fileType = 'Motion JPEG 2000';
break
case 'mp4'
fileType = 'MPEG-4';
break
case 'm4v'
fileType = 'MPEG-4';
break
otherwise
uiwait(msgbox({'Unsupported filetype' 'Select from (*.avi, *.mj2, *.mp4, *.m4v)'},'Error','error'))
continue
end
end
vid = VideoReader([path fileName]);
fig = figure('Name','Draw crop frame, keystoke to proceed');
vidFrame = readFrame(vid);
imagesc(vidFrame)
axis equal
axis off
cropRect = imrect;
pause
cropPos = int32(getPosition(cropRect)); % [xmin, ymin, width, height]
cropName = inputdlg('Cropped video filename:','Filename',1,cellstr(filenameSplit{1}));
close(fig)
icon = vidFrame(cropPos(2):cropPos(2)+cropPos(4),cropPos(1):cropPos(1)+cropPos(3),:);
cropMsgbox = msgbox({'Cropping in progress', 'Please wait.....'},'Notice','Custom',icon);
% cropPathName = strcat(path,cropName,'.',cellstr(filenameSplit{2}));
cropVid = VideoWriter([path char(cropName)],fileType);
cropVid.FrameRate = vid.FrameRate;
cropVid.Quality = 100;
open(cropVid);
vid.CurrentTime = 0;
while hasFrame(vid)
vidFrame = readFrame(vid);
vidFrame = vidFrame(cropPos(2):cropPos(2)+cropPos(4),cropPos(1):cropPos(1)+cropPos(3),:);
writeVideo(cropVid,vidFrame);
end
close(cropVid);
delete(cropMsgbox)
msgbox({'Cropping complete', 'Your video is finished'},'Notice','Custom',icon);