-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressAllAVIs.m
66 lines (48 loc) · 1.88 KB
/
compressAllAVIs.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
59
60
61
62
63
64
65
66
function compressAllAVIs(isRecursive)
files = dir;
avis = files(arrayfun(@(f) strcmp(f.name(max(1,end-3):end),'.avi'),files));
for ii = 1:numel(avis)
fprintf('Copying AVI file %s to temporary file',avis(ii).name);
writer = VideoWriter('temp.avi'); %#ok<TNMLP>
reader = VideoReader(avis(ii).name); %#ok<TNMLP>
writer.FrameRate = reader.FrameRate;
open(writer);
while hasFrame(reader)
writeVideo(writer,readFrame(reader));
fprintf('.');
end
fprintf('\nFinished copying, deleting temporary file...\n');
close(writer);
clear reader; % this releases the file handle
try
delete(avis(ii).name);
fprintf('\nFinished deleting, moving temporary file to %s...\n',avis(ii).name);
movefile('temp.avi',avis(ii).name);
fprintf('Done!\n\n');
catch err
logMatlabError(err, 'Encountered error deleting original file and moving compressed copy:\n');
backupFile = sprintf('temp %s',avis(ii).name);
fprintf('Moving temporary file to %s\n',backupFile);
movefile('temp.avi',backupFile);
end
end
if nargin < 1 || ~isRecursive
return
end
dirs = files([files.isdir]);
for ii = 1:numel(dirs)
if strcmp(dirs(ii).name,'.') || strcmp(dirs(ii).name,'..')
continue
end
cd(dirs(ii).name);
try
fprintf('Entered folder %s\n\n',dirs(ii).name);
compressAllAVIs(true);
cd ..;
fprintf('Left folder %s\n\n',dirs(ii).name);
catch err
logMatlabError(err,sprintf('Encountered error compressing AVIs in dir %s\n',dirs(ii).name));
cd ..;
end
end
end