-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.pas
2091 lines (1808 loc) · 86.9 KB
/
main.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
{ Minimalist Media Player
Copyright (C) 2021 Baz Cuda
https://github.com/BazzaCuda/MinimalistMediaPlayer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
}
unit main;
interface
uses
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Buttons,
System.Classes, WMPLib_TLB, Vcl.AppEvnts, WinApi.Messages, WinApi.Windows;
type
TMMPUI = class(TForm)
applicationEvents: TApplicationEvents;
lblAudioBitRate: TLabel;
lblBitRate: TLabel;
lblFileSize: TLabel;
lblFrameRate: TLabel;
lblInfo: TLabel;
lblTimeDisplay: TLabel;
lblVideoBitRate: TLabel;
lblXY: TLabel;
lblXY2: TLabel;
lblXYRatio: TLabel;
progressBar: TProgressBar;
tmrInfo: TTimer;
tmrMetaData: TTimer;
tmrPlayNext: TTimer;
tmrTimeDisplay: TTimer;
WMP: TWindowsMediaPlayer;
lblMediaCaption: TLabel;
tmrMediaCaption: TTimer;
tmrResize: TTimer;
procedure applicationEventsMessage(var Msg: tagMSG; var Handled: Boolean);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormResize(Sender: TObject);
procedure lblMuteUnmuteClick(Sender: TObject);
procedure progressBarMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure progressBarMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure tmrInfoTimer(Sender: TObject);
procedure tmrMetaDataTimer(Sender: TObject);
procedure tmrPlayNextTimer(Sender: TObject);
procedure tmrTimeDisplayTimer(Sender: TObject);
procedure WMPClick(ASender: TObject; nButton, nShiftState: SmallInt; fX, fY: Integer);
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
procedure WMPKeyDown(ASender: TObject; nKeyCode, nShiftState: SmallInt);
procedure WMPKeyUp(ASender: TObject; nKeyCode, nShiftState: SmallInt);
procedure WMPMouseDown(ASender: TObject; nButton, nShiftState: SmallInt; fX, fY: Integer);
procedure WMPMouseMove(ASender: TObject; nButton, nShiftState: SmallInt; fX, fY: Integer);
procedure WMPPlayStateChange(ASender: TObject; NewState: Integer);
procedure WMSysCommand(var Message : TWMSysCommand); Message WM_SYSCOMMAND;
procedure tmrMediaCaptionTimer(Sender: TObject);
procedure tmrResizeTimer(Sender: TObject);
private
protected
public
// UI Functions only - application logic is in TFX
function addMenuItem: boolean;
function fakeSystemMenu: boolean;
function hideLabels: boolean;
function positionWindow(winNum: WORD): boolean;
function posWinXY(x: integer; y: integer): boolean;
function repositionLabels: boolean;
function repositionTimeDisplay: boolean;
function repositionWMP: boolean;
function resizeWindow1: boolean;
function resizeWindow2: boolean;
function resizeWindow3(Shift: TShiftState): boolean;
function resizeWindow4(numApps: WORD): boolean;
function setupProgressBar: boolean;
function showAboutBox: boolean;
function showHelpWindow(create: boolean = TRUE): boolean;
function showHideHelp: boolean;
function showInfo(aInfo: string): boolean;
function showMediaCaption: boolean;
function showXY: boolean;
function toggleControls(Shift: TShiftState): boolean;
end;
var
UI: TMMPUI; // User Interface
implementation
uses
WinApi.CommCtrl, WinApi.uxTheme,
System.SysUtils, System.Generics.Collections, System.Math, System.Variants,
FormInputBox, MMSystem, Mixer, VCL.Graphics, clipbrd, System.IOUtils, ShellAPI, FormAbout, FormHelp, _debugWindow;
const
MENU_ABOUT_ID = 1001;
MENU_HELP_ID = 1002;
WIN_CLOSEAPP = 1103;
WIN_RESIZE = 1004;
WIN_POSITION = 1005;
WIN_CONTROLS = 1006;
WIN_RESTART = 1007;
WIN_TAB = 1008;
WIN_CAPTION = 1009;
WIN_PAUSE_PLAY = 1010;
type
TGV = class // Global [application-wide] Variables
strict private // force code to use the properties
FBlackOut: boolean;
FClosing: boolean;
FplayIx: integer;
Fplaylist: TList<string>;
FInputBox: boolean;
FmetaDataCount: integer;
FMute: boolean;
FnumApps: integer;
FnewMediaFile: boolean;
FPotPlayer: boolean;
FSampling: boolean;
FShowingHelp: boolean;
FStartUp: boolean;
FUserMoved: boolean;
FZoomed: boolean;
function getExePath: string;
function getAppBuildVersion: string;
function getAppReleaseVersion: string;
function getFileVersion(const aFilePath: string = ''; const fmt: string = '%d.%d.%d.%d'): string;
private
public
constructor create;
destructor destroy; override;
function invalidPlayIx: boolean;
property alwaysPlayWithPotPlayer: boolean read FPotPlayer write FPotPlayer;
property appBuildVersion: string read getAppBuildVersion;
property appReleaseVersion: string read getAppReleaseVersion;
property blackOut: boolean read FBlackOut write FBlackOut;
property closing: boolean read FClosing write FClosing;
property exePath: string read GetExePath;
property inputBox: boolean read FInputBox write FInputBox;
property mute: boolean read FMute write FMute;
property metaDataCount: integer read FmetaDataCount write FmetaDataCount;
property newMediaFile: boolean read FnewMediaFile write FnewMediaFile;
property numApps: integer read FnumApps write FnumApps;
property playIx: integer read FplayIx write FplayIx;
property playlist: TList<string> read Fplaylist;
property sampling: boolean read FSampling write FSampling;
property showingHelp: boolean read FShowingHelp write FShowingHelp;
property startup: boolean read FStartUp write FStartUp;
property userMoved: boolean read FUserMoved write FUserMoved;
property zoomed: boolean read FZoomed write FZoomed;
end;
TFX = class // application Functions aka program/business logic
private
function adjustAspectRatio: boolean;
function blackOut: boolean;
function checkScreenLimits: boolean;
function clearMediaMetaData: boolean;
function closeApp: boolean;
function clipboardCurrentFileName: boolean;
function currentFilePath: string;
function delay(dwMilliseconds:DWORD): boolean;
function deleteBookmark: boolean;
function deleteCurrentFile(Shift: TShiftState): boolean;
function deleteThisFile(AFilePath: string; Shift: TShiftState): boolean;
function doCentreWindow: boolean;
function doCommandLine(aCommandLIne: string): boolean;
function doMiniWindow: boolean;
function doMuteUnmute: boolean;
function doPausePlay: boolean;
function fetchMediaMetaData: boolean;
function findMediaFilesInFolder(aFilePath: string; aFileList: TList<string>; MinFileSize: int64 = 0): integer;
function formatSeconds(seconds: integer): string;
function fullScreen: boolean;
function getFileSize(const aFilePath: string): int64;
function getINIname: string;
function getScreenHeight: integer;
function getScreenWidth: integer;
function goDown: boolean;
function goLeft: boolean;
function goRight: boolean;
function goUp: boolean;
function greaterWindow(Shift: TShiftState): boolean;
function hasMediaFiles: boolean;
function hasMetaData: boolean;
function isAltKeyDown: boolean;
function isCapsLockOn: boolean;
function isControlKeyDown: boolean;
function isLastFile: boolean;
function isShiftKeyDown: boolean;
function isVideoOffscreen: boolean;
function keepCurrentFile: boolean;
function matchVideoWidth: boolean;
function noMediaFiles: boolean;
function openWithLosslessCut: boolean;
function openWithShotcut: boolean;
function playCurrentFile: boolean;
function playFirstFile: boolean;
function playLastFile: boolean;
function playNextFile: boolean;
function playPrevFile: boolean;
function playWithPotPlayer: boolean;
function rateReset: boolean;
function reloadMediaFiles: boolean;
function renameCurrentFile: boolean;
function resumeBookmark: boolean;
function sampleVideo: boolean;
function saveBookmark: boolean;
function sendToAll(cmd: WORD): boolean;
function ShowOKCancelMsgDlg(aMsg: string;
msgDlgType: TMsgDlgType = mtConfirmation;
msgDlgButtons: TMsgDlgButtons = MBOKCANCEL;
defButton: TMsgDlgBtn = MBCANCEL): TModalResult;
function speedDecrease(Shift: TShiftState): boolean;
function speedIncrease(Shift: TShiftState): boolean;
function startOver: boolean;
function tabForwardsBackwards(aFactor: integer = 0): boolean;
function UIKey(var Key: Word; Shift: TShiftState; KeyUp: boolean = FALSE): boolean;
function UIKeyDown(var Key: Word; Shift: TShiftState): boolean;
function UIKeyUp(var Key: Word; Shift: TShiftState): boolean;
function unZoom: boolean;
function updateRateLabel: boolean;
function updateTimeDisplay: boolean;
function updateVolumeDisplay: boolean;
function windowCaption: boolean;
function windowMaximizeRestore: boolean;
function WMPplay: boolean;
function zoomIn: boolean;
function zoomOut: boolean;
end;
var
FX: TFX; // Functions
GV: TGV; // Global Variables
{ TFX }
function TFX.adjustAspectRatio: boolean;
// [J] = ad[J]ust aspect ratio
// This attempts to resize the window height to match its width in the same ratio as the video dimensions,
// in order to eliminate the black bars above and below the video.
// Usage: size the window to the required width then press J to ad-J-ust the window's height to match the aspect ratio
// On other diplays, the magic numbers may need to be configured via an application INI file.
var
X, Y: integer;
vRatio: double;
vHeightTitle: integer;
vDelta: integer;
begin
case noMediaFiles of TRUE: EXIT; end;
X := UI.WMP.currentMedia.imageSourceWidth;
Y := UI.WMP.currentMedia.imageSourceHeight;
case (X = 0) OR (Y = 0) of TRUE: EXIT; end;
vRatio := Y / X;
UI.Height := trunc(UI.Width * vRatio);
checkScreenLimits;
UI.showHelpWindow(FALSE);
// debugInteger('X', X); debugInteger('Y', Y); debugFormat('vRatio: %f', [vRatio]);
// debugInteger('screen height', FX.getScreenHeight);
// debugInteger('screen width', FX.getScreenWidth);
// debugInteger('UI width', UI.width);
end;
function TFX.blackOut: boolean;
// [B] = [B]lackout i.e. Show/Hide Progress[B]ar
begin
GV.blackOut := NOT GV.blackOut;
UI.progressBar.Visible := NOT GV.blackOut;
UI.repositionWMP;
UI.repositionLabels;
end;
function TFX.clearMediaMetaData: boolean;
// palette cleanser :D
// performed each time a new video is played or unpaused
begin
UI.lblXY.Caption := format('XY:', []);
UI.lblXY2.Caption := format('XY:', []);
UI.lblFrameRate.Caption := format('FR:', []);
UI.lblBitRate.Caption := format('BR:', []);
UI.lblAudioBitRate.Caption := format('AR:', []);
UI.lblVideoBitRate.Caption := format('VR:', []);
UI.lblXYRatio.Caption := format('XY:', []);
UI.lblFileSize.Caption := format('FS:', []);
end;
function TFX.checkScreenLimits: boolean;
// checks whether the UI window is taller than the height of the screen and readjusts until it isn't.
// this can happen with some phone videos which were filmed in portrait, e.g. Vines and Tiktoks.
// Purely for completeness, we also check the width.
begin
var rect := screen.WorkAreaRect; // the screen minus the taskbar, which we assume is at the bottom of the desktop
case UI.Height > rect.bottom - rect.top of TRUE: begin
UI.width := trunc(UI.width * 0.90); // Yes, adjust the width!!...
adjustAspectRatio; // ...then let adjustAspectRatio do the rest
end;end; // adjustAspectRatio recalls checkScreenLimits
case UI.Width > rect.right - rect.left of TRUE: begin
UI.width := trunc(UI.width * 0.90);
adjustAspectRatio; // adjustAspectRatio recalls checkScreenLimits
end;end; // until both the width and height of the window fit the screen work area dimensions
var vR: TRect;
GetWindowRect(UI.Handle, vR);
case vR.bottom > rect.bottom of TRUE: UI.top := UI.top - (vR.bottom - rect.bottom); end; // is the bottom of the window below the taskbar? Move the window up.
case GV.userMoved of FALSE: doCentreWindow; end; // re-centre the window unless the user has deliberately positioned it somewhere
UI.showHelpWindow(FALSE);
end;
function TFX.clipboardCurrentFileName: boolean;
// [=] copy name of current video file (without the extension) to the clipboard
// This can be useful, before opening the file in ShotCut, for naming the edited video
begin
clipboard.AsText := TPath.GetFileNameWithoutExtension(currentFilePath);
end;
function TFX.closeApp: boolean;
begin
UI.tmrMetaData.Enabled := FALSE;
UI.WMP.controls.stop;
UI.CLOSE;
end;
function TFX.currentFilePath: string;
// returns the current file in the list
begin
case noMediaFiles of TRUE: EXIT; end;
result := GV.playlist[GV.playIx];
end;
function TFX.Delay(dwMilliseconds: DWORD): boolean;
// Used to delay an operation; "sleep()" would suspend the thread, which is not what is required
var
iStart, iStop: DWORD;
begin
iStart := GetTickCount;
repeat
iStop := GetTickCount;
Application.ProcessMessages;
until (iStop - iStart) >= dwMilliseconds;
end;
function TFX.deleteBookmark: boolean;
begin
DeleteFile(getINIname);
UI.showInfo('bookmark deleted');
end;
function TFX.deleteCurrentFile(Shift: TShiftState): boolean;
// [D] / DEL = [D]elete the current file
// Ctrl-D / Ctrl-DEL = Delete the entire contents of the current file's folder (doesn't touch subfolders)
begin
case noMediaFiles of TRUE: EXIT; end;
UI.WMP.controls.pause;
var vMsg := 'DELETE '#13#10#13#10'Folder: ' + ExtractFilePath(currentFilePath);
case ssCtrl in Shift of TRUE: vMsg := vMsg + '*.*';
FALSE: vMsg := vMsg + #13#10#13#10'File: ' + ExtractFileName(currentFilePath); end;
case showOkCancelMsgDlg(vMsg) = IDOK of
TRUE: begin
deleteThisFile(currentFilePath, Shift);
case isLastFile or (ssCtrl in Shift) of TRUE: begin UI.CLOSE; EXIT; end;end; // close app after deleting final file or deleting folder contents
GV.playlist.Delete(GV.playIx);
GV.playIx := GV.playIx - 1;
playNextFile;
end;
end;
end;
function TFX.deleteThisFile(AFilePath: string; Shift: TShiftState): boolean;
// performs (in a separate process) the actual file/folder deletion initiated by deleteCurrentFile
begin
case ssCtrl in Shift of TRUE: doCommandLine('rot -nobanner -p 1 -r "' + ExtractFilePath(AFilePath) + '*.* "'); // folder contents but not subfolders
FALSE: doCommandLine('rot -nobanner -p 1 -r "' + AFilePath + '"'); end; // one individual file
end;
function TFX.doCentreWindow: boolean;
// [H] = [H]orizontal
// Position the window centrally, both horizontally and vertically.
// Originally, the window was only positioned horizontally, hence [H].
// Later, this was changed to centre the window on the screen.
var
vR: TRect;
begin
GetWindowRect(UI.Handle, vR);
SetWindowPos(UI.Handle, 0, (getScreenWidth - (vR.Right - vR.Left)) div 2,
(getScreenHeight - (vR.Bottom - vR.Top)) div 2, 0, 0, SWP_NOZORDER + SWP_NOSIZE);
UI.showHelpWindow(FALSE);
GV.userMoved := FALSE;
end;
function TFX.doCommandLine(aCommandLIne: string): boolean;
// Create a cmd.exe process to execute any command line
// "Current Directory" defaults to the folder containing this application's executable.
var
vStartInfo: TStartupInfo;
vProcInfo: TProcessInformation;
begin
result := FALSE;
case trim(aCommandLIne) = '' of TRUE: EXIT; end;
FillChar(vStartInfo, SizeOf(TStartupInfo), #0);
FillChar(vProcInfo, SizeOf(TProcessInformation), #0);
vStartInfo.cb := SizeOf(TStartupInfo);
vStartInfo.wShowWindow := SW_HIDE;
vStartInfo.dwFlags := STARTF_USESHOWWINDOW;
var vCmd := 'c:\windows\system32\cmd.exe';
var vParams := '/c ' + aCommandLIne;
result := CreateProcess(PWideChar(vCmd), PWideChar(vParams), nil, nil, FALSE,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, PWideChar(GV.exePath), vStartInfo, vProcInfo);
end;
function TFX.doMiniWindow: boolean;
// [4] = Mini Window top right corner of screen, i.e. to one of the [4] corners
// Ctrl-[4] will maintain the current size of the window
var
vR: TRect;
begin
GetWindowRect(UI.Handle, vR);
case isControlKeyDown of FALSE: begin
SetWindowPos(UI.Handle, 0, (getScreenWidth - (vR.Right - vR.Left)) div 2,
(getScreenHeight - (vR.Bottom - vR.Top)) div 2, 400, 400, SWP_NOZORDER);
adjustAspectRatio;
// now reposition the window after having adjusted the size and the aspect ratio
end;end;
// Right - 18 to avoid the scrollbars of other [maximized] windows; Top + 30 to avoid the system icons of other [maximized] windows;.
SetWindowPos(UI.Handle, 0, (getScreenWidth - UI.Width - 18), (0 + 30), 0, 0, SWP_NOZORDER + SWP_NOSIZE);
GV.userMoved := TRUE;
end;
function TFX.doMuteUnmute: boolean;
// [E]ars = mute / unmute system sound
begin
GV.mute := NOT GV.mute;
g_mixer.muted := GV.mute;
end;
function TFX.doPausePlay: boolean;
// [SpaceBar] or double-click (or right-click) on video = Pause / Play
begin
case UI.WMP.playState of
wmppsPlaying: UI.WMP.controls.pause;
wmppsPaused,
wmppsStopped: WMPplay; end;
end;
function TFX.fetchMediaMetaData: boolean;
// Called from the tmrMetaDataTimer event handler.
// There is a delay after a video starts playing before its metadata becomes available.
// Consequently, when a video starts playing, tmrMetaData is started to delay an attempt to access it.
// For some very large media files, e.g. 7GB, WMP doesn't report a file size(!), so we do it ourselves.
begin
UI.lblXY.Caption := format('XY: %s x %s', [UI.WMP.currentMedia.getItemInfo('WM/VideoWidth'), UI.WMP.currentMedia.getItemInfo('WM/VideoHeight')]);
case trim(UI.lblXY.Caption) = 'XY: x' of TRUE: UI.lblXY.Caption := 'XY:'; end;
UI.lblXY2.Caption := format('XY: %d x %d', [UI.WMP.currentMedia.imageSourceWidth, UI.WMP.currentMedia.imageSourceHeight]);
try UI.lblFrameRate.Caption := format('FR: %f fps', [StrToFloat(UI.WMP.currentMedia.getItemInfo('FrameRate')) / 1000]); except end;
try UI.lblBitRate.Caption := format('BR: %d Kb/s', [trunc(StrToFloat(UI.WMP.currentMedia.getItemInfo('BitRate')) / 1024)]); except end;
try UI.lblAudioBitRate.Caption := format('AR: %d Kb/s', [trunc(StrToFloat(UI.WMP.currentMedia.getItemInfo('AudioBitRate')) / 1024)]); except end;
try UI.lblVideoBitRate.Caption := format('VR: %d Kb/s', [trunc(StrToFloat(UI.WMP.currentMedia.getItemInfo('VideoBitRate')) / 1024)]); except end;
try UI.lblXYRatio.Caption := format('XY: %s:%s', [UI.WMP.currentMedia.getItemInfo('PixelAspectRatioX'), UI.WMP.currentMedia.getItemInfo('PixelAspectRatioY')]); except end;
var vSize := getFileSize(currentFilePath);
case vSize >= 1052266987 of TRUE: try UI.lblFileSize.Caption := format('FS: %.3f GB', [vSize / 1024 / 1024 / 1024]); except end; // >= 0.98 of 1GB
FALSE: try UI.lblFileSize.Caption := format('FS: %d MB', [trunc(vSize / 1024 / 1024)]); except end;end;
end;
function TFX.findMediaFilesInFolder(aFilePath: string; aFileList: TList<string>; MinFileSize: int64 = 0): integer;
// Called from FormCreate and reloadVideoFiles.
// Standard Functionality: Clicking a video file type associated with this application causes MediaPlayer to run and play the video.
// All supported video file types in that video's folder will be added to aFileList (subfolders aren't processed).
// The function returns the aFileList index of the clicked video file.
// This list of supported file types was created manually after confirming they are playable by Windows Media Player.
// It sometimes has problems playing an flv video but, bizarrely, will play it if the file is renamed to, for example, .m4v
// PotPlayer has no such problems playing the same .flv video.
const EXTS_FILTER = '.wmv.mp4.avi.flv.mpg.mpeg.mkv.3gp.mov.m4v.vob.ts.webm.divx.m4a.mp3.wav.aac.m2ts.flac.mts.rm.asf';
var
sr: TSearchRec;
vFolderPath: string;
function isFileSizeOK: boolean;
// If a minimum file size has been stipulated by the caller, this filters out those that don't apply
begin
result := (MinFileSize <= 0) OR (AFilePath = vFolderPath + sr.Name) OR (GetFileSize(vFolderPath + sr.Name) >= MinFileSize);
end;
function isFileExtOK: boolean;
// Filter out all but the explicity-supported file types
begin
result := EXTS_FILTER.Contains(LowerCase(ExtractFileExt(sr.Name)));
end;
begin
result := -1;
case FileExists(AFilePath) of FALSE: EXIT; end;
aFileList.Clear;
aFileList.Sort;
vFolderPath := ExtractFilePath(AFilePath);
case FindFirst(vFolderPath + '*.*', faAnyFile, sr) = 0 of TRUE:
repeat
case (sr.Attr AND faDirectory) = faDirectory of FALSE:
case isFileSizeOK AND isFileExtOK of TRUE: aFileList.Add(vFolderPath + sr.Name); end;end;
until FindNext(sr) <> 0;
end;
FindClose(sr);
aFileList.Sort;
result := aFileList.IndexOf(aFilePath);
end;
function TFX.formatSeconds(seconds: integer): string;
begin
case seconds < 60 of TRUE: result := format('%ds', [seconds]);
FALSE: result := format('%d:%.2d', [seconds div 60, seconds mod 60]);
end;
end;
function TFX.fullScreen: boolean;
// [F] = Tell WMP to diplay fullScreen.
// The video timestamp and metadata displays etc. won't show until fullScreen mode is exited.
begin
case noMediaFiles of TRUE: EXIT; end;
case UI.WMP.fullScreen of TRUE: UI.WMP.fullScreen := FALSE;
FALSE: UI.WMP.fullScreen := TRUE; end;
end;
function TFX.getFileSize(const aFilePath: string): int64;
var
vHandle: THandle;
vRec: TWin32FindData;
begin
vHandle := FindFirstFile(PChar(aFilePath), vRec);
case vHandle <> INVALID_HANDLE_VALUE of TRUE: begin
WinAPI.Windows.FindClose(vHandle);
case (vRec.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 of TRUE:
result := (Int64(vRec.nFileSizeHigh) shl 32) + vRec.nFileSizeLow; end;end;end;
end;
function TFX.getINIname: string;
// A video timestamp bookmark can be saved to and retrieved from an INI file, named after the video file.
begin
result := ExtractFileName(currentFilePath);
result := ChangeFileExt(result, '.ini');
result := ExtractFilePath(currentFilePath) + result;
end;
function TFX.getScreenHeight: integer;
begin
var rect := screen.WorkAreaRect; // the screen minus the taskbar
result := rect.Bottom - rect.Top;
end;
function TFX.getScreenWidth: integer;
begin
result := GetSystemMetrics(SM_CXVIRTUALSCREEN); // we'll assume that the taskbar is in it's usual place at the bottom of the screen
end;
// When the video is zoomed in or out, the CTRL key plus the UP, DOWN, LEFT, RIGHT arrow keys can be used to reposition the video
// Bizarrely, if WMP is paused and repositioned, it will revert its position when playback is resumed;
// if WMP is repositioned during zoomed playback then paused, it will revert its position.
const MOVE_PIXELS = 10;
function TFX.goDown: boolean;
begin
UI.WMP.Top := UI.WMP.Top + MOVE_PIXELS;
end;
function TFX.goLeft: boolean;
begin
UI.WMP.Left := UI.WMP.Left - MOVE_PIXELS;
end;
function TFX.goRight: boolean;
begin
UI.WMP.Left := UI.WMP.left + MOVE_PIXELS;
end;
function TFX.goUp: boolean;
begin
UI.WMP.Top := UI.WMP.Top - MOVE_PIXELS;
end;
function TFX.hasMediaFiles: boolean;
begin
result := GV.playlist.Count > 0;
end;
function TFX.hasMetaData: boolean;
// is the media file's meta data ready to be accessed from WMP
begin
result := (UI.WMP.currentMedia.imageSourceWidth <> 0) AND (UI.WMP.currentMedia.imageSourceHeight <> 0);
end;
function TFX.isAltKeyDown: boolean;
// Did the user hold down the ALT key while pressing another key?
begin
result := (GetKeyState(VK_MENU) AND $80) <> 0;
end;
function TFX.isCapsLockOn: boolean;
// Is the CAPS LOCK key toggled on?
begin
result := GetKeyState(VK_CAPITAL) <> 0;
end;
function TFX.isControlKeyDown: boolean;
// Did the user hold down the CTRL key while pressing another key?
//
// see VCL.Forms.KeyboardStateToShiftState and KeyDataToShiftState
// If the high-order bit is 1, the key is down, otherwise it is up.
// If the low-order bit is 1, the key is toggled.
// A key, such as the CAPS LOCK key, is toggled if it is turned on.
// The key is off and untoggled if the low-order bit is 0.
// A toggled key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
// Check high-order bit of state...
begin
result := (GetKeyState(VK_CONTROL) AND $80) <> 0;
end;
function TFX.isLastFile: boolean;
// Is the current video file the last in the list?
begin
result := GV.playIx = GV.playlist.Count - 1;
end;
function TFX.isShiftKeyDown: boolean;
// Did the user hold down a SHIFT key while pressing another key?
begin
result := (GetKeyState(VK_SHIFT) AND $80) <> 0;
end;
function TFX.isVideoOffscreen: boolean;
// adjustAspectRatio and its call to checkScreenLimits ensures that the video will fit on the screen.
// isVideoOffScreen ensures that the entire video actually is on the screen.
var
vR: TRect;
begin
GetWindowRect(UI.handle, vR);
result := (vR.bottom > getScreenHeight) or (vR.right > getScreenWidth) or (vR.left < 0) or (vR.top < 0);
case result of FALSE: EXIT; end;
case (vR.left < 0) of TRUE: setWindowPos(UI.handle, 0, 0, UI.top, 0, 0, SWP_SHOWWINDOW + SWP_NOSIZE); end;
case (vR.top < 0) of TRUE: setWindowPos(UI.handle, 0, UI.left, 0, 0, 0, SWP_SHOWWINDOW + SWP_NOSIZE); end;
case (vR.bottom > getScreenHeight) of TRUE: begin var newTop := UI.top - (vR.bottom - getScreenHeight);
setWindowPos(UI.handle, 0, UI.left, newTop, 0, 0, SWP_SHOWWINDOW + SWP_NOSIZE); end;end;
case (vR.right > getScreenWidth) of TRUE: begin var newLeft := UI.left - (vR.right - getScreenWidth);
setWindowPos(UI.handle, 0, newLeft, UI.top, 0, 0, SWP_SHOWWINDOW + SWP_NOSIZE); end;end;
end;
function TFX.keepCurrentFile: boolean;
// [K] = [K]eep current file
// When examining a folder to determine which videos to keep or delete,
// this provides a convenient way to mark a video to be kept by renaming the file, prefixing an underscore to its filename.
// This causes all such files to gravitate to the top of the displayed folder thus making it easy to select all the other files and delete them.
// Occasionally, Windows or WMP will prevent the file from being renamed while WMP has it open.
// There is no apparent pattern to when the rename is allowed and when it isn't.
begin
case noMediaFiles of TRUE: EXIT; end;
UI.WMP.controls.pause;
delay(250); // give WMP time to register internally that the video has been paused (delay() doesn't "sleep()" the thread).
var vFileName := '_' + ExtractFileName(currentFilePath);
var vFilePath := ExtractFilePath(currentFilePath) + vFileName;
case RenameFile(currentFilePath, vFilePath) of FALSE: ShowMessage('Keep failed:' + #13#10 + SysErrorMessage(getlasterror));
TRUE: begin
GV.playlist[GV.playIx] {currentFilePath} := vFilePath; // reflect the new name in the list
UI.showInfo('Kept'); end;end; // reassure the user
windowCaption;
UI.WMP.controls.play;
end;
function TFX.matchVideoWidth: boolean;
// [9] = resize the width of the window to match the video width.
// Judicious use of [9], ad[J]ust, [H]orizontal and [G]reater can be used to obtain the optimum window to match the video.
// [4], [H], [G] can also be very useful.
begin
case noMediaFiles of TRUE: EXIT; end;
var X := UI.WMP.currentMedia.imageSourceWidth;
var Y := UI.WMP.currentMedia.imageSourceHeight;
UI.Width := X;
end;
function TFX.noMediaFiles: boolean;
begin
result := GV.playlist.Count = 0;
end;
function TFX.openWithLosslessCut: boolean;
// [F11] = open with LosslessCut
begin
UI.WMP.controls.pause;
ShellExecute(0, 'open', 'B:\Tools\LosslessCut-win-x64\LosslessCut.exe', pchar('"' + currentFilePath + '"'), '', SW_SHOW);
end;
function TFX.openWithShotcut: boolean;
// [F12] = open the video in the ShotCut* video editor
// mklink C:\ProgramFiles "C:\Program Files"
// The above command line allows C:\Program Files\ to be referenced in programs without the annoying space.
// This can simpifly things when multiple nested double quotes and apostrophes are being used to construct a command line
// At some point, the user's preferred video editor needs to be configurable via an application INI file.
// *https://shotcut.org/
// Amended to use ShellExecute. It handles the space in "Program Files" just fine, which the current implementation of doCommandLine doesn't.
begin
case noMediaFiles of TRUE: EXIT; end;
UI.WMP.controls.pause;
shellExecute(0, 'open', 'C:\Program Files\Shotcut\shotcut.exe', pchar('"' + currentFilePath + '"'), '', SW_SHOW);
end;
function TFX.playCurrentFile: boolean;
// CurrentFile is the one whose index in the playList equals playIx
var
media: IWMPMedia;
playlist: IWMPPlayList;
begin
case GV.invalidPlayIx of TRUE: EXIT; end; // sanity check
case FileExists(currentFilePath) of TRUE: begin // i.e. if file *still* exists :D
windowCaption;
UI.lblMediaCaption.Visible := TRUE;
// If the user switches several media files quickly in succession, we need to cancel the previous timer event and re-enable the timer,
// otherwise tmrMediaCaptionTimer will prematurely cancel the display of this media file's caption.
UI.tmrMediaCaption.Enabled := FALSE;
UI.tmrMediaCaption.Enabled := TRUE;
// Load subtitles
UI.WMP.closedCaption.captioningId := 'captions';
UI.WMP.closedCaption.SAMIFileName := 'file://B:\Movies\Let The Right One In (2008).smi';
UI.WMP.URL := 'file://' + currentFilePath;
// playlist := UI.WMP.currentPlaylist;
// media := UI.WMP.newMedia('B:\Movies\Let The Right One In (2008).srt');
// playlist.appendItem(Media);
// UI.WMP.currentPlaylist := playlist;
unZoom;
GV.newMediaFile := TRUE;
GV.metaDataCount := 0; // tmrMetaData will be enabled in WMPplay after playback commences
WMPplay;
case GV.alwaysPlayWithPotPlayer of TRUE: begin delay(3000); playWithPotPlayer; end;end;
end;end;
end;
function TFX.playFirstFile: boolean;
// [A] = play the first video in the list
begin
case hasMediaFiles of TRUE: begin
GV.playIx := 0;
playCurrentFile;
end;end;
end;
function TFX.playLastFile: boolean;
// [Z] = play the last video in the list
begin
case hasMediaFiles of TRUE: begin
GV.playIx := GV.playlist.Count - 1;
playCurrentFile;
end;end;
end;
function TFX.playNextFile: boolean;
// W = [W]atch the next video in the list
begin
case isLastFile of TRUE: begin UI.CLOSE; EXIT; end;end;
GV.playIx := GV.playIx + 1;
playCurrentFile;
end;
function TFX.playPrevFile: boolean;
// [Q] = play the previous video in the list
begin
case GV.playIx > 0 of TRUE: begin
GV.playIx := GV.playIx - 1;
playCurrentFile;
end;
end;
end;
function TFX.playWithPotPlayer: boolean;
// [P] = Play with [P]otPlayer
// At some point, the user's preferred alternative media player needs to be picked up from a .ini file
// Amended to use ShellExecute. It handles the space in "Program Files" just fine, which the current implementation of doCommandLine doesn't.
// Amended to use the default PotPlayer installation location
begin
UI.WMP.controls.pause;
ShellExecute(0, 'open', 'C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe', pchar('"' + currentFilePath + '"'), '', SW_SHOW);
end;
function TFX.rateReset: boolean;
// [1] = reset the playback rate to 100%
begin
UI.WMP.settings.rate := 1;
FX.updateRateLabel;
end;
function TFX.reloadMediaFiles: boolean;
// [L] = re[L]oad the list of video files from the current folder
// Previously, a facility existed whereby if MediaPlayer was launched with the CAPS LOCK key on,
// only video files greater than 100MB in size would be loaded into the file list.
// This allowed folders to be examined to quickly keep or delete the largest videos.
// This reloadMediaFiles function could than be used to re-find all files in the current folder regardless of size,
// without having to close and restart the app *without* the CAPS LOCK key on.
// Typically, this was actually because the user had launched MediaPlayer forgetting that the CAPS LOCK key was on!
// The CAPS LOCK key has now been repurposed for something else (see FormCreate) so for the time being this function isn't as useful as it once was - sic transit gloria mundi!
begin
GV.playIx := findMediaFilesInFolder(currentFilePath, GV.playlist);
windowCaption;
end;
function TFX.renameCurrentFile: boolean;
// [R] = [R]ename current file
// Occasionally, Windows or WMP will prevent the file from being renamed while WMP has it open.
// There is no apparent pattern to when the rename is allowed and when it isn't.
var
vOldFileName: string;
vExt: string;
s: string;
vNewFilePath: string;
begin
case noMediaFiles of TRUE: EXIT; end;
UI.WMP.controls.pause;
try
vOldFileName := ExtractFileName(currentFilePath);
vExt := ExtractFileExt(vOldFileName);
vOldFileName := copy(vOldFileName, 1, pos(vExt, vOldFileName) - 1); // strip the file extension; the user can edit the main part of the filename
GV.inputBox := TRUE; // ignore keystrokes. Let the InputBoxForm handle them
try
s := InputBoxForm(vOldFileName); // the form returns the edited filename or the original if the user pressed cancel
finally
GV.inputBox := FALSE;
end;
except
s := ''; // any funny business, force the rename to be abandoned
end;
case (s = '') OR (s = vOldFileName) of TRUE: EXIT; end; // nothing to do
vNewFilePath := ExtractFilePath(currentFilePath) + s + vExt; // construct the full path and new filename with the original extension
case RenameFile(currentFilePath, vNewFilePath) of FALSE: ShowMessage('Rename failed:' + #13#10 + SysErrorMessage(getlasterror));
TRUE: GV.playlist[GV.playIx] {currentFilePath} := vNewFilePath; end;
windowCaption; // update the caption with the new name
end;
function TFX.greaterWindow(Shift: TShiftState): boolean;
// [G]reater = increase size of window
// Ctrl-G = decrease size of window
begin
UI.resizeWindow3(Shift);
adjustAspectRatio;
isVideoOffscreen;
case GV.userMoved of FALSE: doCentreWindow; end;
UI.showHelpWindow(FALSE);
windowCaption;
UI.showXY;
end;
function TFX.resumeBookmark: boolean;
// [6] = read the saved video position from the INI file and continue playing from that position
begin
case noMediaFiles of TRUE: EXIT; end;
case FileExists(getINIname) of FALSE: begin UI.showInfo('no bookmark'); EXIT; end;end;
var sl := TStringList.Create;
sl.LoadFromFile(getINIname);
UI.WMP.controls.currentPosition := StrToFloat(sl[0]);
sl.Free;
UI.showInfo('resumed from bookmark');
end;
function TFX.sampleVideo: boolean;
// [Y] = sample/tr[Y]out the video by playing a few seconds then skipping 10% of the video
// This will stop once the current video position is more than 90% the way through the video
// If the next video is played, sampling will continue until Y is pressed again to cancel sampling
begin
case noMediaFiles of TRUE: EXIT; end;
case GV.sampling of TRUE: begin GV.sampling := FALSE; EXIT; end;end;
GV.sampling := TRUE;
try
repeat
UI.WMP.controls.currentPosition := UI.WMP.controls.currentPosition + (UI.WMP.currentMedia.duration / 10);
delay(3000); // let the video play for 3 seconds before skipping (delay() doesn't "sleep()" the thread).
until GV.Closing OR NOT GV.sampling OR (UI.WMP.controls.currentPosition >= (UI.wmp.currentMedia.duration * 0.90));
finally
end;
end;
function TFX.saveBookmark: boolean;
// [5] = save current video position to an ini file
begin
case noMediaFiles of TRUE: EXIT; end;
case FileExists(getINIname) of
TRUE: case MessageDlg('Do you want to overwrite the previous bookmark?', TMsgDlgType.mtConfirmation, [mbYes, mbNo], 0) = mrNo of TRUE: EXIT; end;end;
var sl := TStringList.Create;
sl.Add(FloatToStr(UI.WMP.controls.currentPosition));
sl.SaveToFile(getINIname);
sl.Free;
UI.showInfo('bookmarked');
end;
var handles: array of HWND;
function enumAllWindows(handle: HWND; lparam: LPARAM): BOOL; stdcall;
var
windowName : array[0..255] of char;
className : array[0..255] of char;
begin
case getClassName(handle, className, sizeOf(className)) > 0 of TRUE:
case strComp(className, 'TMMPUI') = 0 of TRUE: begin setLength(handles, length(handles) + 1);
handles[high(handles)] := handle; end;end;end;
result := TRUE;
end;
function TFX.sendToAll(cmd: WORD): boolean;
// send the user's command to all running instances of MinimalistMediaPlayer
var
i: integer;
begin
setLength(handles, 0);
enumWindows(@enumAllWindows, 0);
for i := low(handles) to high(handles) do begin
case cmd of
WIN_CLOSEAPP: sendMessage(handles[i], WM_SYSCOMMAND, WIN_CLOSEAPP, 0); // CTRL-0 = closeApp;
WIN_RESIZE: begin sendMessage(handles[i], WM_SYSCOMMAND, WIN_RESIZE, length(handles));// CTRL-9 = resize however many simultaneous windows there are
sendMessage(handles[i], WM_SYSCOMMAND, WIN_POSITION, i + 1); end; // then tell each window which number they are, e.g. 1-9
WIN_CONTROLS: sendMessage(handles[i], WM_SYSCOMMAND, WIN_CONTROLS, 0); // get each window to toggle full controls
WIN_RESTART: sendMessage(handles[i], WM_SYSCOMMAND, WIN_RESTART, 0); // get each window to restart their video
WIN_TAB: sendMessage(handles[i], WM_SYSCOMMAND, WIN_TAB, 0); // get each window to tab forwards
WIN_CAPTION: sendMessage(handles[i], WM_SYSCOMMAND, WIN_CAPTION, 0); // get each window to display caption
WIN_PAUSE_PLAY: sendMessage(handles[i], WM_SYSCOMMAND, WIN_PAUSE_PLAY, 0); // toggle pause/play in each window
end;end;
setLength(handles, 0);
end;
function TFX.speedDecrease(Shift: TShiftState): boolean;
// Ctrl-DownArrow = decrease playback speed by 10%
begin
case ssCtrl in Shift of FALSE: EXIT; end;
UI.WMP.settings.rate := UI.WMP.settings.rate - 0.1;
FX.updateRateLabel;
end;
function TFX.speedIncrease(Shift: TShiftState): boolean;
// Ctrl-UpArrow = increase playback speed by 10%
begin
case ssCtrl in Shift of FALSE: EXIT; end;