forked from abc874/ca2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UCutApplicationAviDemux.pas
346 lines (298 loc) · 11 KB
/
UCutApplicationAviDemux.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
unit UCutApplicationAviDemux;
{$I Information.inc}
// basic review and reformatting: done
interface
uses
// Delphi
System.Classes, System.IniFiles, System.Contnrs, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Controls, Vcl.ExtCtrls, Vcl.Forms,
// Jedi
JvExStdCtrls, JvCheckBox,
// CA
UCutApplicationBase;
type
TCutApplicationAviDemux = class;
TfrmCutApplicationAviDemux = class(TfrmCutApplicationBase)
edtADCommandLineOptions: TEdit;
lblCommandLineOptions: TLabel;
cbADRebuildIndex: TJvCheckBox;
cbADScanVBR: TJvCheckBox;
cbADSmartCopy: TJvCheckBox;
cbADNoGUI: TJvCheckBox;
cbADAutoSave: TJvCheckBox;
cbADNotClose: TJvCheckBox;
private
{ private declarations }
procedure SetCutApplication(const Value: TCutApplicationAviDemux);
function GetCutApplication: TCutApplicationAviDemux;
public
{ public declarations }
property CutApplication: TCutApplicationAviDemux read GetCutApplication write SetCutApplication;
procedure Init; override;
procedure Apply; override;
end;
TCutApplicationAviDemux = class(TCutApplicationBase)
protected
FScriptFileName: string;
function CreateADScript(cutlist: TObjectList; Inputfile, Outputfile: string; var scriptfile: string): Boolean;
public
CommandLineOptions: string;
RebuildIndex,
ScanVBR,
SmartCopy,
NoGUI,
AutoSave,
NotClose: Boolean;
//TempDir: string;
constructor Create; 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
frmCutApplicationAviDemux: TfrmCutApplicationAviDemux;
implementation
{$R *.dfm}
{....$WARN UNIT_PLATFORM OFF}
uses
// Delphi
Winapi.Windows, System.SysUtils, System.StrUtils, Winapi.ActiveX,
// Jedi
JvCreateProcess,
// CA
CAResources, Main, Utils, UCutlist;
const
AVIDEMUX_DEFAULT_EXENAME = 'avidemux2.exe';
AVIDEMUX_DEFAULT_EXENAME_GTK = 'avidemux2_gtk.exe';
AVIDEMUX_DEFAULT_EXENAME_QT4 = 'avidemux2_qt4.exe';
AVIDEMUX_DEFAULT_EXENAME_CLI = 'avidemux2_cli.exe';
{ TCutApplicationAviDemux }
constructor TCutApplicationAviDemux.Create;
begin
inherited;
FrameClass := TfrmCutApplicationAviDemux;
Name := 'AviDemux';
DefaultExeNames.Add(AVIDEMUX_DEFAULT_EXENAME);
DefaultExeNames.Add(AVIDEMUX_DEFAULT_EXENAME_GTK);
DefaultExeNames.Add(AVIDEMUX_DEFAULT_EXENAME_QT4);
DefaultExeNames.Add(AVIDEMUX_DEFAULT_EXENAME_CLI);
RedirectOutput := False;
ShowAppWindow := True;
end;
function TCutApplicationAviDemux.LoadSettings(IniFile: TCustomIniFile): Boolean;
var
section: string;
success: Boolean;
begin
// This part only for compatibility issues for versions below 0.9.9
// This Setting may be overwritten below
TempDir := IniFile.ReadString('AviDemux', 'ScriptsPath', '');
success := inherited LoadSettings(IniFile);
section := GetIniSectionName;
CommandLineOptions := IniFile.ReadString(section, 'CommandLineOptions', CommandLineOptions);
NotClose := IniFile.ReadBool(section, 'NotClose', False);
AutoSave := IniFile.ReadBool(section, 'StartAndRun', True); //only for Compatibility to 0.9.7.x
AutoSave := IniFile.ReadBool(section, 'AutoSave', AutoSave);
RebuildIndex := IniFile.ReadBool(section, 'RebuildIndex', False);
ScanVBR := IniFile.ReadBool(section, 'ScanVBR', True);
SmartCopy := IniFile.ReadBool(section, 'SmartCopy', True);
NoGUI := IniFile.ReadBool(section, 'NoGUI', False);
Result := success;
end;
function TCutApplicationAviDemux.SaveSettings(IniFile: TCustomIniFile): Boolean;
var
section: string;
success: Boolean;
begin
success := inherited SaveSettings(IniFile);
section := GetIniSectionName;
IniFile.WriteString(section, 'CommandLineOptions', CommandLineOptions);
IniFile.WriteBool(section, 'RebuildIndex', RebuildIndex);
IniFile.WriteBool(section, 'ScanVBR', ScanVBR);
IniFile.WriteBool(section, 'AutoSave', AutoSave);
IniFile.WriteBool(section, 'NoGUI', NoGUI);
IniFile.WriteBool(section, 'NotClose', NotClose);
IniFile.WriteBool(section, 'SmartCopy', SmartCopy);
Result := success;
end;
function TCutApplicationAviDemux.PrepareCutting(SourceFileName: string; var DestFileName: string; Cutlist: TObjectList): Boolean;
var
TempCutlist: TCutlist;
MustFreeTempCutlist: Boolean;
CommandLine: string;
begin
Result := inherited PrepareCutting(SourceFileName, DestFileName, Cutlist);
if Result then
begin
FCommandLines.Clear;
MustFreeTempCutlist := False;
TempCutlist := (Cutlist as TCutlist);
if TempCutlist.Mode <> clmTrim then
begin
TempCutlist := TempCutlist.convert;
MustFreeTempCutlist := True;
end;
try
FScriptFileName := '';
if TempDir = '' then
begin
FScriptFileName := SourceFileName + '.avidemux';
end else begin
if not DirectoryExists(TempDir) then
begin
if YesNoWarnMsgFmt(CAResources.RsMsgCutAppTempDirMissing, [TempDir]) then
ForceDirectories(TempDir);
end;
if not DirectoryExists(TempDir) then
Exit;
FScriptFileName := IncludeTrailingPathDelimiter(TempDir) + ExtractFileName(SourceFileName) + '.avidemux';
end;
CreateADScript(TempCutlist, SourceFileName, DestFileName, FScriptFileName);
CommandLine := '';
if NoGUI then
CommandLine := '--nogui ';
CommandLine := CommandLine + '--run "' + FScriptFileName + '"';
if SmartCopy then
CommandLine := CommandLine + ' --force-smart ';
if AutoSave then
CommandLine := CommandLine + ' --save "' + DestFileName + '"';
if (not NotClose) and AutoSave then
CommandLine := CommandLine + ' --quit';
CommandLine := CommandLine + ' ' + CommandLineOptions;
FCommandLines.Add(CommandLine);
Result := True;
finally
if MustFreeTempCutlist then
FreeAndNil(TempCutlist);
end;
end;
end;
function TCutApplicationAviDemux.InfoString: string;
begin
Result := Format(CAResources.RsCutAppInfoAviDemux, [inherited InfoString, CommandLineOptions, BoolToStr(RebuildIndex, True), BoolToStr(ScanVBR, True), BoolToStr(SmartCopy, True)]);
end;
function TCutApplicationAviDemux.WriteCutlistInfo(CutlistFile: TCustomIniFile;
section: string): Boolean;
begin
Result := inherited WriteCutlistInfo(CutlistFile, section);
if Result then
begin
cutlistfile.WriteString(section, 'IntendedCutApplicationOptions', CommandLineOptions);
cutlistfile.WriteBool(section, 'AviDemuxRebuildIndex', RebuildIndex);
cutlistfile.WriteBool(section, 'AviDemuxScanVBR', ScanVBR);
cutlistfile.WriteBool(section, 'AviDemuxSmartCopy', SmartCopy);
end;
end;
function TCutApplicationAviDemux.CreateADScript(cutlist: TObjectList; Inputfile, Outputfile: string; var scriptfile: string): Boolean;
function EscapeString(s: string): string;
begin
Result := AnsiReplaceStr(s, '\', '\\');
Result := AnsiReplaceStr(Result, '''', '\''');
end;
var
f: Textfile;
i: Integer;
vdubStart, vdubLength: string;
cutlist_tmp: TCutlist;
begin
cutlist_tmp := cutlist as TCutlist;
if scriptfile = '' then
scriptfile := Inputfile + '.avidemux';
AssignFile(f, scriptfile);
Rewrite(f);
Writeln(f, '//AD <- Needed to identify//');
Writeln(f, '//--Generated by ' + Application_friendly_name);
Writeln(f, '');
Writeln(f, 'var app = new Avidemux();');
Writeln(f, '');
Writeln(f, '//** Video **');
Writeln(f, 'app.load("' + EscapeString(Inputfile) + '");');
Writeln(f, 'app.clearSegments();');
cutlist_tmp.sort;
for i := 0 to Pred(cutlist_tmp.Count) do
begin
if cutlist_tmp.FramesPresent and not cutlist_tmp.HasChanged then
begin
vdubstart := IntToStr(cutlist_tmp.Cut[i].frame_from);
vdubLength := IntToStr(cutlist_tmp.Cut[i].DurationFrames);
end else
begin
vdubstart := IntToStr(Round(cutlist_tmp.Cut[i].pos_from / MovieInfo.frame_duration));
vdubLength := IntToStr(Round((cutlist_tmp.Cut[i].pos_to - cutlist_tmp.Cut[i].pos_from) / MovieInfo.frame_duration + 1));
end;
Writeln(f, 'app.addSegment(0,' + vdubstart + ', ' + vdubLength + ');');
end;
if RebuildIndex then
Writeln(f, 'app.rebuildIndex();');
Writeln(f, '');
Writeln(f, '//** Postproc **');
Writeln(f, 'app.video.setPostProc(3,3,0);');
Writeln(f, 'app.video.codec("Copy","CQ=4","0 ");');
Writeln(f, '');
Writeln(f, '//** Audio **');
Writeln(f, 'app.audio.reset();');
Writeln(f, 'app.audio.codec("Copy",128);');
Writeln(f, 'app.audio.normalize=False;');
Writeln(f, 'app.audio.delay=0;');
if ScanVBR then
Writeln(f, 'app.audio.scanVBR();');
Writeln(f, '');
Writeln(f, 'app.setContainer("AVI");');
if SmartCopy then
Writeln(f, 'app.smartCopyMode();');
if AutoSave then
Writeln(f, 'setSuccess(app.save("' + EscapeString(Outputfile) + '"));')
else
Writeln(f, 'setSuccess(1);');
CloseFile(f);
Result := True;
end;
function TCutApplicationAviDemux.CleanUpAfterCutting: Boolean;
var
success: Boolean;
begin
if CleanUp then
begin
Result := inherited CleanUpAfterCutting;
if FileExists(FScriptFileName) then
begin
success := DeleteFile(FScriptFileName);
Result := Result and success;
end;
end else
Result := False;
end;
{ TfrmCutApplicationAviDemux }
procedure TfrmCutApplicationAviDemux.Init;
begin
inherited;
edtADCommandLineOptions.Text := CutApplication.CommandLineOptions;
CBADAutoSave.Checked := CutApplication.AutoSave;
CBADNotClose.Checked := CutApplication.NotClose;
CBADRebuildIndex.Checked := CutApplication.RebuildIndex;
CBADScanVBR.Checked := CutApplication.ScanVBR;
CBADSmartCopy.Checked := CutApplication.SmartCopy;
CBADNoGUI.Checked := CutApplication.NoGUI;
end;
procedure TfrmCutApplicationAviDemux.Apply;
begin
inherited;
CutApplication.CommandLineOptions := edtADCommandLineOptions.Text;
CutApplication.AutoSave := CBADAutoSave.Checked;
CutApplication.NotClose := CBADNotClose.Checked;
CutApplication.RebuildIndex := CBADRebuildIndex.Checked;
CutApplication.ScanVBR := CBADScanVBR.Checked;
CutApplication.SmartCopy := CBADSmartCopy.Checked;
CutApplication.NoGUI := CBADNoGUI.Checked;
end;
procedure TfrmCutApplicationAviDemux.SetCutApplication(const Value: TCutApplicationAviDemux);
begin
FCutApplication := Value;
end;
function TfrmCutApplicationAviDemux.GetCutApplication: TCutApplicationAviDemux;
begin
Result := (FCutApplication as TCutApplicationAviDemux);
end;
end.