-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressorDaemon.m
86 lines (65 loc) · 3.14 KB
/
CompressorDaemon.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
classdef CompressorDaemon < handle
properties
WorkingDirectory;
ProcessedAVIs;
Timer;
end
methods
function self = CompressorDaemon(folder)
if nargin < 1
self.WorkingDirectory = pwd;
else
self.WorkingDirectory = folder;
end
self.ProcessedAVIs = {};
% TODO : start timer
end
function compressAVIs(self)
foldersToCheck = {self.WorkingDirectory};
while ~isempty(foldersToCheck)
folder = foldersToCheck{1};
foldersToCheck(1) = [];
cd(folder);
files = dir;
foldersToCheck = [foldersToCheck cellfun(@(s) [folder '\' s],{files([files.isdir] & ~strncmpi('.',{files.name},1)).name},'UniformOutput',false)]; %#ok<AGROW>
filenames = {files.name};
avis = filenames(~cellfun(@isempty,regexp(filenames,'\.avi$','once')));
for ii = 1:numel(avis)
tic;
avi = [folder '\' avis{ii}];
if any(strcmp(avi,self.ProcessedAVIs))
fprintf('INFO: Skipping already processed file %s\n',avi);
toc;
continue
end
[status,output] = system(['handle ' avi]);
if status ~= 0
if ii == numel(avis)
warnStr = 'this is the last file in the folder, so I''m going to skip in case it''s currently being written to.';
else
warnStr = 'this is not the last file so I''m assuming no-one has an open handle to it.';
end
warning('Unable to run handle.exe for file %s, %s\n',avi,warnStr);
if ii == numel(avis)
toc;
continue
end
elseif ~isempty(strfind(avi,output))
fprintf('INFO: File %s currently open in another process, skipping...\n',avi);
toc;
continue
end
[~,aviName] = fileparts(avi); % TODO : these variable names are crap
status = system(sprintf('avconv -i %s.avi -vcodec h264 -preset veryslow -crf 0 %s.mp4',aviName,aviName));
if status ~= 0
warning('avconv failed for file %s, check output for details.\n',avi);
toc;
continue
end
self.ProcessedAVIs{end+1} = avi;
toc;
end
end
end
end
end