-
Notifications
You must be signed in to change notification settings - Fork 1
/
UCutApplicationMP4Box.pas
291 lines (250 loc) · 8.58 KB
/
UCutApplicationMP4Box.pas
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
unit UCutApplicationMP4Box;
{$I Information.inc}
// basic review and reformatting: done
interface
uses
// Delphi
System.Classes, System.IniFiles, System.Contnrs, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Controls,
// Jedi
JvExStdCtrls, JvCheckBox,
// CA
UCutApplicationBase;
type
TCutApplicationMP4Box = class;
TfrmCutApplicationMP4Box = class(TfrmCutApplicationBase)
edtCommandLineOptions: TEdit;
lblCommandLineOptions: TLabel;
private
{ private declarations }
procedure SetCutApplication(const Value: TCutApplicationMP4Box);
function GetCutApplication: TCutApplicationMP4Box;
public
{ public declarations }
property CutApplication: TCutApplicationMP4Box read GetCutApplication write SetCutApplication;
procedure Init; override;
procedure Apply; override;
end;
TCutApplicationMP4Box = class(TCutApplicationBase)
protected
FSourceFile, FDestFile: string;
FFilePath: string;
FOriginalFileList, FTempFileList: TStringList;
FAddLastCommandTriggerIndex: Integer;
procedure CommandLineTerminate(Sender: TObject; const CommandLineIndex: Integer; const CommandLine: string); override;
public
CommandLineOptions: string;
constructor Create; override;
destructor Destroy; override;
function LoadSettings(IniFile: TCustomIniFile): Boolean; override;
function SaveSettings(IniFile: TCustomIniFile): Boolean; override;
function InfoString: string; override;
function WriteCutlistInfo(CutlistFile: TCustomIniFile; section: string): Boolean; override;
function PrepareCutting(SourceFileName: string; var DestFileName: string; Cutlist: TObjectList): Boolean; override;
function CleanUpAfterCutting: Boolean; override;
end;
var
frmCutApplicationMP4Box: TfrmCutApplicationMP4Box;
implementation
{$R *.dfm}
uses
// Delphi
System.SysUtils, System.StrUtils, System.Math,
// CA
UCutlist, CAResources, Utils;
const
MP4BOX_DEFAULT_EXENAME = 'MP4Box.exe';
{ TCutApplicationMP4Box }
constructor TCutApplicationMP4Box.Create;
begin
inherited;
RawRead := False;
FrameClass := TfrmCutApplicationMP4Box;
Name := 'MP4Box';
DefaultExeNames.Add(MP4BOX_DEFAULT_EXENAME);
RedirectOutput := True;
ShowAppWindow := False;
FOriginalFileList := TStringList.Create;
FTempFileList := TStringList.Create;
end;
function TCutApplicationMP4Box.LoadSettings(IniFile: TCustomIniFile): Boolean;
var
section: string;
success: Boolean;
begin
success := inherited LoadSettings(IniFile);
section := GetIniSectionName;
CommandLineOptions := IniFile.ReadString(section, 'CommandLineOptions', '');
Result := success;
end;
function TCutApplicationMP4Box.SaveSettings(IniFile: TCustomIniFile): Boolean;
var
section: string;
success: Boolean;
begin
success := inherited SaveSettings(IniFile);
section := GetIniSectionName;
IniFile.WriteString(section, 'CommandLineOptions', CommandLineOptions);
Result := success;
end;
function TCutApplicationMP4Box.PrepareCutting(SourceFileName: string; var DestFileName: string; Cutlist: TObjectList): Boolean;
const
ForcedFileExt = '.mp4';
var
TempCutlist: TCutlist;
iCut: Integer;
MustFreeTempCutlist: Boolean;
CommandLine: string;
SearchRec: TSearchRec;
begin
Result := inherited PrepareCutting(SourceFileName, DestFileName, Cutlist);
if Result then
begin
// Rename Files to MP4
// TempFileExt := ExtractFileExt(SourceFileName);
// ChangeFileExt(SourceFileName, ForcedFileExt);
// ChangeFileExt(DestFileName, ForcedFileExt);
ChDir(ExtractFileDir(SourceFileName));
FCommandLines.Clear;
MustFreeTempCutlist := False;
TempCutlist := TCutlist(Cutlist);
FSourceFile := SourceFileName;
FDestFile := DestFileName;
if TempCutlist.Mode <> clmTrim then
begin
TempCutlist := TempCutlist.Convert;
MustFreeTempCutlist := True;
end;
try
TempCutlist.Sort;
for iCut := 0 to Pred(tempCutlist.Count) do
begin
CommandLine := '"' + SourceFileName + '" ' + CommandLineOptions;
CommandLine := CommandLine + ' -splitx ' + FloatToStrInvariant(TempCutlist[iCut].pos_from) + ':' + FloatToStrInvariant(TempCutlist[iCut].pos_to);
FCommandLines.Add(CommandLine);
end;
//Workaround for not working -out parameter
//Add final Command after last Command of list is executed
FAddLastCommandTriggerIndex := Pred(FCommandLines.Count);
//
FFilePath := ExtractFilePath(SourceFileName);
//Make List of existing files
FOriginalFileList.Clear;
if FindFirst(FFilePath + '*.*', faAnyFile, SearchRec) = 0 then
begin
repeat
FOriginalFileList.Add(SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
Result := True;
finally
if MustFreeTempCutlist then
FreeAndNil(TempCutlist);
end;
end;
end;
function TCutApplicationMP4Box.InfoString: string;
begin
Result := Format(CAResources.RsCutAppInfoMP4Box, [inherited InfoString, CommandLineOptions]);
end;
function TCutApplicationMP4Box.WriteCutlistInfo(CutlistFile: TCustomIniFile; section: string): Boolean;
begin
Result := inherited WriteCutlistInfo(CutlistFile, section);
if Result then
begin
cutlistfile.WriteString(section, 'IntendedCutApplicationOptions', CommandLineOptions);
Result := True;
end;
end;
destructor TCutApplicationMP4Box.Destroy;
begin
FreeAndNil(FTempFileList);
FreeAndNil(FOriginalFileList);
inherited;
end;
function FileNameCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := Sign(Integer(List.Objects[Index1]) - Integer(List.Objects[Index2]));
end;
procedure TCutApplicationMP4Box.CommandLineTerminate(Sender: TObject; const CommandLineIndex: Integer; const CommandLine: string);
var
SearchRec: TSearchRec;
i: Integer;
NewCommandLine: string;
EndsSeconds: Integer;
posUnderscore, posDot: Integer;
begin
if CommandLineIndex = FAddLastCommandTriggerIndex then
begin
//Last Command Line Executed, now determine new files and add -cat command
//Make List of new files
FTempFileList.Clear;
if findFirst(FFilePath + '*.*', faAnyFile, SearchRec) = 0 then
begin
repeat
if FOriginalFileList.IndexOf(SearchRec.Name) < 0 then
begin
//get end Time in seconds from file Name
// Name_22.09.18_22-30_Station_140_TVOON_DE.mpg_6989.3140451_7993.9657083.mp4
EndsSeconds := 0;
posUnderscore := LastDelimiter('_', SearchRec.Name);
if PosUnderscore > 0 then
begin
posDot := Pos('.', SearchRec.Name, PosUnderscore);
if posDot < posUnderScore then posDot := Length(SearchRec.Name) + 1;
EndsSeconds := StrToIntDef(Copy(SearchRec.Name, PosUnderScore + 1, PosDot - (PosUnderScore + 1)), 0);
end;
FTempFileList.AddObject(SearchRec.Name, TObject(EndsSeconds));
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
//sort List
FTempFileList.CustomSort(FileNameCompare);
//Make CommandLine
NewCommandLine := CommandLineOptions;
for i := 0 to Pred(FTempFileList.Count) do
NewCommandLine := NewCommandLine + ' -cat "' { + FFilePath } + FTempFileList.Strings[i] + '"';
NewCommandLine := NewCommandLine + ' "' + FDestFile + '"';
FCommandLines.Add(NewCommandLine);
end;
end;
end;
function TCutApplicationMP4Box.CleanUpAfterCutting: Boolean;
var
i: Integer;
success: Boolean;
begin
if CleanUp then
begin
Result := inherited CleanUpAfterCutting;
for i := 0 to Pred(FTempFileList.Count) do
begin
if FileExists(FFilePath + FTempFileList.Strings[i]) then
begin
success := DeleteFile(FFilePath + FTempFileList.Strings[i]);
Result := Result and success;
end;
end;
end else
Result := False;
end;
{ TfrmCutApplicationMP4Box }
procedure TfrmCutApplicationMP4Box.Init;
begin
inherited;
edtCommandLineOptions.Text := CutApplication.CommandLineOptions;
end;
procedure TfrmCutApplicationMP4Box.Apply;
begin
inherited;
CutApplication.CommandLineOptions := edtCommandLIneOptions.Text;
end;
procedure TfrmCutApplicationMP4Box.SetCutApplication(const Value: TCutApplicationMP4Box);
begin
FCutApplication := Value;
end;
function TfrmCutApplicationMP4Box.GetCutApplication: TCutApplicationMP4Box;
begin
Result := FCutApplication as TCutApplicationMP4Box;
end;
end.