-
Notifications
You must be signed in to change notification settings - Fork 1
/
UCutlist.pas
1269 lines (1106 loc) · 38.7 KB
/
UCutlist.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit UCutlist;
{$I Information.inc}
// basic review and reformatting: done
interface
uses
// Delphi
System.Classes, System.Contnrs,
// CA
Settings_dialog, Movie, Utils, UCutApplicationBase;
const
CUTLIST_EXT = 'cutlist';
CUTLIST_EXTENSION = '.' + CUTLIST_EXT;
type
TCutList = class;
RCut = record
pos_from, pos_to: Double;
frame_from, frame_to: Integer;
end;
TCut = class
private
Fpos_from, Fpos_to: Double;
Fframe_from, Fframe_to: Integer;
public
constructor Create(pos_from, pos_to: Double); overload;
constructor Create(pos_from, pos_to: Double; frame_from, frame_to: Integer); overload;
property pos_from: Double read Fpos_from write Fpos_from;
property pos_to: Double read Fpos_to write Fpos_to;
property frame_from: Integer read Fframe_from write Fframe_from;
property frame_to: Integer read Fframe_to write Fframe_to;
function DurationFrames: Integer;
function GetData: RCut;
end;
TCutlistMode = (clmCutOut, clmTrim);
TCutlistSaveMode = (csmNeverAsk, csmAskBeforeOverwrite, csmAskWhenSavingFirstTime, csmAlwaysAsk);
TCutlistSearchType = (cstBySize, cstByName);
TCutlistSearchTypes = set of TCutlistSearchType;
TCutlistServerCommand = (cscRate, cscDelete, cscUpload);
TCutlistCallBackMethod = procedure(cutlist: TCutList) of object;
TCutList = class(TObjectList)
private
FSettings: TSettings;
FMovieInfo: TMovieInfo;
FIDOnServer: string;
FRatingOnServer: Double;
FHasChanged: Boolean;
FRefreshCallBack: TCutlistCallBackMethod;
FMode: TCutlistMode;
FSuggestedMovieName: string;
FFrameDuration, FFrameRate: Double;
FCutlistFile: TMemIniFileEx;
function GetCut(iCut: Integer): TCut;
procedure SetIDOnServer(const Value: string);
procedure FillCutPosArray(var CutPosArray: array of Double);
procedure SetMode(const Value: TCutlistMode);
procedure SetSuggestedMovieName(const Value: string);
function CutApplication: TCutApplicationBase;
procedure SetFrameDuration(d: Double);
procedure SetFrameRate(d: Double);
function SaveServerInfos(cutlistfile: TMemIniFileEx): Boolean;
procedure RemoveCutSections(cutlistfile: TMemIniFileEx);
function FindCutLinear(fPos: Double): Integer;
public
AppName, AppVersion: string;
ApplyToFile: string;
Comments: TStrings;
//Info
RatingByAuthor: Integer;
RatingByAuthorPresent: Boolean;
EPGError,
MissingBeginning,
MissingEnding,
MissingVideo,
MissingAudio,
OtherError: Boolean;
ActualContent,
OtherErrorDescription,
UserComment,
Author: string;
FramesPresent: Boolean;
SavedToFilename: string;
RatingSent: Integer;
OriginalFileSize: Int64;
RatingCountOnServer: Integer;
DownloadTime: Int64;
constructor Create(Settings: TSettings; MovieInfo: TMovieInfo);
destructor Destroy; override;
property FrameDuration: Double read FFrameDuration write SetFrameDuration;
property FrameRate: Double read FFrameRate write SetFrameRate;
property RefreshCallBack: TCutlistCallBackMethod read FRefreshCallBack write FRefreshCallBack;
procedure RefreshGUI;
property Cut[iCut: Integer]: TCut read GetCut; default;
function FindCutIndex(fPos: Double): Integer;
function FindCut(fPos: Double): TCut;
function AddCut(pos_from, pos_to: Double): Boolean;
function ReplaceCut(pos_from, pos_to: Double; CutToReplace: Integer): Boolean;
function SplitCut(pos_from, pos_to: Double): Boolean;
function DeleteCut(dCut: Integer): Boolean;
property Mode: TCutlistMode read FMode write SetMode;
property IDOnServer: string read FIDOnServer write SetIDOnServer;
property HasChanged: Boolean read FHasChanged;
function UserShouldSendRating: Boolean;
function CutCommand: string;
function cut_times_valid(var pos_from, pos_to: Double; do_not_test_cut: Integer; var interfering_cut: Integer): Boolean;
function FilenameSuggestion: string;
property SuggestedMovieName: string read FSuggestedMovieName write SetSuggestedMovieName;
function TotalDurationOfCuts: Double;
function NextCutPos(CurrentPos: Double): Double;
function PreviousCutPos(CurrentPos: Double): Double;
function clear_after_confirm: Boolean;
procedure Init;
procedure Sort;
function Convert: TCutList;
function LoadFromFile(FileName: string; noWarnings: Boolean): Boolean; overload;
function LoadFromFile(FileName: string): Boolean; overload;
function EditInfo: Boolean;
function Save(AskForPath: Boolean): Boolean; overload;
function SaveAs(FileName: string): Boolean;
function GetChecksum: Cardinal;
function LoadFrom(cutlistfile: TMemIniFileEx; noWarnings: Boolean): Boolean;
function StoreTo(cutlistfile: TMemIniFileEx): Boolean; overload;
property RatingOnServer: Double read FRatingOnServer write FRatingOnServer;
function AddServerInfos(FileName: string): Boolean;
end;
implementation
uses
// Delphi
Winapi.Windows, System.SysUtils, System.StrUtils, System.DateUtils, System.IniFiles, System.Math, System.UITypes,
Vcl.Forms, Vcl.Dialogs, Vcl.Controls,
// Jedi
JclMath,
// CA
CutlistInfo_dialog, UCutApplicationAsfbin, UCutApplicationMP4Box, CAResources, Main;
function CutlistCompareItems(Item1, Item2: Pointer): Integer;
begin
if Assigned(Item1) and Assigned(Item2) then
Result := CompareValue(TCut(Item1).pos_from, TCut(Item2).pos_from) // no overlap, compare from only
else
Result := CompareValue(Integer(Item1), Integer(Item2)); // Should be always Assigned?
end;
{ TCut }
constructor TCut.Create(pos_from, pos_to: Double);
begin
Create(pos_from, pos_to, 0, 0);
end;
constructor TCut.Create(pos_from, pos_to: Double; frame_from, frame_to: Integer);
begin
if pos_from > pos_to then
raise Exception.CreateFmt('Invalid Range: %f - %f', [pos_from, pos_to]);
if frame_from > frame_to then
raise Exception.CreateFmt('Invalid frame range: %d - %d', [frame_from, frame_to]);
Fpos_from := pos_from;
Fpos_to := pos_to;
Fframe_from := frame_from;
Fframe_to := frame_to;
end;
function TCut.DurationFrames: Integer;
begin
Result := frame_to - frame_from + 1;
end;
function TCut.GetData: RCut;
begin
Result.pos_from := Fpos_from;
Result.pos_to := Fpos_to;
Result.frame_from := Fframe_from;
Result.frame_to := Fframe_to;
end;
{ TCutList }
procedure TCutList.SetFrameDuration(d: Double);
begin
if d < 0 then
d := 0;
FFrameDuration := d;
if d > 0 then
FFrameRate := 1 / d
else
FFrameRate := 0;
end;
procedure TCutList.SetFrameRate(d: Double);
begin
if d < 0 then
d := 0;
FFrameRate := d;
if d > 0 then
FFrameDuration := 1 / d
else
FFrameDuration := 0;
end;
function TCutList.FindCutIndex(fPos: Double): Integer;
begin
Result := FindCutLinear(fPos);
end;
function TCutList.FindCut(fPos: Double): TCut;
var
idx: Integer;
begin
Result := nil;
idx := FindCutIndex(fPos);
if idx >= 0 then
Result := Cut[idx];
end;
function TCutList.FindCutLinear(fPos: Double): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to Pred(Count) do
with Cut[I] do
if (fPos >= pos_from) and (fPos <= pos_to) then
begin
Result := I;
Break;
end;
end;
function TCutList.AddCut(pos_from, pos_to: Double): Boolean;
var
icut: Integer;
begin
if cut_times_valid(pos_from, pos_to, -1, iCut) then
begin
Add(TCut.Create(pos_from, pos_to));
FHasChanged := True;
IDOnServer := '';
FramesPresent := False;
Sort;
RefreshGUI;
Result := True;
end else
Result := False;
end;
function TCutList.ReplaceCut(pos_from, pos_to: Double; CutToReplace: Integer): Boolean;
var
icut: Integer;
begin
if cut_times_valid(pos_from, pos_to, CutToReplace, iCut) then
begin
Cut[CutToReplace].pos_from := pos_from;
Cut[CutToReplace].pos_to := pos_to;
FHasChanged := True;
IDOnServer := '';
FramesPresent := False;
Sort;
RefreshGUI;
Result := True;
end else
Result := False;
end;
function TCutList.SplitCut(pos_from, pos_to: Double): Boolean;
var
LeftIndex, RightIndex: Integer;
LeftPos, RightPos: Double;
begin
Result := False;
if (pos_to > pos_from) and (pos_from <= FMovieInfo.current_file_duration) and (pos_to >= 0) then
begin
LeftIndex := FindCutIndex(pos_from);
RightIndex := FindCutIndex(pos_to);
if LeftIndex = RightIndex then
begin
if LeftIndex < 0 then
begin
Result := AddCut(pos_from, pos_to);
end else
begin
LeftPos := Cut[LeftIndex].pos_from;
RightPos := Cut[LeftIndex].pos_to;
Result := DeleteCut(LeftIndex) and AddCut(LeftPos, pos_from) and AddCut(pos_to, RightPos);
end;
end;
end;
end;
function TCutList.DeleteCut(dCut: Integer): Boolean;
begin
Delete(dCut);
FHasChanged := True;
IDOnServer := '';
Result := True;
RefreshGUI;
end;
function TCutList.clear_after_confirm: Boolean; // True if cleared, False if cancelled
begin
Result := True;
if not batchmode and HasChanged then
begin
case MessageDlg(RsTitleCutlistSaveChanges + #13#13 + RsMsgCutlistSaveChanges, mtConfirmation, mbYesNoCancel, 0, mbCancel) of
mrYes : Result := Save(False); // Can Clear if saved successfully
mrNo : Result := True;
else Result := False;
end;
end;
if Result then
Init;
end;
function TCutList.Convert: TCutList;
procedure AddCut(_pos_from, _pos_to: Double; _frame_from, _frame_to: Integer);
var
newCut: TCut;
begin
if _pos_from < _pos_to then
begin
if FramesPresent and (_frame_from <= _frame_to) then
begin
newCut := TCut.Create(_pos_from, _pos_to, _frame_from, _frame_to);
end else
begin
newCut := TCut.Create(_pos_from, _pos_to);
Result.FramesPresent := False;
end;
Result.Add(newCut);
end;
end;
var
iCut: Integer;
pos_Prev: Double;
Frame_prev: Integer;
curCut: TCut;
begin
Result := TCutList.Create(FSettings, FMovieInfo);
Result.FFrameRate := FFrameRate;
Result.FFrameDuration := FFrameDuration;
Result.FramesPresent := FramesPresent;
Result.SavedToFilename := SavedToFilename;
Result.Author := Author;
Result.RatingByAuthorPresent := RatingByAuthorPresent;
Result.RatingByAuthor := RatingByAuthor;
Result.EPGError := EPGError;
Result.ActualContent := ActualContent;
Result.MissingBeginning := MissingBeginning;
Result.MissingEnding := MissingEnding;
Result.MissingVideo := MissingVideo;
Result.MissingAudio := MissingAudio;
Result.OriginalFileSize := OriginalFileSize;
Result.OtherError := OtherError;
Result.OtherErrorDescription := OtherErrorDescription;
Result.SuggestedMovieName := SuggestedMovieName;
Result.UserComment := UserComment;
Result.FRatingOnServer := RatingOnServer;
Result.RatingCountOnServer := RatingCountOnServer;
Result.RatingSent := RatingSent;
Result.DownloadTime := DownloadTime;
Result.ApplyToFile := ApplyToFile;
if Mode = clmCutOut then
Result.FMode := clmTrim
else
Result.FMode := clmCutOut;
if Count > 0 then
begin
Sort;
pos_prev := 0;
Frame_prev := 0;
for iCut := 0 to Pred(Count) do
begin
curCut := Cut[iCut];
AddCut(pos_prev, curCut.pos_from - FrameDuration, frame_prev, curCut.frame_from - 1);
pos_prev := curCut.pos_to + FrameDuration;
frame_prev := curCut.frame_to + 1;
end;
//rest to end of file - this could be more accurate
AddCut(pos_prev, FMovieInfo.current_file_duration, frame_prev, Round(FMovieInfo.current_file_duration * FrameRate));
end;
Result.Sort;
Result.FHasChanged := HasChanged;
Result.IDOnServer := IDOnServer;
Result := Result;
end;
constructor TCutList.Create(Settings: TSettings; MovieInfo: TMovieInfo);
begin
inherited Create;
FSettings := Settings;
FMovieInfo := MovieInfo;
Comments := TStringList.Create;
FCutlistFile := TMemIniFileEx.Create('');
Init;
end;
destructor TCutList.Destroy;
begin
FreeAndNil(Comments);
FreeAndNil(FCutlistFile);
inherited;
end;
function TCutList.CutApplication: TCutApplicationBase;
begin
Result := FSettings.GetCutApplicationByMovieType(FMovieInfo.MovieType);
end;
function TCutList.CutCommand: string;
var
command: string;
iCut: Integer;
ConvertedCutlist: TCutList;
begin
if Count > 0 then
begin
if Mode = clmTrim then
begin
command := '';
Sort;
for iCut := 0 to Pred(Count) do
begin
command := command + ' -start ' + FMovieInfo.FormatPosition(Cut[iCut].pos_from);
command := command + ' -duration ' + FMovieInfo.FormatPosition(Cut[iCut].pos_to - Cut[iCut].pos_from);
end;
end else
begin
ConvertedCutlist := Convert;
command := ConvertedCutlist.CutCommand;
FreeAndNil(ConvertedCutlist);
end;
Result := command;
end else
begin
ErrMsg(RsMsgCutlistNoCutsDefined);
Result := '';
end;
end;
function TCutList.cut_times_valid(var pos_from, pos_to: Double; do_not_test_cut: Integer; var interfering_cut: Integer): Boolean;
var
icut: Integer;
begin
if (pos_to > pos_from) and (pos_from <= FMovieInfo.current_file_duration) and (pos_to >= 0) then
begin
if pos_from < 0 then
pos_from := 0;
if pos_to > FMovieInfo.current_file_duration then
pos_to := FMovieInfo.current_file_duration;
interfering_cut := -1;
for iCut := 0 to Pred(Count) do
begin
if iCut <> do_not_test_cut then
begin
if (pos_from < Cut[iCut].pos_to) and (pos_to > Cut[iCut].pos_from) then
begin
ErrMsgFmt(RsErrorCutlistCutOverlap, [icut]);
interfering_cut := icut;
Break;
end;
end;
end;
Result := interfering_cut < 0;
end else
Result := False;
end;
function TCutList.EditInfo: Boolean;
begin
Result := False;
if not FCutlistInfo.Visible then
begin
FCutlistInfo.original_movie_filename := FMovieInfo.current_filename;
FCutlistInfo.CBFramesPresent.Checked := (FramesPresent and not HasChanged);
FCutlistInfo.lblFrameRate.Caption := FrameRateToStr(FrameDuration, 'C');
if Author = '' then
FCutlistInfo.lblAuthor.Text := RsCaptionCutlistAuthorUnknown
else
FCutlistInfo.lblAuthor.Text := Format(RsCaptionCutlistAuthor, [Author]);
if RatingByAuthorPresent then
FCutlistInfo.RGRatingByAuthor.ItemIndex := RatingByAuthor
else
FCutlistInfo.RGRatingByAuthor.ItemIndex := -1;
FCutlistInfo.CBEPGError.Checked := EPGError;
if EPGError then
FCutlistInfo.edtActualContent.Text := ActualContent
else
FCutlistInfo.edtActualContent.Text := '';
FCutlistInfo.CBMissingBeginning.Checked := MissingBeginning;
FCutlistInfo.CBMissingEnding.Checked := MissingEnding;
FCutlistInfo.CBMissingVideo.Checked := MissingVideo;
FCutlistInfo.CBMissingAudio.Checked := MissingAudio;
FCutlistInfo.CBOtherError.Checked := OtherError;
if OtherError then
FCutlistInfo.edtOtherErrorDescription.Text := OtherErrorDescription
else
FCutlistInfo.edtOtherErrorDescription.Text := '';
FCutlistInfo.edtUserComment.Text := UserComment;
FCutlistInfo.edtMovieName.Text := SuggestedMovieName;
// Server information
FCutlistInfo.grpServerRating.Enabled := IDOnServer <> '';
if IDOnServer <> '' then
begin
FCutlistInfo.edtRatingOnServer.Text := IfThen(RatingOnServer < 0, '?', Format('%f', [RatingOnServer]));
FCutlistInfo.edtRatingCountOnServer.Text := IfThen(RatingCountOnServer < 0, '?', IntToStr(RatingCountOnServer));
FCutlistInfo.edtDownloadTime.Text := IfThen(DownloadTime <= 0, '?', FormatDateTime('', UnixToDateTime(DownloadTime)));
FCutlistInfo.edtRatingSent.Text := IfThen(RatingSent < 0, '', IntToStr(RatingSent));
end else
begin
FCutlistInfo.edtRatingOnServer.Text := '';
FCutlistInfo.edtRatingCountOnServer.Text := '';
FCutlistInfo.edtDownloadTime.Text := '';
FCutlistInfo.edtRatingSent.Text := '';
end;
if FCutlistInfo.ShowModal = mrOK then
begin
FHasChanged := True;
if FCutlistInfo.RGRatingByAuthor.ItemIndex = -1 then
begin
RatingByAuthorPresent := False;
Result := False;
end else
begin
RatingByAuthorPresent := True;
RatingByAuthor := FCutlistInfo.RGRatingByAuthor.ItemIndex;
Result := True;
end;
EPGError := FCutlistInfo.CBEPGError.Checked;
if EPGError then
ActualContent := FCutlistInfo.edtActualContent.Text
else
ActualContent := '';
MissingBeginning := FCutlistInfo.CBMissingBeginning.Checked;
MissingEnding := FCutlistInfo.CBMissingEnding.Checked;
MissingVideo := FCutlistInfo.CBMissingVideo.Checked;
MissingAudio := FCutlistInfo.CBMissingAudio.Checked;
OtherError := FCutlistInfo.CBOtherError.Checked;
if OtherError then
OtherErrorDescription := FCutlistInfo.edtOtherErrorDescription.Text
else
OtherErrorDescription := '';
UserComment := FCutlistInfo.edtUserComment.Text;
SuggestedMovieName := FCutlistInfo.edtMovieName.Text;
RefreshGUI;
end;
end;
end;
function TCutList.FilenameSuggestion: string;
begin
if FMovieInfo.current_filename <> '' then
Result := ExtractFileName(FMovieInfo.current_filename) + CUTLIST_EXTENSION
else
Result := 'Cutlist_01' + CUTLIST_EXTENSION;
end;
procedure TCutList.FillCutPosArray(var CutPosArray: array of Double);
var
iCut: Integer;
begin
Sort;
for iCut := 0 to Pred(Count) do
begin
CutPosArray[iCut * 2] := Cut[iCut].pos_from;
CutPosArray[iCut * 2 + 1] := Cut[iCut].pos_to;
end;
end;
function TCutList.NextCutPos(CurrentPos: Double): Double;
var
CutPosArray: array of Double;
iPos: Integer;
begin
Result := -1;
SetLength(CutPosArray, Count * 2);
FillCutPosArray(CutPosArray);
for iPos := 0 to Pred(2 * Count) do
begin
if CutPosArray[iPos] > CurrentPos then
begin
Result := CutPosArray[iPos];
Break;
end;
end;
end;
function TCutList.PreviousCutPos(CurrentPos: Double): Double;
var
CutPosArray: array of Double;
iPos: Integer;
begin
Result := -1;
SetLength(CutPosArray, Count * 2);
FillCutPosArray(CutPosArray);
for iPos := Pred(2 * Count) downto 0 do
begin
if CutPosArray[iPos] < CurrentPos then
begin
Result := CutPosArray[iPos];
Break;
end;
end;
end;
procedure TCutList.RefreshGUI;
begin
if Assigned(FRefreshCallBack) then
RefreshCallBack(Self);
end;
function TCutList.GetCut(iCut: Integer): TCut;
begin
Result := TCut(items[iCut]);
end;
function TCutList.GetChecksum: Cardinal;
var
s: TMemoryStream;
iCut: Integer;
procedure Write(const v : Integer); overload;
begin
s.Write(v, SizeOf(v));
end;
procedure Write(const v: RCut); overload;
begin
s.Write(v, SizeOf(v));
end;
procedure Write(const v: PChar; const l: Integer); overload;
begin
s.Write(v, l);
end;
procedure Write(const v: string); overload;
begin
Write(Length(v));
Write(PChar(v), Length(v));
end;
begin
s := TMemoryStream.Create();
try
Write(IDOnServer);
Write(OriginalFileSize);
Write(Author);
Write(Count);
for iCut := 0 to Pred(Count) do
Write(Cut[iCut].GetData);
s.Position := 0;
Result := Crc32_P(s.Memory, s.Size);
finally
FreeAndNil(s);
end;
end;
procedure TCutList.Init;
begin
Clear;
FCutlistFile.Clear;
AppName := Application_name;
AppVersion := Application_version;
ApplyToFile := '';
Comments.Text := RsCutlistInternalComment;
FFrameRate := 0;
FFrameDuration := 0;
FramesPresent := False;
SavedToFilename := '';
Author := Fsettings.UserName;
RatingByAuthorPresent := False;
RatingByAuthor := 3;
EPGError := False;
ActualContent := '';
MissingBeginning := False;
MissingEnding := False;
MissingVideo := False;
MissingAudio := False;
OtherError := False;
OtherErrorDescription := '';
SuggestedMovieName := '';
UserComment := '';
IDOnServer := '';
FRatingOnServer := -1;
RatingCountOnServer := -1;
RatingSent := -1;
OriginalFileSize := -1;
FHasChanged := False;
DownloadTime := 0;
RefreshGUI;
end;
function TCutList.LoadFromFile(FileName: string): Boolean;
begin
Result := LoadFromFile(FileName, batchmode);
end;
function TCutList.LoadFromFile(FileName: string; noWarnings: Boolean): Boolean;
var
cutlistfile: TMemIniFileEx;
begin
if FileExists(FileName) then
begin
cutlistfile := TMemIniFileEx.Create(FileName);
try
Result := LoadFrom(cutlistfile, noWarnings);
if Result then
SavedToFilename := FileName;
finally
cutlistfile.Free;
end;
end else
begin
Result := False;
if not noWarnings then
ErrMsgFmt(RsErrorFileNotFound, [FileName]);
end;
end;
function TCutList.LoadFrom(cutlistfile: TMemIniFileEx; noWarnings: Boolean): Boolean;
var
section, my_file, intended_options, intendedCutApp, intendedCutAppVersionStr, myCutApp, myOptions: string;
myCutAppVersionWords, intendedCutAppVersionWords: ARFileVersion;
iCUt, cCuts, ACut, iFramesDifference: Integer;
cut: TCut;
_pos_from, _pos_to: Double;
_frame_from, _frame_to: Integer;
CutAppAsfBin: TCutApplicationAsfbin;
cutChecksum: Cardinal;
begin
Result := False;
if Assigned(cutlistfile) and (noWarnings or clear_after_confirm) then
begin
Init;
if FCutlistFile <> cutlistfile then
FCutlistFile.LoadFromString(cutlistfile.GetDataString());
if cutlistfile.FileName <> '' then
SavedToFilename := cutlistfile.FileName;
section := 'General';
AppName := cutlistfile.ReadString(section, 'Application', '');
AppVersion := cutlistfile.ReadString(section, 'Version', '');
iniReadStrings(cutlistfile, section, 'comment', False, Comments);
ApplyToFile := cutlistfile.ReadString(section, 'ApplyToFile', Format('(%s)', [RsCutlistTargetUnknown]));
my_file := ExtractFileName(FMovieInfo.current_filename);
if AnsiSameText(ApplyToFile, my_file) or noWarnings or YesNoMsgFmt(RsMsgCutlistTargetMismatch, [ApplyToFile, my_file], Settings.NoOtherFileMsg) then
begin
OriginalFileSize := cutlistfile.ReadInt64(section, 'OriginalFileSizeBytes', -1);
// App + version
if CutApplication <> nil then
begin
intendedCutApp := cutlistfile.ReadString(section, 'IntendedCutApplication', '');
intendedCutAppVersionStr := cutlistfile.ReadString(section, 'IntendedCutApplicationVersion', '');
intendedCutAppVersionWords := Parse_File_Version(intendedCutAppVersionStr);
myCutApp := ExtractFileName(CutApplication.Path);
myCutAppVersionWords := Parse_File_Version(CutApplication.Version);
if (not noWarnings) then
begin
if not AnsiSameText(intendedCutApp, myCutApp) then
begin
if not YesNoMsgFmt(RsMsgCutlistCutAppMismatch, [IntendedCutApp, myCutApp], Settings.NoOtherProgMsg) then
Exit;
end else
if (myCutAppVersionWords[0] <> intendedCutAppVersionWords[0]) or (myCutAppVersionWords[1] <> intendedCutAppVersionWords[1]) or (myCutAppVersionWords[2] < intendedCutAppVersionWords[2]) then
begin
if not YesNoMsgFmt(RsMsgCutlistCutAppVerMismatch, [IntendedCutApp, intendedCutAppVersionStr, CutApplication.Version], Settings.NoOtherProgMsg) then
Exit;
end;
end;
// options for asfbin
if CutApplication is TCutApplicationAsfbin then
begin
CutAppAsfBin := CutApplication as TCutApplicationAsfbin;
myOptions := CutAppAsfBin.CommandLineOptions;
intended_options := cutlistfile.ReadString(section, 'IntendedCutApplicationOptions', myOptions);
if not AnsiSameText(intended_options, myOptions) then
begin
if noWarnings or YesNoMsgFmt(RsMsgCutlistAsfbinOptionMismatch, [intended_options, myOptions]) then
CutAppAsfBin.CommandLineOptions := intended_options;
end;
end;
end;
// Number of Cuts
cCuts := cutlistfile.ReadInteger(section, 'NoOfCuts', 0);
FrameRate := cutlistfile.ReadFloat(section, 'FramesPerSecond', 0);
if (FrameRate > 0) and (FMovieInfo.frame_duration > 0) then
begin
iFramesDifference := FMovieInfo.FrameCount - Trunc(FrameRate * FMovieInfo.current_file_duration);
if not noWarnings and (Abs(iFramesDifference) > 1) and not NoYesMsgFmt(RsMsgCutlistFrameRateMismatch, [FrameRate, 1 / FMovieInfo.frame_duration, Abs(iFramesDifference)]) then
FrameDuration := FMovieInfo.frame_duration;
end else
FrameDuration := FMovieInfo.frame_duration;
section := 'Server';
FRatingOnServer := cutlistfile.ReadFloat(section, 'Rating', -1);
RatingCountOnServer := cutlistfile.ReadInteger(section, 'RatingCount', -1);
DownloadTime := cutlistfile.ReadInteger(section, 'DownloadTime', 0);
RatingSent := cutlistfile.ReadInteger(section, 'RatingSent', -1);
IDOnServer := cutlistfile.ReadString(section, 'ID', '');
cutChecksum := StrToInt64Def(cutlistfile.ReadString(section, 'Checksum', ''), 0);
// info
section := 'Info';
Author := cutlistfile.ReadString(section, 'Author', '');
RatingByAuthor := cutlistfile.ReadInteger(section, 'RatingByAuthor', -1);
RatingByAuthorPresent := RatingByAuthor <> -1;
EPGError := cutlistfile.ReadBool(section, 'EPGError', False);
if EPGError then
ActualContent := cutlistfile.ReadString(section, 'ActualContent', '')
else
ActualContent := '';
MissingBeginning := cutlistfile.ReadBool(section, 'MissingBeginning', False);
MissingEnding := cutlistfile.ReadBool(section, 'MissingEnding', False);
MissingVideo := cutlistfile.ReadBool(section, 'MissingVideo', False);
MissingAudio := cutlistfile.ReadBool(section, 'MissingAudio', False);
OtherError := cutlistfile.ReadBool(section, 'OtherError', False);
if OtherError then
OtherErrorDescription := cutlistfile.ReadString(section, 'OtherErrorDescription', '')
else
OtherErrorDescription := '';
SuggestedMovieName := cutlistfile.ReadString(section, 'SuggestedMovieName', '');
UserComment := cutlistfile.ReadString(section, 'UserComment', '');
FramesPresent := True;
for iCut := 0 to Pred(cCuts) do
begin
section := 'Cut' + IntToStr(iCut);
_pos_from := cutlistfile.ReadFloat(section, 'Start', 0);
_pos_to := _pos_from + cutlistfile.ReadFloat(section, 'Duration', 0) - FrameDuration;
_Frame_from := cutlistfile.ReadInteger(section, 'StartFrame', -1);
_Frame_to := _frame_from + cutlistfile.ReadInteger(section, 'DurationFrames', -1) - 1;
if cut_times_valid(_pos_from, _pos_to, -1, aCut) then
begin
cut := TCut.Create(_pos_from, _pos_to);
if (_frame_from >= 0) and (_frame_to >= 0) then
begin
cut.frame_from := _Frame_from;
cut.frame_to := _frame_to;
end else
FramesPresent := False;
Add(cut);
end;
end;
if (cutCheckSum = 0) or (cutChecksum <> GetChecksum) then // Remove server and rating information, if changed
IDOnServer := '';
FMode := clmTrim;
FHasChanged := False;
Result := True;
if not noWarnings then
InfMsgFmt(RsMsgCutlistLoaded, [Count, cCuts], Settings.NoCutsLoadedMsg);
RefreshGUI;
end;
end;
end;
function TCutList.Save(AskForPath: Boolean): Boolean;
var
cutlist_path, target_file: string;
saveDlg: TSaveDialog;
begin
Result := False;
if Count > 0 then
begin
if SavedToFilename = '' then
begin
case FSettings.SaveCutlistMode of
smWithSource : cutlist_path := ExtractFilePath(FMovieInfo.current_filename); // with source
smGivenDir : cutlist_path := IncludeTrailingPathDelimiter(FSettings.CutlistSaveDir); // in given Dir
else cutlist_path := ExtractFilePath(FMovieInfo.current_filename); // with source
end;
target_file := cutlist_path + FilenameSuggestion;
end else
target_file := SavedToFilename;
if (SavedToFilename = '') or AskForPath then
begin
// Display Save Dialog?
AskForPath := AskForPath or FSettings.CutlistNameAlwaysConfirm;
if FileExists(target_File) and (not AskForPath) and NoYesWarnMsgFmt(RsWarnTargetExistsOverwrite, [target_file]) then
AskForPath := True;
if AskForPath then
begin
saveDlg := TSaveDialog.Create(Application.MainForm);
saveDlg.Filter := MakeFilterString(RsFilterDescriptionCutlists, '*' + CUTLIST_EXTENSION) + '|' + MakeFilterString(RsFilterDescriptionAll, '*.*');
saveDlg.FilterIndex := 1;
saveDlg.Title := RsSaveCutlistAs;
saveDlg.InitialDir := cutlist_path;
saveDlg.FileName := FilenameSuggestion;
saveDlg.DefaultExt := CUTLIST_EXT;
saveDlg.Options := saveDlg.Options + [ofOverwritePrompt, ofPathMustExist];
if saveDlg.Execute then
begin
target_file := saveDlg.FileName;
FreeAndNil(saveDlg);
end else
begin
FreeAndNil(saveDlg);
Exit;
end;
end;
cutlist_path := ExtractFilePath(target_file);
if not ForceDirectories(cutlist_path) then
begin
if not batchmode then
ErrMsgFmt(RsErrorCreatePathFailedAbort, [cutlist_path]);
Exit;
end;
if FileExists(target_File) then
begin
if not deletefile(target_file) then
begin
ErrMsgFmt(RsCouldNotDeleteFile, [target_file]);
Exit;
end;
end;
end;