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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "Edl.h"
#include "FileItem.h"
#include "ServiceBroker.h"
#include "cores/EdlEdit.h"
#include "filesystem/File.h"
#include "pvr/PVREdl.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/XBMCTinyXML.h"
#include "utils/log.h"
#include "PlatformDefs.h"
#define COMSKIP_HEADER "FILE PROCESSING COMPLETE"
#define VIDEOREDO_HEADER "<Version>2"
#define VIDEOREDO_TAG_CUT "<Cut>"
#define VIDEOREDO_TAG_SCENE "<SceneMarker "
using namespace EDL;
using namespace XFILE;
CEdl::CEdl()
{
Clear();
}
void CEdl::Clear()
{
m_vecEdits.clear();
m_vecSceneMarkers.clear();
m_totalCutTime = 0;
m_lastEditTime = -1;
}
bool CEdl::ReadEditDecisionLists(const CFileItem& fileItem, const float fFramesPerSecond)
{
bool bFound = false;
/*
* Only check for edit decision lists if the movie is on the local hard drive, or accessed over a
* network share.
*/
const std::string& strMovie = fileItem.GetDynPath();
if ((URIUtils::IsHD(strMovie) || URIUtils::IsOnLAN(strMovie)) &&
!URIUtils::IsInternetStream(strMovie))
{
CLog::Log(LOGDEBUG,
"{} - Checking for edit decision lists (EDL) on local drive or remote share for: {}",
__FUNCTION__, CURL::GetRedacted(strMovie));
/*
* Read any available file format until a valid EDL related file is found.
*/
if (!bFound)
bFound = ReadVideoReDo(strMovie);
if (!bFound)
bFound = ReadEdl(strMovie, fFramesPerSecond);
if (!bFound)
bFound = ReadComskip(strMovie, fFramesPerSecond);
if (!bFound)
bFound = ReadBeyondTV(strMovie);
}
else
{
bFound = ReadPvr(fileItem);
}
if (bFound)
{
MergeShortCommBreaks();
AddSceneMarkersAtStartAndEndOfEdits();
}
return bFound;
}
bool CEdl::ReadEdl(const std::string& strMovie, const float fFramesPerSecond)
{
Clear();
std::string edlFilename(URIUtils::ReplaceExtension(strMovie, ".edl"));
if (!CFile::Exists(edlFilename))
return false;
CFile edlFile;
if (!edlFile.Open(edlFilename))
{
CLog::Log(LOGERROR, "{} - Could not open EDL file: {}", __FUNCTION__,
CURL::GetRedacted(edlFilename));
return false;
}
bool bError = false;
int iLine = 0;
std::string strBuffer;
strBuffer.resize(1024);
while (edlFile.ReadString(&strBuffer[0], 1024))
{
// Log any errors from previous run in the loop
if (bError)
CLog::Log(LOGWARNING, "{} - Error on line {} in EDL file: {}", __FUNCTION__, iLine,
CURL::GetRedacted(edlFilename));
bError = false;
iLine++;
char buffer1[513];
char buffer2[513];
int iAction;
int iFieldsRead = sscanf(strBuffer.c_str(), "%512s %512s %i", buffer1,
buffer2, &iAction);
if (iFieldsRead != 2 && iFieldsRead != 3) // Make sure we read the right number of fields
{
bError = true;
continue;
}
std::vector<std::string> strFields(2);
strFields[0] = buffer1;
strFields[1] = buffer2;
if (iFieldsRead == 2) // If only 2 fields read, then assume it's a scene marker.
{
iAction = atoi(strFields[1].c_str());
strFields[1] = strFields[0];
}
if (StringUtils::StartsWith(strFields[0], "##"))
{
CLog::Log(LOGDEBUG, "Skipping comment line {} in EDL file: {}", iLine,
CURL::GetRedacted(edlFilename));
continue;
}
/*
* For each of the first two fields read, parse based on whether it is a time string
* (HH:MM:SS.sss), frame marker (#12345), or normal seconds string (123.45).
*/
int64_t editStartEnd[2];
for (int i = 0; i < 2; i++)
{
if (strFields[i].find(':') != std::string::npos) // HH:MM:SS.sss format
{
std::vector<std::string> fieldParts = StringUtils::Split(strFields[i], '.');
if (fieldParts.size() == 1) // No ms
{
editStartEnd[i] = StringUtils::TimeStringToSeconds(fieldParts[0]) *
static_cast<int64_t>(1000); // seconds to ms
}
else if (fieldParts.size() == 2) // Has ms. Everything after the dot (.) is ms
{
/*
* Have to pad or truncate the ms portion to 3 characters before converting to ms.
*/
if (fieldParts[1].length() == 1)
{
fieldParts[1] = fieldParts[1] + "00";
}
else if (fieldParts[1].length() == 2)
{
fieldParts[1] = fieldParts[1] + "0";
}
else if (fieldParts[1].length() > 3)
{
fieldParts[1] = fieldParts[1].substr(0, 3);
}
editStartEnd[i] =
static_cast<int64_t>(StringUtils::TimeStringToSeconds(fieldParts[0])) * 1000 +
std::atoi(fieldParts[1].c_str()); // seconds to ms
}
else
{
bError = true;
continue;
}
}
else if (strFields[i][0] == '#') // #12345 format for frame number
{
if (fFramesPerSecond > 0.0f)
{
editStartEnd[i] = static_cast<int64_t>(std::atol(strFields[i].substr(1).c_str()) /
fFramesPerSecond * 1000); // frame number to ms
}
else
{
CLog::Log(LOGERROR,
"Edl::ReadEdl - Frame number not supported in EDL files when frame rate is "
"unavailable (ts) - supplied frame number: {}",
strFields[i].substr(1));
return false;
}
}
else // Plain old seconds in float format, e.g. 123.45
{
editStartEnd[i] = std::lround(std::atof(strFields[i].c_str()) * 1000); // seconds to ms
}
}
if (bError) // If there was an error in the for loop, ignore and continue with the next line
continue;
Edit edit;
edit.start = editStartEnd[0];
edit.end = editStartEnd[1];
switch (iAction)
{
case 0:
edit.action = Action::CUT;
if (!AddEdit(edit))
{
CLog::Log(LOGWARNING, "{} - Error adding cut from line {} in EDL file: {}", __FUNCTION__,
iLine, CURL::GetRedacted(edlFilename));
continue;
}
break;
case 1:
edit.action = Action::MUTE;
if (!AddEdit(edit))
{
CLog::Log(LOGWARNING, "{} - Error adding mute from line {} in EDL file: {}", __FUNCTION__,
iLine, CURL::GetRedacted(edlFilename));
continue;
}
break;
case 2:
if (!AddSceneMarker(edit.end))
{
CLog::Log(LOGWARNING, "{} - Error adding scene marker from line {} in EDL file: {}",
__FUNCTION__, iLine, CURL::GetRedacted(edlFilename));
continue;
}
break;
case 3:
edit.action = Action::COMM_BREAK;
if (!AddEdit(edit))
{
CLog::Log(LOGWARNING, "{} - Error adding commercial break from line {} in EDL file: {}",
__FUNCTION__, iLine, CURL::GetRedacted(edlFilename));
continue;
}
break;
default:
CLog::Log(LOGWARNING, "{} - Invalid action on line {} in EDL file: {}", __FUNCTION__, iLine,
CURL::GetRedacted(edlFilename));
continue;
}
}
if (bError) // Log last line warning, if there was one, since while loop will have terminated.
CLog::Log(LOGWARNING, "{} - Error on line {} in EDL file: {}", __FUNCTION__, iLine,
CURL::GetRedacted(edlFilename));
edlFile.Close();
if (HasEdits() || HasSceneMarker())
{
CLog::Log(LOGDEBUG, "{0} - Read {1} edits and {2} scene markers in EDL file: {3}", __FUNCTION__,
m_vecEdits.size(), m_vecSceneMarkers.size(), CURL::GetRedacted(edlFilename));
return true;
}
else
{
CLog::Log(LOGDEBUG, "{} - No edits or scene markers found in EDL file: {}", __FUNCTION__,
CURL::GetRedacted(edlFilename));
return false;
}
}
bool CEdl::ReadComskip(const std::string& strMovie, const float fFramesPerSecond)
{
Clear();
std::string comskipFilename(URIUtils::ReplaceExtension(strMovie, ".txt"));
if (!CFile::Exists(comskipFilename))
return false;
CFile comskipFile;
if (!comskipFile.Open(comskipFilename))
{
CLog::Log(LOGERROR, "{} - Could not open Comskip file: {}", __FUNCTION__,
CURL::GetRedacted(comskipFilename));
return false;
}
char szBuffer[1024];
if (comskipFile.ReadString(szBuffer, 1023)
&& strncmp(szBuffer, COMSKIP_HEADER, strlen(COMSKIP_HEADER)) != 0) // Line 1.
{
CLog::Log(LOGERROR,
"{} - Invalid Comskip file: {}. Error reading line 1 - expected '{}' at start.",
__FUNCTION__, CURL::GetRedacted(comskipFilename), COMSKIP_HEADER);
comskipFile.Close();
return false;
}
int iFrames;
float fFrameRate;
if (sscanf(szBuffer, "FILE PROCESSING COMPLETE %i FRAMES AT %f", &iFrames, &fFrameRate) != 2)
{
/*
* Not all generated Comskip files have the frame rate information.
*/
if (fFramesPerSecond > 0.0f)
{
fFrameRate = fFramesPerSecond;
CLog::Log(LOGWARNING,
"Edl::ReadComskip - Frame rate not in Comskip file. Using detected frames per "
"second: {:.3f}",
fFrameRate);
}
else
{
CLog::Log(LOGERROR, "Edl::ReadComskip - Frame rate is unavailable and also not in Comskip file (ts).");
return false;
}
}
else
fFrameRate /= 100; // Reduce by factor of 100 to get fps.
(void)comskipFile.ReadString(szBuffer, 1023); // Line 2. Ignore "-------------"
bool bValid = true;
int iLine = 2;
while (bValid && comskipFile.ReadString(szBuffer, 1023)) // Line 3 and onwards.
{
iLine++;
double dStartFrame, dEndFrame;
if (sscanf(szBuffer, "%lf %lf", &dStartFrame, &dEndFrame) == 2)
{
Edit edit;
edit.start = std::lround(dStartFrame / static_cast<double>(fFrameRate) * 1000.0);
edit.end = std::lround(dEndFrame / static_cast<double>(fFrameRate) * 1000.0);
edit.action = Action::COMM_BREAK;
bValid = AddEdit(edit);
}
else
bValid = false;
}
comskipFile.Close();
if (!bValid)
{
CLog::Log(LOGERROR,
"{} - Invalid Comskip file: {}. Error on line {}. Clearing any valid commercial "
"breaks found.",
__FUNCTION__, CURL::GetRedacted(comskipFilename), iLine);
Clear();
return false;
}
else if (HasEdits())
{
CLog::Log(LOGDEBUG, "{0} - Read {1} commercial breaks from Comskip file: {2}", __FUNCTION__,
m_vecEdits.size(), CURL::GetRedacted(comskipFilename));
return true;
}
else
{
CLog::Log(LOGDEBUG, "{} - No commercial breaks found in Comskip file: {}", __FUNCTION__,
CURL::GetRedacted(comskipFilename));
return false;
}
}
bool CEdl::ReadVideoReDo(const std::string& strMovie)
{
/*
* VideoReDo file is strange. Tags are XML like, but it isn't an XML file.
*
* http://www.videoredo.com/
*/
Clear();
std::string videoReDoFilename(URIUtils::ReplaceExtension(strMovie, ".Vprj"));
if (!CFile::Exists(videoReDoFilename))
return false;
CFile videoReDoFile;
if (!videoReDoFile.Open(videoReDoFilename))
{
CLog::Log(LOGERROR, "{} - Could not open VideoReDo file: {}", __FUNCTION__,
CURL::GetRedacted(videoReDoFilename));
return false;
}
char szBuffer[1024];
if (videoReDoFile.ReadString(szBuffer, 1023)
&& strncmp(szBuffer, VIDEOREDO_HEADER, strlen(VIDEOREDO_HEADER)) != 0)
{
CLog::Log(LOGERROR,
"{} - Invalid VideoReDo file: {}. Error reading line 1 - expected {}. Only version 2 "
"files are supported.",
__FUNCTION__, CURL::GetRedacted(videoReDoFilename), VIDEOREDO_HEADER);
videoReDoFile.Close();
return false;
}
int iLine = 1;
bool bValid = true;
while (bValid && videoReDoFile.ReadString(szBuffer, 1023))
{
iLine++;
if (strncmp(szBuffer, VIDEOREDO_TAG_CUT, strlen(VIDEOREDO_TAG_CUT)) == 0) // Found the <Cut> tag
{
/*
* double is used as 32 bit float would overflow.
*/
double dStart, dEnd;
if (sscanf(szBuffer + strlen(VIDEOREDO_TAG_CUT), "%lf:%lf", &dStart, &dEnd) == 2)
{
/*
* Times need adjusting by 1/10,000 to get ms.
*/
Edit edit;
edit.start = std::lround(dStart / 10000);
edit.end = std::lround(dEnd / 10000);
edit.action = Action::CUT;
bValid = AddEdit(edit);
}
else
bValid = false;
}
else if (strncmp(szBuffer, VIDEOREDO_TAG_SCENE, strlen(VIDEOREDO_TAG_SCENE)) == 0) // Found the <SceneMarker > tag
{
int iScene;
double dSceneMarker;
if (sscanf(szBuffer + strlen(VIDEOREDO_TAG_SCENE), " %i>%lf", &iScene, &dSceneMarker) == 2)
bValid = AddSceneMarker(
std::lround(dSceneMarker / 10000)); // Times need adjusting by 1/10,000 to get ms.
else
bValid = false;
}
/*
* Ignore any other tags.
*/
}
videoReDoFile.Close();
if (!bValid)
{
CLog::Log(LOGERROR,
"{} - Invalid VideoReDo file: {}. Error in line {}. Clearing any valid edits or "
"scenes found.",
__FUNCTION__, CURL::GetRedacted(videoReDoFilename), iLine);
Clear();
return false;
}
else if (HasEdits() || HasSceneMarker())
{
CLog::Log(LOGDEBUG, "{0} - Read {1} edits and {2} scene markers in VideoReDo file: {3}",
__FUNCTION__, m_vecEdits.size(), m_vecSceneMarkers.size(),
CURL::GetRedacted(videoReDoFilename));
return true;
}
else
{
CLog::Log(LOGDEBUG, "{} - No edits or scene markers found in VideoReDo file: {}", __FUNCTION__,
CURL::GetRedacted(videoReDoFilename));
return false;
}
}
bool CEdl::ReadBeyondTV(const std::string& strMovie)
{
Clear();
std::string beyondTVFilename(URIUtils::ReplaceExtension(strMovie, URIUtils::GetExtension(strMovie) + ".chapters.xml"));
if (!CFile::Exists(beyondTVFilename))
return false;
CXBMCTinyXML xmlDoc;
if (!xmlDoc.LoadFile(beyondTVFilename))
{
CLog::Log(LOGERROR, "{} - Could not load Beyond TV file: {}. {}", __FUNCTION__,
CURL::GetRedacted(beyondTVFilename), xmlDoc.ErrorDesc());
return false;
}
if (xmlDoc.Error())
{
CLog::Log(LOGERROR, "{} - Could not parse Beyond TV file: {}. {}", __FUNCTION__,
CURL::GetRedacted(beyondTVFilename), xmlDoc.ErrorDesc());
return false;
}
TiXmlElement *pRoot = xmlDoc.RootElement();
if (!pRoot || strcmp(pRoot->Value(), "cutlist"))
{
CLog::Log(LOGERROR, "{} - Invalid Beyond TV file: {}. Expected root node to be <cutlist>",
__FUNCTION__, CURL::GetRedacted(beyondTVFilename));
return false;
}
bool bValid = true;
TiXmlElement *pRegion = pRoot->FirstChildElement("Region");
while (bValid && pRegion)
{
TiXmlElement *pStart = pRegion->FirstChildElement("start");
TiXmlElement *pEnd = pRegion->FirstChildElement("end");
if (pStart && pEnd && pStart->FirstChild() && pEnd->FirstChild())
{
/*
* Need to divide the start and end times by a factor of 10,000 to get msec.
* E.g. <start comment="00:02:44.9980867">1649980867</start>
*
* Use atof so doesn't overflow 32 bit float or integer / long.
* E.g. <end comment="0:26:49.0000009">16090090000</end>
*
* Don't use atoll even though it is more correct as it isn't natively supported by
* Visual Studio.
*
* atof() returns 0 if there were any problems and will subsequently be rejected in AddEdit().
*/
Edit edit;
edit.start = std::lround((std::atof(pStart->FirstChild()->Value()) / 10000));
edit.end = std::lround((std::atof(pEnd->FirstChild()->Value()) / 10000));
edit.action = Action::COMM_BREAK;
bValid = AddEdit(edit);
}
else
bValid = false;
pRegion = pRegion->NextSiblingElement("Region");
}
if (!bValid)
{
CLog::Log(LOGERROR,
"{} - Invalid Beyond TV file: {}. Clearing any valid commercial breaks found.",
__FUNCTION__, CURL::GetRedacted(beyondTVFilename));
Clear();
return false;
}
else if (HasEdits())
{
CLog::Log(LOGDEBUG, "{0} - Read {1} commercial breaks from Beyond TV file: {2}", __FUNCTION__,
m_vecEdits.size(), CURL::GetRedacted(beyondTVFilename));
return true;
}
else
{
CLog::Log(LOGDEBUG, "{} - No commercial breaks found in Beyond TV file: {}", __FUNCTION__,
CURL::GetRedacted(beyondTVFilename));
return false;
}
}
bool CEdl::ReadPvr(const CFileItem &fileItem)
{
const std::vector<Edit> editlist = PVR::CPVREdl::GetEdits(fileItem);
for (const auto& edit : editlist)
{
switch (edit.action)
{
case Action::CUT:
case Action::MUTE:
case Action::COMM_BREAK:
if (AddEdit(edit))
{
CLog::Log(LOGDEBUG, "{} - Added break [{} - {}] found in PVR item for: {}.", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
CURL::GetRedacted(fileItem.GetDynPath()));
}
else
{
CLog::Log(LOGERROR,
"{} - Invalid break [{} - {}] found in PVR item for: {}. Continuing anyway.",
__FUNCTION__, MillisecondsToTimeString(edit.start),
MillisecondsToTimeString(edit.end), CURL::GetRedacted(fileItem.GetDynPath()));
}
break;
case Action::SCENE:
if (!AddSceneMarker(edit.end))
{
CLog::Log(LOGWARNING, "{} - Error adding scene marker for PVR item", __FUNCTION__);
}
break;
default:
CLog::Log(LOGINFO, "{} - Ignoring entry of unknown edit action: {}", __FUNCTION__,
static_cast<int>(edit.action));
break;
}
}
return !editlist.empty();
}
bool CEdl::AddEdit(const Edit& newEdit)
{
Edit edit = newEdit;
if (edit.action != Action::CUT && edit.action != Action::MUTE &&
edit.action != Action::COMM_BREAK)
{
CLog::Log(LOGERROR,
"{} - Not an Action::CUT, Action::MUTE, or Action::COMM_BREAK! [{} - {}], {}",
__FUNCTION__, MillisecondsToTimeString(edit.start),
MillisecondsToTimeString(edit.end), static_cast<int>(edit.action));
return false;
}
if (edit.start < 0)
{
CLog::Log(LOGERROR, "{} - Before start! [{} - {}], {}", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
static_cast<int>(edit.action));
return false;
}
if (edit.start >= edit.end)
{
CLog::Log(LOGERROR, "{} - Times are around the wrong way or the same! [{} - {}], {}",
__FUNCTION__, MillisecondsToTimeString(edit.start),
MillisecondsToTimeString(edit.end), static_cast<int>(edit.action));
return false;
}
if (InEdit(edit.start) || InEdit(edit.end))
{
CLog::Log(LOGERROR, "{} - Start or end is in an existing edit! [{} - {}], {}", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
static_cast<int>(edit.action));
return false;
}
for (size_t i = 0; i < m_vecEdits.size(); ++i)
{
if (edit.start < m_vecEdits[i].start && edit.end > m_vecEdits[i].end)
{
CLog::Log(LOGERROR, "{} - Edit surrounds an existing edit! [{} - {}], {}", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
static_cast<int>(edit.action));
return false;
}
}
if (edit.action == Action::COMM_BREAK)
{
/*
* Detection isn't perfect near the edges of commercial breaks so automatically wait for a bit at
* the start (autowait) and automatically rewind by a bit (autowind) at the end of the commercial
* break.
*/
int autowait = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_iEdlCommBreakAutowait * 1000; // seconds -> ms
int autowind = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_iEdlCommBreakAutowind * 1000; // seconds -> ms
if (edit.start > 0) // Only autowait if not at the start.
{
/* get the edit length so we don't start skipping after the end */
int editLength = edit.end - edit.start;
/* add the lesser of the edit length or the autowait to the start */
edit.start += autowait > editLength ? editLength : autowait;
}
if (edit.end > edit.start) // Only autowind if there is any edit time remaining.
{
/* get the remaining edit length so we don't rewind to before the start */
int editLength = edit.end - edit.start;
/* subtract the lesser of the edit length or the autowind from the end */
edit.end -= autowind > editLength ? editLength : autowind;
}
}
/*
* Insert edit in the list in the right position (ALL algorithms assume edits are in ascending order)
*/
if (m_vecEdits.empty() || edit.start > m_vecEdits.back().start)
{
CLog::Log(LOGDEBUG, "{} - Pushing new edit to back [{} - {}], {}", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
static_cast<int>(edit.action));
m_vecEdits.emplace_back(edit);
}
else
{
std::vector<Edit>::iterator pCurrentEdit;
for (pCurrentEdit = m_vecEdits.begin(); pCurrentEdit != m_vecEdits.end(); ++pCurrentEdit)
{
if (edit.start < pCurrentEdit->start)
{
CLog::Log(LOGDEBUG, "{} - Inserting new edit [{} - {}], {}", __FUNCTION__,
MillisecondsToTimeString(edit.start), MillisecondsToTimeString(edit.end),
static_cast<int>(edit.action));
m_vecEdits.insert(pCurrentEdit, edit);
break;
}
}
}
if (edit.action == Action::CUT)
m_totalCutTime += edit.end - edit.start;
return true;
}
bool CEdl::AddSceneMarker(const int iSceneMarker)
{
Edit edit;
if (InEdit(iSceneMarker, &edit) && edit.action == Action::CUT) // Only works for current cuts.
return false;
CLog::Log(LOGDEBUG, "{} - Inserting new scene marker: {}", __FUNCTION__,
MillisecondsToTimeString(iSceneMarker));
m_vecSceneMarkers.push_back(iSceneMarker); // Unsorted
return true;
}
bool CEdl::HasEdits() const
{
return !m_vecEdits.empty();
}
bool CEdl::HasCuts() const
{
return m_totalCutTime > 0;
}
int CEdl::GetTotalCutTime() const
{
return m_totalCutTime; // ms
}
const std::vector<EDL::Edit> CEdl::GetEditList() const
{
// the sum of cut durations while we iterate over them
// note: edits are ordered by start time
int surpassedSumOfCutDurations{0};
std::vector<EDL::Edit> editList;
// @note we should not modify the original edits since
// they are used during playback. However we need to correct
// the start and end times to present on the GUI by removing
// the already surpassed cut time. The copy here is intentional
// \sa Player_Editlist
for (EDL::Edit edit : m_vecEdits)
{
if (edit.action == Action::CUT)
{
surpassedSumOfCutDurations += edit.end - edit.start;
continue;
}
// substract the duration of already surpassed cuts
edit.start -= surpassedSumOfCutDurations;
edit.end -= surpassedSumOfCutDurations;
editList.emplace_back(edit);
}
return editList;
}
const std::vector<int64_t> CEdl::GetCutMarkers() const
{
int surpassedSumOfCutDurations{0};
std::vector<int64_t> cutList;
for (const EDL::Edit& edit : m_vecEdits)
{
if (edit.action != Action::CUT)
continue;
cutList.emplace_back(edit.start - surpassedSumOfCutDurations);
surpassedSumOfCutDurations += edit.end - edit.start;
}
return cutList;
}
const std::vector<int64_t> CEdl::GetSceneMarkers() const
{
std::vector<int64_t> sceneMarkers;
sceneMarkers.reserve(m_vecSceneMarkers.size());
for (const int& scene : m_vecSceneMarkers)
{
sceneMarkers.emplace_back(GetTimeWithoutCuts(scene));
}
return sceneMarkers;
}
int CEdl::GetTimeWithoutCuts(int seek) const
{
if (!HasCuts())
return seek;
int cutTime = 0;
for (const EDL::Edit& edit : m_vecEdits)
{
if (edit.action != Action::CUT)
continue;
// inside cut
if (seek >= edit.start && seek <= edit.end)
{
// decrease cut lenght by 1 ms to jump over the end boundary.
cutTime += seek - edit.start - 1;
}
// cut has already been passed over
else if (seek >= edit.start)
{
cutTime += edit.end - edit.start;
}
}
return seek - cutTime;
}
double CEdl::GetTimeAfterRestoringCuts(double seek) const
{
if (!HasCuts())
return seek;
for (const EDL::Edit& edit : m_vecEdits)
{
double cutDuration = static_cast<double>(edit.end - edit.start);
// add 1 ms to jump over the start boundary
if (edit.action == Action::CUT && seek > edit.start + 1)
{
seek += cutDuration;
}
}
return seek;
}
bool CEdl::HasSceneMarker() const
{
return !m_vecSceneMarkers.empty();
}
bool CEdl::InEdit(const int iSeek, Edit* pEdit)
{
for (size_t i = 0; i < m_vecEdits.size(); ++i)
{
if (iSeek < m_vecEdits[i].start) // Early exit if not even up to the edit start time.
return false;
if (iSeek >= m_vecEdits[i].start && iSeek <= m_vecEdits[i].end) // Inside edit.
{
if (pEdit)
*pEdit = m_vecEdits[i];
return true;
}
}
return false;
}
int CEdl::GetLastEditTime() const
{
return m_lastEditTime;
}
void CEdl::SetLastEditTime(int editTime)
{
m_lastEditTime = editTime;
}
void CEdl::ResetLastEditTime()
{
m_lastEditTime = -1;
}
void CEdl::SetLastEditActionType(EDL::Action action)
{
m_lastEditActionType = action;
}
EDL::Action CEdl::GetLastEditActionType() const
{
return m_lastEditActionType;
}
bool CEdl::GetNextSceneMarker(bool bPlus, const int iClock, int *iSceneMarker)
{
if (!HasSceneMarker())
return false;
int iSeek = GetTimeAfterRestoringCuts(iClock);
int iDiff = 10 * 60 * 60 * 1000; // 10 hours to ms.
bool bFound = false;
if (bPlus) // Find closest scene forwards
{
for (int i = 0; i < (int)m_vecSceneMarkers.size(); i++)
{
if ((m_vecSceneMarkers[i] > iSeek) && ((m_vecSceneMarkers[i] - iSeek) < iDiff))
{
iDiff = m_vecSceneMarkers[i] - iSeek;
*iSceneMarker = m_vecSceneMarkers[i];
bFound = true;
}
}
}
else // Find closest scene backwards
{
for (int i = 0; i < (int)m_vecSceneMarkers.size(); i++)
{
if ((m_vecSceneMarkers[i] < iSeek) && ((iSeek - m_vecSceneMarkers[i]) < iDiff))
{
iDiff = iSeek - m_vecSceneMarkers[i];
*iSceneMarker = m_vecSceneMarkers[i];
bFound = true;
}
}
}
/*
* If the scene marker is in a cut then return the end of the cut. Can't guarantee that this is
* picked up when scene markers are added.
*/
Edit edit;
if (bFound && InEdit(*iSceneMarker, &edit) && edit.action == Action::CUT)
*iSceneMarker = edit.end;
return bFound;
}
std::string CEdl::MillisecondsToTimeString(const int iMilliseconds)
{
std::string strTimeString = StringUtils::SecondsToTimeString((long)(iMilliseconds / 1000), TIME_FORMAT_HH_MM_SS); // milliseconds to seconds
strTimeString += StringUtils::Format(".{:03}", iMilliseconds % 1000);
return strTimeString;
}
void CEdl::MergeShortCommBreaks()
{
/*
* mythcommflag routinely seems to put a 20-40ms commercial break at the start of the recording.
*
* Remove any spurious short commercial breaks at the very start so they don't interfere with
* the algorithms below.
*/
if (!m_vecEdits.empty() && m_vecEdits[0].action == Action::COMM_BREAK &&
(m_vecEdits[0].end - m_vecEdits[0].start) < 5 * 1000) // 5 seconds
{
CLog::Log(LOGDEBUG, "{} - Removing short commercial break at start [{} - {}]. <5 seconds",
__FUNCTION__, MillisecondsToTimeString(m_vecEdits[0].start),
MillisecondsToTimeString(m_vecEdits[0].end));
m_vecEdits.erase(m_vecEdits.begin());
}
const std::shared_ptr<CAdvancedSettings> advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
if (advancedSettings->m_bEdlMergeShortCommBreaks)
{
for (size_t i = 0; i < m_vecEdits.size() - 1; ++i)
{
if ((m_vecEdits[i].action == Action::COMM_BREAK &&
m_vecEdits[i + 1].action == Action::COMM_BREAK) &&
(m_vecEdits[i + 1].end - m_vecEdits[i].start <
advancedSettings->m_iEdlMaxCommBreakLength * 1000) // s to ms
&& (m_vecEdits[i + 1].start - m_vecEdits[i].end <
advancedSettings->m_iEdlMaxCommBreakGap * 1000)) // s to ms
{
Edit commBreak;
commBreak.action = Action::COMM_BREAK;
commBreak.start = m_vecEdits[i].start;
commBreak.end = m_vecEdits[i + 1].end;
CLog::Log(
LOGDEBUG, "{} - Consolidating commercial break [{} - {}] and [{} - {}] to: [{} - {}]",
__FUNCTION__, MillisecondsToTimeString(m_vecEdits[i].start),
MillisecondsToTimeString(m_vecEdits[i].end),
MillisecondsToTimeString(m_vecEdits[i + 1].start),
MillisecondsToTimeString(m_vecEdits[i + 1].end),
MillisecondsToTimeString(commBreak.start), MillisecondsToTimeString(commBreak.end));
/*
* Erase old edits and insert the new merged one.
*/
m_vecEdits.erase(m_vecEdits.begin() + i, m_vecEdits.begin() + i + 2);
m_vecEdits.insert(m_vecEdits.begin() + i, commBreak);
i--; // Reduce i to see if the next break is also within the max commercial break length.
}
}
/*
* To cater for recordings that are started early and then have a commercial break identified
* before the TV show starts, expand the first commercial break to the very beginning if it
* starts within the maximum start gap. This is done outside of the consolidation to prevent
* the maximum commercial break length being triggered.
*/
if (!m_vecEdits.empty() && m_vecEdits[0].action == Action::COMM_BREAK &&
m_vecEdits[0].start < advancedSettings->m_iEdlMaxStartGap * 1000)
{
CLog::Log(LOGDEBUG, "{} - Expanding first commercial break back to start [{} - {}].",
__FUNCTION__, MillisecondsToTimeString(m_vecEdits[0].start),
MillisecondsToTimeString(m_vecEdits[0].end));
m_vecEdits[0].start = 0;
}
/*
* Remove any commercial breaks shorter than the minimum (unless at the start)
*/
for (size_t i = 0; i < m_vecEdits.size(); ++i)
{
if (m_vecEdits[i].action == Action::COMM_BREAK && m_vecEdits[i].start > 0 &&
(m_vecEdits[i].end - m_vecEdits[i].start) <
advancedSettings->m_iEdlMinCommBreakLength * 1000)
{
CLog::Log(LOGDEBUG,
"{} - Removing short commercial break [{} - {}]. Minimum length: {} seconds",
__FUNCTION__, MillisecondsToTimeString(m_vecEdits[i].start),
MillisecondsToTimeString(m_vecEdits[i].end),
advancedSettings->m_iEdlMinCommBreakLength);
m_vecEdits.erase(m_vecEdits.begin() + i);
i--;
}
}
}
}
void CEdl::AddSceneMarkersAtStartAndEndOfEdits()
{
for (const EDL::Edit& edit : m_vecEdits)
{
// Add scene markers at the start and end of commercial breaks
if (edit.action == Action::COMM_BREAK)
{
// Don't add a scene marker at the start.
if (edit.start > 0)
AddSceneMarker(edit.start);
AddSceneMarker(edit.end);
}
}
}
|