-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.pas
772 lines (706 loc) · 28.9 KB
/
base.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
unit base;
{$mode objfpc}{$H+}
{$MODESWITCH TYPEHELPERS}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics,
Dialogs, ActnList, ComCtrls, StdCtrls, Buttons, ExtCtrls, windows,
process, LazFileUtils, fpjson, dateutils, localizedforms,DefaultTranslator,
Grids, strutils, playerform
// FPC 3.0 fileinfo reads exe resources as long as you register the appropriate units
, Generics.Collections
, AnchorDocking
, main;
const
BUF_SIZE = 2048;
ffmpeg = 'ffmpeg.exe';
ffprobe = 'ffprobe.exe';
type
{ TTLabeledEditHelper }
TTLabeledEditHelper = class helper for TLabeledEdit
function toHHMMSS:String;
end;
{ TTJSONDataHelper }
TTJSONDataHelper = class helper for TJSONData
function FindPathDef(const APath: TJSONStringType; defaultValue:String = '"None"'):TJSONData;
end;
{ TFileChaptersItem}
TFileChaptersItem = specialize TObjectDictionary <String, TStringList >;
{ TFilesChapters}
TFilesChapters = specialize TObjectList<TFileChaptersItem>;
{ TSegmentInfoBackup }
TSegmentInfoBackup = class(TComponent) // backup leVelikostSegmentu and radGrSegment
public
segRadioGr : Byte;
segSize: String;
procedure BackupSegmentInfo();
procedure ClearBackup();
procedure RestoreBackup();
end;
{ TfrmBase }
TfrmBase = class(TLocalizedForm)
acAudioOriginal: TAction;
acAudioMp3: TAction;
acClearLog: TAction;
acQuitApplication: TAction;
acFlipForms: TAction;
acOpenFiles: TAction;
acVideoOriginal: TAction;
baseAkce: TActionList;
btnVideo: TButton;
btnAudioPuvodni: TButton;
btnAudioMP3: TButton;
btnSmazLog: TButton;
btnExit: TButton;
chcbPlaylist: TCheckBox;
chcbWithoutSplit: TCheckBox;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
GroupBox4: TGroupBox;
lblPocetSouboruCislo: TLabel;
lblPocetSouboru: TLabel;
leVelikostSegmentu: TLabeledEdit;
memLog: TMemo;
OpenDialog1: TOpenDialog;
prbUkazatel: TProgressBar;
radGrSegment: TRadioGroup;
SpeedButton1: TSpeedButton;
stgVlastnosti: TStringGrid;
procedure acAudioMp3Execute(Sender: TObject);
procedure acAudioOriginalExecute(Sender: TObject);
procedure acClearLogExecute(Sender: TObject);
procedure acFlipFormsExecute(Sender: TObject);
procedure acOpenFilesExecute(Sender: TObject);
procedure acQuitApplicationExecute(Sender: TObject);
procedure acVideoOriginalExecute(Sender: TObject);
procedure chcbWithoutSplitChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure leVelikostSegmentuEditingDone(Sender: TObject);
procedure runFFMPEG(exeFile,myParameters:String;progressBegin:Integer);
procedure FillGridFromFiles;
procedure stgVlastnostiAfterSelection(Sender: TObject; aCol, aRow: Integer);
procedure stgVlastnostiBeforeSelection(Sender: TObject; aCol, aRow: Integer
);
procedure stgVlastnostiCheckboxToggled(Sender: TObject; aCol,
aRow: Integer; aState: TCheckboxState);
private
public
filesChapters : TFilesChapters;
segInfoBck :TSegmentInfoBackup;
procedure HowToSplit(i:integer);
procedure HowToName(fileName:String;actualFile:Integer);
procedure UpdateTranslation(ALang: String); override;
end;
resourcestring
rsVelikost = 'Velikost';
rsZaTek = 'Začátek';
rsJmNo = 'Jméno';
rsKodek = 'Kodek';
rsChapters = 'Kapitoly';
rsUseChapters = 'Použít kapitoly';
rsBaseCaption = 'Akce';
rsFile = 'Soubor: ';
rsNoInternalCh = 'žádné interní kapitoly';
rsHas = 'nemá ';
var
frmBase: TfrmBase;
implementation
function pomSegment: String;
begin
case frmBase.radGrSegment.ItemIndex of
0 : Result := '-segment_time ' ;
1 : Result := '-segment_times ';
end;
end;
function pomsegmentlist(pomfile:string;i:integer):string;
begin
// ' , -segment_list out.m3u8'
if not(frmBase.chcbPlaylist.Checked) and not(frmBase.stgVlastnosti.Cells[3,frmBase.stgVlastnosti.Row] = '1')
and not(frmBase.filesChapters.Items[i]['user'].Count > 0) then
Result := ''
else
Result:= ' , -segment_list ' + AnsiQuotedStr(ExtractFilePath(pomFile)+
ExtractFileNameOnly(pomFile) + '.m3u8','"');
end;
{$R *.lfm}
{ TSegmentInfoBackup }
procedure TSegmentInfoBackup.BackupSegmentInfo();
begin
self.segRadioGr:= frmBase.radGrSegment.ItemIndex ;
//frmBase.memLog.Append(frmBase.radGrSegment.Items[frmBase.radGrSegment.ItemIndex]);
self.segSize:= frmBase.leVelikostSegmentu.Caption;
//frmBase.memLog.Append(self.segSize);
end;
procedure TSegmentInfoBackup.ClearBackup();
begin
self.segRadioGr:=0;
self.segSize:='';
end;
procedure TSegmentInfoBackup.RestoreBackup;
begin
frmBase.radGrSegment.ItemIndex := self.segRadioGr;
frmBase.leVelikostSegmentu.Caption := self.segSize;
end;
{ TTJSONDataHelper }
function TTJSONDataHelper.FindPathDef(const APath: TJSONStringType;
defaultValue: String = '"None"' ): TJSONData;
var
pomData: TJSONData;
begin
pomData := self.FindPath(APath);
if pomData = Nil then
Result := GetJSON(defaultValue)
else
Result := pomData;
end;
{ TTLabeledEditHelper }
function TTLabeledEditHelper.toHHMMSS: String;
var
pomI, i: Integer;
pomS: String;
begin
Result := '';
pomI:= 0;
if not(self.Text='') then
pomI := WordCount(self.Text,[',']);
begin
if pomI = 0 then
// ffmpeg option segment_time X - cuts file to segments with the same length X
Result:=TimeToStr(incminute( 0,strtoint(self.Text)))
else
begin
// ffmpeg option segment_times X,Y,Z - cuts file in time X, Y, Z see: time vs times :-)
for i:=1 to pomI do
begin
pomS := ExtractWord(i,self.Text,[',']);
if RPos(':', pomS) > 0 then
begin
Result := Result + ',' + pomS;
end
else
begin
Result := Result + ',' + TimeToStr(incminute( 0,strtoint(pomS)));
end;
end;
// must be used double quotes "00:10:00,00:30:00" due to command line parameters separated
// already with commas see:
// http://www.ffmpeg-archive.org/Alternative-options-for-comma-separated-filters-td4666298.html
// and https://stackoverflow.com/questions/5230166/how-can-i-launch-a-folder-whose-name-contains-a-comma-using-processstartinfo-in/5230195#5230195
Result:= TrimSet(Result,[',']);
Result:= '"' + Result + '"';
end;
// great document about TDateTime see:
// https://www.freepascal.org/~michael/articles/datetime/datetime.pdf
end;
end;
{ TfrmBase }
procedure TfrmBase.leVelikostSegmentuEditingDone(Sender: TObject);
begin
if (radGrSegment.ItemIndex = 0) and AnsiContainsStr(leVelikostSegmentu.text,',') then
begin
ShowMessage('Only first value is accepted!!!');
leVelikostSegmentu.Text:= ExtractWord(1,leVelikostSegmentu.Text,[',']);
end;
end;
procedure TfrmBase.FillGridFromFiles;
var
jData:TJSONData;
jObject:TJSONObject;
pomJData:TJSONData;
i,j: Integer;
progressMax:Integer;
streamsInf : String;
internalChapters: TStringList;
internalChapterNames: TStringList; // file names for splitting according to the internal chapters
codecInfo: TStringList;
fileChaptersItem : TFileChaptersItem;
userChapters :TStringList;
userChapterNames : TStringList;
chaptersCount : Word;
codecName, codecType:String;
begin
progressMax:=0;
stgVlastnosti.ClearRows; // clear all rows (fixed too)
stgVlastnosti.RowCount:=1; // rows are counted from 0
stgVlastnosti.FixedRows:=1;
lblPocetSouboruCislo.Caption := IntToStr(OpenDialog1.Files.Count);
filesChapters.Clear;
for i:=0 to OpenDialog1.Files.Count-1 do
begin
memLog.Clear;
runFFMPEG(ffprobe,'-v quiet, -print_format json, -show_format, -show_streams, -show_chapters '+
AnsiQuotedStr(OpenDialog1.Files[i],'"'),0);
// capture json ffmpeg output from memLog component
jData:=GetJSON(memLog.Text); // !!! object created it needs to be freed in the end :-)
jObject:=TJSONObject(jData);
userChapters := TStringList.Create(True);
userChapterNames := TStringList.Create(True);
internalChapters := TStringList.Create(True);
internalChapterNames := TStringList.Create(True);
chaptersCount := jObject.FindPath('chapters').Count;
if chaptersCount <> 0 then
// file has internal chapters
begin
chaptersCount := jObject.FindPath('chapters').Count;
for j:=0 to chaptersCount-1 do
begin
pomJData := jObject.FindPath('chapters').Items[j] ;
internalChapters.Add(
FormatDateTime('hh:nnn:ss', pomJData.FindPathDef('start_time').AsFloat / (24 * 60 * 60)) );
internalChapterNames.Add( Format('%.2d - %s', [j,pomJData.FindPathDef('tags.title').AsString]) );
end;
internalChapters.Delimiter:= ',';
internalChapterNames.Delimiter:=',';
//memLog.Append(internalChapters.DelimitedText);
// memLog.Append(internalChapterNames.DelimitedText);
//leVelikostSegmentu.Caption:= internalChapters.DelimitedText;
end;
streamsInf:= '';
for j:=0 to jObject.FindPath('streams').Count-1 do
begin
codecType := jObject.FindPath('streams').Items[j].FindPathDef('codec_type').AsString;
codecName := jObject.FindPath('streams').Items[j].FindPathDef('codec_name').AsString;
streamsInf:= streamsInf + codecType + ':' + codecName + ' ';
end;
progressMax:=progressMax+round(jObject.FindPath('format.duration').AsFloat);
stgVlastnosti.InsertRowWithValues( i+1,
[ ExtractFileName(jObject.FindPath('format.filename').AsString),
streamsInf,IntToStr(chaptersCount),'false' ] );
jData.Free; // Object cleaned after 5 min debugging :-)
// audio codec extention needed for original audio extracting
codecInfo := TStringList.Create(True);
codecInfo.NameValueSeparator := ':';
codecInfo.Delimiter := ' ';
codecInfo.StrictDelimiter := True;
codecInfo.DelimitedText := streamsInf;
// add dictionaries with TStringList items to fileChapters list
userChapters.Delimiter:= ',';
fileChaptersItem := TFileChaptersItem.Create([doOwnsValues]);
fileChaptersItem.add('internal',internalChapters);
fileChaptersItem.add('internalNames',internalChapterNames);
fileChaptersItem.add('user',userChapters);
fileChaptersItem.add('userNames',userChapterNames);
fileChaptersItem.add('codecInfo',codecInfo);
filesChapters.Add(fileChaptersItem);
//memLog.Append('User chapters count: '+IntToStr(userChapters.Count));
//memLog.Append('Internal chapters count: '+IntToStr(internalChapters.Count));
//memLog.Append(IntToStr(filesChapters[i]['internal'].Count));
//memlog.Append(intTostr(filesChapters.Items[i]['user'].Count)) ;
// memLog.Append('TADY TO JE ..... ' + codecInfo.Values['audio']);
//internalChapters.Free; - TObjectDictionary manages memory of its items automatically
end;
prbUkazatel.Max:=progressMax;
stgVlastnosti.AutoSizeColumns();
// fill player form from 1st line of stgVlastnosti - player caption
frmPlayer.Caption:= stgVlastnosti.Cells[0,stgVlastnosti.Row];
// file to play
// frmPlayer.mpvPlayer.FileName:= OpenDialog1.Files[stgVlastnosti.Row-1];
frmPlayer.mpvPlayer.Play(OpenDialog1.Files[stgVlastnosti.Row-1]);
// user chapters if any
// frmPlayer.stgTimePoints.Items.AddStrings(filesChapters[stgVlastnosti.Row-1]['user']);
frmPlayer.stgTimePoints.Cols[0].AddStrings(filesChapters[stgVlastnosti.Row-1]['user']);
// values will be time points
radGrSegment.ItemIndex := 1;
// enable btnImportChapters action if possible
if filesChapters[stgVlastnosti.Row-1]['internal'].Count > 0 then
frmPlayer.acImportChapters.Enabled:=True
else
frmPlayer.acImportChapters.Enabled:=False;
end;
procedure TfrmBase.HowToSplit(i: integer);
var
pomStringList : TStringList;
begin
// when string grid position was not changed by user, user chapters dicts needs update!
if stgVlastnosti.Row-1 = i then
begin
filesChapters[stgVlastnosti.Row-1]['user'].clear;
filesChapters[stgVlastnosti.Row-1]['user'].AddStrings(frmPlayer.stgTimePoints.Cols[0]);
//memLog.Append(format('for row: %d added chapters user chapters from player',[i]));
filesChapters[stgVlastnosti.Row-1]['userNames'].clear;
filesChapters[stgVlastnosti.Row-1]['userNames'].AddStrings(frmPlayer.stgTimePoints.Cols[1]);
//memLog.Append(format('for row: %d added user chapters names from player',[i]));
end;
// file has user defined chapters - by movie player
if filesChapters.Items[i]['user'].Count > 0 then
begin
// remove time code 00:00:00 for the first part ! :-)
pomStringList := TStringList.Create();
filesChapters.Items[i]['user'].Slice(1,pomStringList);
leVelikostSegmentu.Caption:= pomStringList.DelimitedText;
radGrSegment.ItemIndex := 1 ;
FreeAndNil(pomStringList);
end
// file has internal chapters and using them is checked in 4th grid column
else if (filesChapters.Items[i]['internal'].count > 0) and (stgVlastnosti.Cells[3,i+1] = '1') then
begin
pomStringList := TStringList.Create();
filesChapters.Items[i]['internal'].Slice(1,pomStringList);
leVelikostSegmentu.Caption:= pomStringList.DelimitedText;
radGrSegment.ItemIndex := 1 ;
FreeAndNil(pomStringList);
end
else
// use no chapter but split setting from leVelikostSegmentu and radGrSegment
begin
segInfoBck.RestoreBackup();
end;
end;
procedure TfrmBase.HowToName(fileName: String; actualFile:Integer);
var
partsFromM3U8: TStringList;
i:Byte;
oldName, newName, filePath, fileExt: String;
whichNames:String;
j: Integer;
begin
// apply the same logic as in HowToSplit()
if filesChapters.Items[actualFile]['user'].Count > 0 then
whichNames := 'userNames'
else if (filesChapters.Items[actualFile]['internal'].count > 0) and (stgVlastnosti.Cells[3,actualFile+1] = '1') then
whichNames := 'internalNames'
else
exit; // renamig is not needed
memLog.Append('Parts renaming ... ');
partsFromM3U8 := TStringList.Create();
filePath := ExtractFilePath(fileName);
// LoadFromFile() does not need AnsiQuatedStr()
partsFromM3U8.LoadFromFile(filePath + ExtractFileNameOnly(fileName) + '.m3u8');
j:=0; // index for file names in m3u8
for i:=0 to partsFromM3U8.count - 1 do
if partsFromM3U8[i].StartsWith('#') then
Continue
else
begin
oldName := filePath + partsFromM3U8[i];
fileExt := ExtractFileExt(partsFromM3U8[i]);
// memLog.Append(oldName);
newName := filePath + filesChapters.Items[actualFile][whichNames][j] + fileExt;
// memLog.Append(newName);
RenameFileUTF8(oldName,newName);
partsFromM3U8[i] := filesChapters.Items[actualFile][whichNames][j] + fileExt;
j:= j + 1;
Application.ProcessMessages;
end;
memLog.Append('Je to ? :-) ... '+partsFromM3U8.Text);
// propert Text includes all items separated by line ending
// delete m3u8 playlist if not requested by user
if not(frmBase.chcbPlaylist.Checked) then
DeleteFileUTF8(filePath + ExtractFileNameOnly(fileName) + '.m3u8')
else
partsFromM3U8.SaveToFile(filePath + ExtractFileNameOnly(fileName) + '.m3u8');
FreeAndNil(partsFromM3U8);
memLog.Append('Parts renaming done');
end;
procedure TfrmBase.acOpenFilesExecute(Sender: TObject);
begin
if OpenDialog1.Execute then FillGridFromFiles;
end;
procedure TfrmBase.stgVlastnostiAfterSelection(Sender: TObject; aCol,
aRow: Integer);
begin
// add selected rows file to play
frmPlayer.Caption:= stgVlastnosti.Cells[0,stgVlastnosti.Row];
//frmPlayer.mpvPlayer.Filename:= OpenDialog1.Files[stgVlastnosti.Row-1];
frmPlayer.mpvPlayer.Play(OpenDialog1.Files[stgVlastnosti.Row-1]);
// add user chapters if any
if filesChapters[stgVlastnosti.Row-1]['user'].Count > 0 then
begin
frmPlayer.stgTimePoints.RowCount:= filesChapters[stgVlastnosti.Row-1]['user'].count;
frmPlayer.stgTimePoints.Cols[0].Clear;
frmPlayer.stgTimePoints.Cols[0].AddStrings(filesChapters[stgVlastnosti.Row-1]['user']);
frmPlayer.stgTimePoints.Cols[1].Clear;
frmPlayer.stgTimePoints.Cols[1].AddStrings(filesChapters[stgVlastnosti.Row-1]['userNames']);
end;
// enable btnImportChapters action if possible
if filesChapters[stgVlastnosti.Row-1]['internal'].Count > 0 then
frmPlayer.acImportChapters.Enabled:=True
else
frmPlayer.acImportChapters.Enabled:=False;
end;
procedure TfrmBase.stgVlastnostiBeforeSelection(Sender: TObject; aCol,
aRow: Integer);
begin
filesChapters[stgVlastnosti.Row-1]['user'].Clear;
filesChapters[stgVlastnosti.Row-1]['user'].AddStrings(frmPlayer.stgTimePoints.Cols[0]);
// clear user chapters in frmPlayer String Grid edit: seems like does not clear Columns properly
// and that is why frmPlayer.stgTimePoints.Cols[0].Clear is line 417 needed;
filesChapters[stgVlastnosti.Row-1]['userNames'].Clear;
filesChapters[stgVlastnosti.Row-1]['userNames'].AddStrings(frmPlayer.stgTimePoints.Cols[1]);
frmPlayer.stgTimePoints.Clear;
end;
procedure TfrmBase.stgVlastnostiCheckboxToggled(Sender: TObject; aCol,
aRow: Integer; aState: TCheckboxState);
begin
//memLog.Append(format('%d %d %d',[aCol,aRow, integer(astate)]));
if (aState = cbchecked) and (filesChapters[aRow-1]['internal'].count = 0) then
begin
stgVlastnosti.Cells[aCol,aRow] := '0';
ShowMessage( rsFile + stgVlastnosti.Cells[0, aRow] + LineEnding + rsHas
+ rsNoInternalCh);
end;
end;
procedure TfrmBase.acVideoOriginalExecute(Sender: TObject);
// ExtractFileDir(pomFile) i:\Jirka-video-audiobook čárka
// ExtractFileNameOnly(pomFile) Astrid_Lindgrenová_Děti_z_Bullerbynu
// ExtractFileName(pomFile) Astrid_Lindgrenová_Děti_z_Bullerbynu.mp4
// ExtractFileExt(pomFile) .mp4
// ExtractFilePath(pomFile) i:\Jirka-video-audiobook čárka\
var
pomFile:String;
i: Integer;
begin
prbUkazatel.Position:=0;
for i:=0 to OpenDialog1.Files.Count-1 do
begin
try
// segment_time vs segment_times see: leVelikostSegmentu.toHHMMSS
pomFile:=OpenDialog1.Files[i];
memLog.Append('velikost segmentu: ' + leVelikostSegmentu.toHHMMSS );
runFFMPEG(ffmpeg,'-progress stats.txt, -i, '+AnsiQuotedStr(pomFile,'"')+
' -c copy, -map 0, ' + pomSegment + leVelikostSegmentu.toHHMMSS +
pomSegmentList(pomFile,i) + ', -f segment, -reset_timestamps 1,'+
AnsiQuotedStr(ExtractFilePath(pomFile)+ExtractFileNameOnly(pomFile)+
'_%03d.mp4','"'),prbUkazatel.Position);
except
on E:Exception do
begin
memLog.Append('Chyba: ' + e.Message);
exit;
end;
end;
end;
memLog.Append('HOTOVO :-)');
end;
procedure TfrmBase.chcbWithoutSplitChange(Sender: TObject);
begin
radGrSegment.Enabled:= not radGrSegment.Enabled;
leVelikostSegmentu.Enabled := not leVelikostSegmentu.Enabled;
chcbPlaylist.Enabled := not chcbPlaylist.Enabled;
end;
procedure TfrmBase.acAudioOriginalExecute(Sender: TObject);
var
pomFile: String;
i: Integer;
begin
prbUkazatel.Position:=0;
segInfoBck.BackupSegmentInfo(); // backup leVelikostSegmentu and radGrSegment;
for i:=0 to OpenDialog1.Files.Count-1 do
begin
pomFile:=OpenDialog1.Files[i];
if not chcbWithoutSplit.Checked then
begin
HowToSplit(i); // decide what to use for splitting
runFFMPEG(ffmpeg,' -progress stats.txt, -i, '+AnsiQuotedStr(pomFile,'"')+
' -vn, -c copy, -map 0, ' + pomSegment +leVelikostSegmentu.toHHMMSS +
pomSegmentList(pomFile,i) + ', -f segment, -reset_timestamps 1,'+
AnsiQuotedStr(ExtractFilePath(pomFile)+ExtractFileNameOnly(pomFile)+
'_%03d.' + filesChapters[i]['codecInfo'].Values['audio'] ,'"')
,prbUkazatel.Position);
segInfoBck.RestoreBackup(); // restore leVelikostSegmentu and radGrSegment backup
HowToName(pomFile,i); // rename parts according to the logic from HowToSplit()
end
else
begin
runFFMPEG(ffmpeg,' -progress stats.txt, -i, '+AnsiQuotedStr(pomFile,'"')+
' -vn, -c copy, -map 0, '+
AnsiQuotedStr(ExtractFilePath(pomFile)+ExtractFileNameOnly(pomFile)+
'.' + filesChapters[i]['codecInfo'].Values['audio'] ,'"')
,prbUkazatel.Position);
end;
end;
end;
procedure TfrmBase.acAudioMp3Execute(Sender: TObject);
var
pomFile: String;
i: Integer;
begin
prbUkazatel.Position:=0;
segInfoBck.BackupSegmentInfo(); // backup leVelikostSegmentu and radGrSegment;
for i:=0 to OpenDialog1.Files.Count-1 do
begin
pomFile:=OpenDialog1.Files[i];
if not chcbWithoutSplit.Checked then
begin
HowToSplit(i); // decide what to use for splitting
runFFMPEG(ffmpeg,' -progress stats.txt, -i, '+AnsiQuotedStr(pomFile,'"')+
' -vn, -c mp3, -map a, ' + pomSegment + leVelikostSegmentu.toHHMMSS +
pomSegmentList(pomFile,i) + ', -f segment, -reset_timestamps 1,'+
AnsiQuotedStr(ExtractFilePath(pomFile)+ExtractFileNameOnly(pomFile)+
'_%03d.mp3','"'),prbUkazatel.Position);
segInfoBck.RestoreBackup(); // restore leVelikostSegmentu and radGrSegment backup
HowToName(pomFile,i); // rename parts according to the logic from HowToSplit()
end
else
begin
runFFMPEG(ffmpeg,' -progress stats.txt, -i, '+AnsiQuotedStr(pomFile,'"')+
' -vn, -c mp3, -map a, ' +
AnsiQuotedStr(ExtractFilePath(pomFile)+ExtractFileNameOnly(pomFile)+
'.mp3','"'),prbUkazatel.Position);
end;
end;
end;
procedure TfrmBase.acClearLogExecute(Sender: TObject);
begin
memLog.Clear;
prbUkazatel.Position:=0;
end;
procedure TfrmBase.acQuitApplicationExecute(Sender: TObject);
begin
frmMain.Close;
end;
procedure TfrmBase.acFlipFormsExecute(Sender: TObject);
var
pomCaption: TCaption;
{
i, j,k: Integer;
mujSplitter: TAnchorDockSplitter;
}
begin
pomCaption := frmMain.Caption;
// undock frmPlayer
frmPlayer.mpvPlayer.Close();
frmPlayer.WindowState:=wsMinimized;
Application.ProcessMessages;
DockMaster.ManualFloat(frmPlayer) ;
// dock frmPlayer from other side
if playerPosition = alRight then
playerPosition:=alLeft
else
playerPosition:=alRight;
Application.ProcessMessages;
DockMaster.ManualDock(DockMaster.GetAnchorSite(frmPlayer),
TCustomForm(frmMain), playerPosition, frmMain);
frmMain.Caption:=pomCaption;
If stgVlastnosti.RowCount > 1 then
frmPlayer.mpvPlayer.Play(OpenDialog1.Files[stgVlastnosti.Row-1]);
// testing AnchorDock inners :-)
{
for i:=0 to DockMaster.ControlCount-1 do
begin
memLog.Append(Format('- control: %s AnchorSite: %s. CustomSite: %s',
[DockMaster.Controls[i].ToString
,BoolToStr(DockMaster.IsAnchorSite(DockMaster.Controls[i]),True)
,BoolToStr(DockMaster.IsCustomSite(DockMaster.Controls[i]),True)
]
)
);
for j:=0 to DockMaster.GetSite(DockMaster.Controls[i]).ControlCount -1 do
begin
memLog.Append(Format(' - - name: %s width: %d ',
[DockMaster.GetSite(DockMaster.Controls[i]).Controls[j].ToString
,DockMaster.GetSite(DockMaster.Controls[i]).Controls[j].Width
]
)
);
for k:=0 to DockMaster.GetSite(DockMaster.Controls[i]).Controls[j].AnchoredControlCount-1 do
begin
memLog.Append(Format(' - - - name: %s width: %d ',
[DockMaster.GetSite(DockMaster.Controls[i]).Controls[j].AnchoredControls[k].ToString
,DockMaster.GetSite(DockMaster.Controls[i]).Controls[j].AnchoredControls[k].Width]));
end;
end;
end;
memLog.Append(
(DockMaster.GetSite(DockMaster.Controls[0]).Controls[0].AnchoredControls[1] as TAnchorDockSplitter)
.getSplitterPosition.ToString);
(DockMaster.GetSite(DockMaster.Controls[0]).Controls[0].AnchoredControls[1] as TAnchorDockSplitter)
.SetSplitterPosition(0);
memlog.Append((frmMain.DockManager as TAnchorDockManager).Site.Controls[0].AnchoredControls[1].ToString);
((frmMain.DockManager as TAnchorDockManager)
.Site.Controls[0].AnchoredControls[1] as TAnchorDockSplitter)
.SetSplitterPosition(1000);
if GetDockSplitter(DockMaster.GetSite(DockMaster.Controls[0]).Controls[0].AnchoredControls[0] ,
akRight, mujSplitter) then
begin
memLog.Append(format('form: %s widthSplitter: %s',
[mujSplitter.ToString, mujSplitter.GetSplitterPosition.ToString]
)
);
end;
}
end;
procedure TfrmBase.runFFMPEG(exeFile,myParameters:String;progressBegin:Integer);
var
AProcess : TProcess;
B: array[0..BUF_SIZE] of Char;
N: Integer;
pomS: AnsiString;
progresFile:TFileStream;
progresTStrings:TStringList;
pomS2: String;
begin
AProcess := TProcess.Create(nil);
// setup ffmpeg process (Tprocess) parametres
AProcess.Executable := 'exes\'+exeFile;
AProcess.Parameters.Delimiter:=',';
AProcess.Parameters.DelimitedText:=myParameters;
AProcess.Options := [poUsePipes,poStderrToOutPut,poNoConsole];
// run ffmpeg
AProcess.Execute;
// create objects needed for progress displaying
// stat.txt is written by ffmpeg process see: -progress stats.txt
progresFile:=TFileStream.Create('stats.txt',fmCreate or fmShareDenyNone);
// TStringList needed for simple processing key=value lines in stats.txt
progresTStrings:=TStringList.Create;
// repeat until ffmpeg process is finished
repeat
// copy ffmpeg process output to memLog component
N := AProcess.Output.Read(B, BUF_SIZE);
if N <> 0 then
begin
SetLength(pomS, N);
Move(B, PomS[1], N);
memLog.Text:= memLog.Text + pomS;
SendMessage(memLog.Handle, WM_VSCROLL, SB_BOTTOM, 0);
end;
// read and "parse" stats.txt
progresTStrings.LoadFromStream(progresFile);
pomS2:=progresTStrings.Values['out_time_ms'];
if not (poms2 = '') then
prbUkazatel.position:=progressBegin+round(StrToFloat(pomS2)/1e6);
// application needs to be responsive
Application.ProcessMessages;
until (N = 0) and (not AProcess.Running);
// clean up objects including stats.txt file
progresFile.Free;
progresTStrings.Free;
AProcess.Free;
DeleteFileUTF8('stats.txt');
end;
procedure TfrmBase.UpdateTranslation(ALang: String);
begin
inherited UpdateTranslation(ALang);
lblPocetSouboru.Refresh;
end;
procedure TfrmBase.FormCreate(Sender: TObject);
begin
// fixed file name and its duration for debugging
//prbUkazatel.Max:=3764;
// OpenDialog1.FileName:='i:\Jirka-video-audiobook čárka\Astrid_Lindgrenová_Děti_z_Bullerbynu.mp4';
radGrSegment.Items[0] := rsVelikost;
radGrSegment.Items[1] := rsZaTek;
stgVlastnosti.Columns.Items[0].Title.Caption := rsJmNo ;
stgVlastnosti.Columns.Items[1].Title.Caption := rsKodek;
stgVlastnosti.Columns.Items[2].Title.Caption := rsChapters ;
stgVlastnosti.Columns.Items[3].Title.Caption := rsUseChapters;
memLog.MaxLength:=0;
stgVlastnosti.ColWidths[0]:=473;
stgVlastnosti.ColWidths[1]:=100;
radGrSegment.ItemIndex:=0;
stgVlastnosti.SelectedColor:= clHighlight;
stgVlastnosti.FocusColor:= clDefault;
filesChapters := TFilesChapters.Create(True);
segInfoBck := TSegmentInfoBackup.Create(Self);
segInfoBck.BackupSegmentInfo();
//DockMaster.MakeDockable(Self);
//DockMaster.ManualDock(DockMaster.GetAnchorSite(self),TCustomForm(frmMain),alBottom,nil);
frmBase.Caption:= rsBaseCaption;
end;
procedure TfrmBase.FormDestroy(Sender: TObject);
begin
filesChapters.Free;
end;
end.