-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmainfeatures.cpp
1409 lines (1313 loc) · 72.7 KB
/
mainfeatures.cpp
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
#include "./mainfeatures.h"
#include "./attachmentinfo.h"
#include "./helper.h"
#ifdef TAGEDITOR_JSON_EXPORT
#include "./json.h"
#endif
#include "../application/knownfieldmodel.h"
// includes for JavaScript support of set operation
#ifdef TAGEDITOR_USE_JSENGINE
#include "./scriptapi.h"
#endif
// includes for generating HTML info
#if defined(TAGEDITOR_GUI_QTWIDGETS) || defined(TAGEDITOR_GUI_QTQUICK)
#include "../misc/htmlinfo.h"
#include "../misc/utility.h"
#endif
#include "resources/config.h"
#include <tagparser/abstractattachment.h>
#include <tagparser/abstractchapter.h>
#include <tagparser/abstracttrack.h>
#include <tagparser/backuphelper.h>
#include <tagparser/diagnostics.h>
#include <tagparser/id3/id3v2tag.h>
#include <tagparser/localehelper.h>
#include <tagparser/mediafileinfo.h>
#include <tagparser/progressfeedback.h>
#include <tagparser/tag.h>
#include <tagparser/tagvalue.h>
#include <tagparser/vorbis/vorbiscomment.h>
#ifdef TAGEDITOR_JSON_EXPORT
#include <reflective_rapidjson/json/reflector.h>
#endif
#include <c++utilities/application/commandlineutils.h>
#include <c++utilities/conversion/conversionexception.h>
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/ansiescapecodes.h>
#include <c++utilities/io/nativefilestream.h>
#include <c++utilities/io/path.h>
#include <c++utilities/misc/parseerror.h>
// includes for JavaScript support of set operation
#ifdef TAGEDITOR_USE_JSENGINE
#include <QCoreApplication>
#include <QFile>
#include <QJSValue>
#include <QQmlEngine>
#include <QTextStream>
#endif
// includes for generating HTML info
#if defined(TAGEDITOR_GUI_QTWIDGETS) || defined(TAGEDITOR_GUI_QTQUICK)
#include <QDir>
#include <qtutilities/misc/conversion.h>
#endif
#ifdef TAGEDITOR_JSON_EXPORT
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/writer.h>
#endif
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <memory>
#include <optional>
#include <string_view>
using namespace std;
using namespace CppUtilities;
using namespace CppUtilities::EscapeCodes;
using namespace Settings;
using namespace TagParser;
#if defined(TAGEDITOR_GUI_QTWIDGETS) || defined(TAGEDITOR_GUI_QTQUICK)
using namespace QtUtilities;
#endif
namespace Cli {
#define FIELD_NAMES \
"title album artist genre recorddate comment bpm bps lyricist track disk part totalparts encoder\n" \
" releasedate performers duration language encodersettings lyrics synchronizedlyrics grouping\n" \
" recordlabel cover composer rating description vendor albumartist\n" \
" subtitle leadperformer arranger conductor director assistantdirector\n" \
" directorofphotography soundengineer artdirector productiondesigner choregrapher\n" \
" costumedesigner actor character writtenby screenplayby editedby producer\n" \
" coproducer exectiveproducer distributedby masteredby encodedby mixedby\n" \
" remixedby productionstudio thanksto publisher mood originalmediatype\n" \
" contenttype subject keywords summary synopsis initialkey period lawrating\n" \
" encodingdate taggingdate originalreleasedate digitalizationdate writingdate\n" \
" purchasingdate recordinglocation compositionlocation composernationality\n" \
" playcounter measure tuning isrc mcdi isbn barcode catalognumber labelcode\n" \
" lccn imdb tmdb tvdb purchaseitem purchaseinfo purchaseowner purchaseprice\n" \
" purchasecurrency copyright productioncopyright license termsofuse publisherwebpage\n" \
" storedescription mediatype performerwebpage contentrating"
#define TRACK_ATTRIBUTE_NAMES "name tracknumber enabled=yes enabled=no forced=yes forced=no default=yes default=no"
#define TAG_MODIFIER "tag=id3v1 tag=id3v2 tag=id3 tag=itunes tag=vorbis tag=matroska tag=all"
#define TRACK_MODIFIER "track-id= track=all"
#define TARGET_MODIFIER \
"target-level target-levelname target-tracks target-tracks\n" \
" target-chapters target-editions target-attachments target-reset"
const char *const fieldNames = FIELD_NAMES;
const char *const fieldNamesForSet = TAG_MODIFIER " " FIELD_NAMES " " TRACK_MODIFIER " " TRACK_ATTRIBUTE_NAMES " " TARGET_MODIFIER;
int exitCode = EXIT_SUCCESS;
void printFieldNames(const ArgumentOccurrence &)
{
CMD_UTILS_START_CONSOLE;
cout << "Field and track attribute names allow referring to a field or track attribute in a format-independent way.\n"
" - Field names:\n " FIELD_NAMES "\n"
" - Track attribute names: " TRACK_ATTRIBUTE_NAMES "\n\n"
"Modifier specify to which tags and tracks the subsequent values should be applied.\n"
" - Tag modifier: " TAG_MODIFIER "\n"
" - Track modifier: track=id1,id2,id3,... track=all\n"
" - Target modifier:\n " TARGET_MODIFIER "\n"
"ID3v2 cover types:\n"
<< joinStrings<std::remove_reference_t<decltype(id3v2CoverTypeNames())>, std::string>(id3v2CoverTypeNames(), "\n"sv, false, " - "sv, ""sv)
<< '\n'
<< flush;
}
void generateFileInfo(const ArgumentOccurrence &, const Argument &inputFileArg, const Argument &outputFileArg, const Argument &validateArg)
{
CMD_UTILS_START_CONSOLE;
#if defined(TAGEDITOR_GUI_QTWIDGETS) || defined(TAGEDITOR_GUI_QTQUICK)
try {
// parse tags
auto inputFileInfo = MediaFileInfo(std::string(inputFileArg.values().front()));
inputFileInfo.setForceFullParse(validateArg.isPresent());
inputFileInfo.open(true);
auto diag = Diagnostics();
auto progress = AbortableProgressFeedback(); // FIXME: actually use the progress object
inputFileInfo.parseEverything(diag, progress);
// generate and save info
auto diagReparsing = Diagnostics();
(outputFileArg.isPresent() ? cout : cerr) << "Saving file info for \"" << inputFileArg.values().front() << "\" ..." << endl;
if (!outputFileArg.isPresent()) {
cout << HtmlInfo::generateInfo(inputFileInfo, diag, diagReparsing).data() << endl;
return;
}
auto file = QFile(fromNativeFileName(outputFileArg.values().front()));
if (file.open(QFile::WriteOnly) && file.write(HtmlInfo::generateInfo(inputFileInfo, diag, diagReparsing)) && file.flush()) {
cout << "File information has been saved to \"" << outputFileArg.values().front() << "\"." << endl;
} else {
const auto errorMessage = file.errorString().toUtf8();
cerr << Phrases::Error << "An IO error occurred when writing the file \"" << outputFileArg.values().front()
<< "\": " << std::string_view(errorMessage.data(), static_cast<std::string_view::size_type>(errorMessage.size()))
<< Phrases::EndFlush;
exitCode = EXIT_IO_FAILURE;
}
} catch (const TagParser::Failure &) {
cerr << Phrases::Error << "A parsing failure occurred when reading the file \"" << inputFileArg.values().front() << "\"."
<< Phrases::EndFlush;
exitCode = EXIT_PARSING_FAILURE;
} catch (const std::ios_base::failure &e) {
cerr << Phrases::Error << "An IO error occurred when reading the file \"" << inputFileArg.values().front() << "\": " << e.what()
<< Phrases::EndFlush;
exitCode = EXIT_IO_FAILURE;
}
#else
CPP_UTILITIES_UNUSED(inputFileArg);
CPP_UTILITIES_UNUSED(outputFileArg);
CPP_UTILITIES_UNUSED(validateArg);
cerr << Phrases::Error << "Generating HTML info is only available if built with Qt support." << Phrases::EndFlush;
exitCode = EXIT_FAILURE;
#endif
}
void displayFileInfo(
const ArgumentOccurrence &, const Argument &filesArg, const Argument &verboseArg, const Argument &pedanticArg, const Argument &validateArg)
{
CMD_UTILS_START_CONSOLE;
// check whether files have been specified
if (!filesArg.isPresent() || filesArg.values().empty()) {
cerr << Phrases::Error << "No files have been specified." << Phrases::End;
std::exit(EXIT_FAILURE);
}
MediaFileInfo fileInfo;
for (const char *file : filesArg.values()) {
Diagnostics diag;
AbortableProgressFeedback progress; // FIXME: actually use the progress object
try {
// parse tags
if (validateArg.isPresent()) {
fileInfo.setForceFullParse(true);
}
fileInfo.setPath(std::string(file));
fileInfo.open(true);
fileInfo.parseContainerFormat(diag, progress);
fileInfo.parseEverything(diag, progress);
// print general/container-related info
cout << "Technical information for \"" << file << "\":\n";
cout << " - " << TextAttribute::Bold << "Container format: " << fileInfo.containerFormatName() << Phrases::End;
printProperty("Size", dataSizeToString(fileInfo.size()));
if (const auto mimeType = fileInfo.mimeType(); !mimeType.empty()) {
printProperty("Mime-type", mimeType);
}
const auto duration = fileInfo.duration();
if (!duration.isNull()) {
printProperty("Duration", duration);
printProperty("Overall avg. bitrate", bitrateToString(fileInfo.overallAverageBitrate()));
}
if (const auto *const container = fileInfo.container()) {
size_t segmentIndex = 0;
for (const auto &title : container->titles()) {
if (segmentIndex) {
printProperty("Title", title % " (segment " % ++segmentIndex + ")");
} else {
++segmentIndex;
printProperty("Title", title);
}
}
printProperty("Document type", container->documentType());
printProperty("Read version", container->readVersion());
printProperty("Version", container->version());
printProperty("Document read version", container->doctypeReadVersion());
printProperty("Document version", container->doctypeVersion());
printProperty("Creation time", container->creationTime());
printProperty("Modification time", container->modificationTime());
printProperty("Tag position", container->determineTagPosition(diag));
printProperty("Index position", container->determineIndexPosition(diag));
if (const auto &muxingApps = container->muxingApplications(); !muxingApps.empty()) {
printProperty("Muxing application", joinStrings(muxingApps, ", "));
}
if (const auto &writingApps = container->writingApplications(); !writingApps.empty()) {
printProperty("Writing application", joinStrings(writingApps, ", "));
}
}
if (fileInfo.paddingSize()) {
printProperty("Padding", dataSizeToString(fileInfo.paddingSize()));
}
// print tracks
const auto tracks = fileInfo.tracks();
if (!tracks.empty()) {
cout << " - " << TextAttribute::Bold << "Tracks: " << fileInfo.technicalSummary() << Phrases::End;
for (const auto *track : tracks) {
printProperty("ID", track->id(), nullptr, true);
printProperty("Name", track->name());
printProperty("Type", track->mediaTypeName());
if (const auto &language = track->locale().fullOrSomeAbbreviatedName(); !language.empty()) {
printProperty("Language", language);
}
const auto fmtName = track->formatName(), fmtAbbr = track->formatAbbreviation();
printProperty("Format", fmtName);
if (fmtName != fmtAbbr) {
printProperty("Abbreviation", fmtAbbr);
}
printProperty("Extensions", track->format().extensionName());
printProperty("Raw format ID", track->formatId());
if (track->size()) {
printProperty("Size", dataSizeToString(track->size(), true));
}
printProperty("Duration", track->duration());
printProperty("FPS", track->fps());
if (!track->pixelSize().isNull()) {
printProperty("Pixel size", track->pixelSize().toString());
}
if (!track->displaySize().isNull()) {
printProperty("Display size", track->displaySize().toString());
}
if (track->pixelAspectRatio().isValid()) {
printProperty("Pixel Aspect Ratio", track->pixelAspectRatio().toString());
}
if (const auto cc = track->channelConfigString(); !cc.empty()) {
printProperty("Channel config", cc);
} else {
printProperty("Channel count", track->channelCount());
}
if (const auto ecc = track->extensionChannelConfigString(); !ecc.empty()) {
printProperty("Extension channel config", ecc);
}
if (track->bitrate() > 0.0) {
printProperty("Bitrate", bitrateToString(track->bitrate()));
}
printProperty("Bits per sample", track->bitsPerSample());
printProperty("Sampling frequency", track->samplingFrequency(), "Hz");
printProperty("Extension sampling frequency", track->extensionSamplingFrequency(), "Hz");
printProperty(track->mediaType() == MediaType::Video ? "Frame count" : "Sample count", track->sampleCount());
printProperty("Creation time", track->creationTime());
printProperty("Modification time", track->modificationTime());
vector<string> labels;
labels.reserve(7);
if (track->isInterlaced()) {
labels.emplace_back("interlaced");
}
if (!track->isEnabled()) {
labels.emplace_back("disabled");
}
if (track->isDefault()) {
labels.emplace_back("default");
}
if (track->isForced()) {
labels.emplace_back("forced");
}
if (track->hasLacing()) {
labels.emplace_back("has lacing");
}
if (track->isEncrypted()) {
labels.emplace_back("encrypted");
}
if (!labels.empty()) {
printProperty("Labeled as", joinStrings(labels, ", "));
}
cout << '\n';
}
} else {
cout << " - File has no (supported) tracks.\n";
}
// print attachments
const auto attachments = fileInfo.attachments();
if (!attachments.empty()) {
cout << " - " << TextAttribute::Bold << "Attachments:" << TextAttribute::Reset << '\n';
for (const auto *attachment : attachments) {
printProperty("ID", attachment->id());
printProperty("Name", attachment->name());
printProperty("MIME-type", attachment->mimeType());
printProperty("Description", attachment->description());
if (attachment->data()) {
printProperty("Size", dataSizeToString(static_cast<std::uint64_t>(attachment->data()->size()), true));
}
cout << '\n';
}
}
// print chapters
const auto chapters = fileInfo.chapters();
if (!chapters.empty()) {
cout << " - " << TextAttribute::Bold << "Chapters:" << TextAttribute::Reset << '\n';
for (const auto *chapter : chapters) {
printProperty("ID", chapter->id());
if (!chapter->names().empty()) {
printProperty("Name", static_cast<string>(chapter->names().front()));
}
if (!chapter->startTime().isNegative()) {
printProperty("Start time", chapter->startTime().toString());
}
if (!chapter->endTime().isNegative()) {
printProperty("End time", chapter->endTime().toString());
}
cout << '\n';
}
}
} catch (const TagParser::Failure &) {
cerr << Phrases::Error << "A parsing failure occurred when reading the file \"" << file << "\"." << Phrases::EndFlush;
exitCode = EXIT_PARSING_FAILURE;
} catch (const std::ios_base::failure &) {
cerr << Phrases::Error << "An IO error occurred when reading the file \"" << file << "\"" << Phrases::EndFlush;
exitCode = EXIT_IO_FAILURE;
}
printDiagMessages(diag, "Diagnostic messages:", verboseArg.isPresent(), &pedanticArg);
cout << endl;
}
}
void displayTagInfo(
const Argument &fieldsArg, const Argument &showUnsupportedArg, const Argument &filesArg, const Argument &verboseArg, const Argument &pedanticArg)
{
CMD_UTILS_START_CONSOLE;
// check whether files have been specified
if (!filesArg.isPresent() || filesArg.values().empty()) {
std::cerr << Phrases::Error << "No files have been specified." << Phrases::End;
std::exit(EXIT_FAILURE);
}
// parse specified fields
const auto fields = parseFieldDenotations(fieldsArg, true);
auto fileInfo = MediaFileInfo();
fileInfo.setFileHandlingFlags(fileInfo.fileHandlingFlags() | MediaFileHandlingFlags::ConvertTotalFields);
for (const char *file : filesArg.values()) {
Diagnostics diag;
AbortableProgressFeedback progress; // FIXME: actually use the progress object
try {
// parse tags
fileInfo.setPath(std::string(file));
fileInfo.open(true);
fileInfo.parseContainerFormat(diag, progress);
fileInfo.parseTags(diag, progress);
cout << "Tag information for \"" << file << "\":\n";
const auto tags = fileInfo.tags();
if (tags.empty()) {
cout << " - File has no (supported) tag information.\n";
continue;
}
// iterate through all tags
for (const auto *tag : tags) {
// determine tag type
const TagType tagType = tag->type();
// write tag name and target, eg. MP4/iTunes tag
cout << " - " << TextAttribute::Bold << tagName(tag) << TextAttribute::Reset << '\n';
// iterate through fields specified by the user
if (fields.empty()) {
for (auto field = firstKnownField; field != KnownField::Invalid; field = nextKnownField(field)) {
printField(FieldScope(field), tag, tagType, true);
}
if (showUnsupportedArg.isPresent()) {
printNativeFields(tag);
}
} else {
for (const auto &fieldDenotation : fields) {
const FieldScope &denotedScope = fieldDenotation.first;
if (denotedScope.tagType == TagType::Unspecified || (denotedScope.tagType | tagType) != TagType::Unspecified) {
printField(denotedScope, tag, tagType, false);
}
}
}
}
} catch (const TagParser::Failure &) {
cerr << Phrases::Error << "A parsing failure occurred when reading the file \"" << file << "\"." << Phrases::EndFlush;
exitCode = EXIT_PARSING_FAILURE;
} catch (const std::ios_base::failure &) {
cerr << Phrases::Error << "An IO error occurred when reading the file \"" << file << "\"." << Phrases::EndFlush;
exitCode = EXIT_IO_FAILURE;
}
printDiagMessages(diag, "Diagnostic messages:", verboseArg.isPresent(), &pedanticArg);
cout << endl;
}
}
struct Id3v2Cover {
explicit Id3v2Cover(TagValue &&value, CoverType type, std::optional<std::string_view> description)
: value(std::move(value))
, type(type)
, description(description)
{
}
TagValue value;
CoverType type;
std::optional<std::string_view> description;
};
template <class TagType> static void setId3v2CoverValues(TagType *tag, std::vector<Id3v2Cover> &&values)
{
auto &fields = tag->fields();
const auto id = tag->fieldId(KnownField::Cover);
const auto range = fields.equal_range(id);
const auto first = range.first;
for (auto &[tagValue, coverType, description] : values) {
// check whether there is already a tag value with the current type and description
auto pair = std::find_if(first, range.second, std::bind(&fieldPredicate<TagType>, coverType, description, placeholders::_1));
if (pair != range.second) {
// there is already a tag value with the current type and description
// -> update this value
pair->second.setValue(tagValue);
// check whether there are more values with the current type and description
while ((pair = std::find_if(++pair, range.second, std::bind(&fieldPredicate<TagType>, coverType, description, placeholders::_1)))
!= range.second) {
// -> remove these values as we only support one value of a type/description in the same tag
pair->second.setValue(TagValue());
}
} else if (!tagValue.isEmpty()) {
using FieldType = typename TagType::FieldType;
auto newField = FieldType(id, tagValue);
newField.setTypeInfo(static_cast<typename FieldType::TypeInfoType>(coverType));
fields.insert(std::pair(id, std::move(newField)));
}
}
}
#ifdef TAGEDITOR_USE_JSENGINE
class JavaScriptProcessor {
public:
explicit JavaScriptProcessor(const SetTagInfoArgs &args);
JavaScriptProcessor(const JavaScriptProcessor &) = delete;
QJSValue callMain(MediaFileInfo &mediaFileInfo, Diagnostics &diag);
private:
static void addWarnings(Diagnostics &diag, const std::string &context, const QList<QQmlError> &warnings);
const SetTagInfoArgs &args;
int argc;
QCoreApplication app;
Diagnostics diag;
QQmlEngine engine; // not using QJSEngine as otherwise XMLHttpRequest is not available
QJSValue module, main;
UtilityObject *utility;
};
/*!
* \brief Initializes JavaScript processing for the specified \a args.
* \remarks
* - Comes with its own QCoreApplication. Only for use within the CLI parts of the app!
* - Exits the app on fatal errors.
* - Logs status/problems directly in accordance with other parts of the CLI.
*/
JavaScriptProcessor::JavaScriptProcessor(const SetTagInfoArgs &args)
: args(args)
, argc(0)
, app(argc, nullptr)
, utility(new UtilityObject(&engine))
{
// print status message
const auto jsPath = args.jsArg.firstValue();
if (!jsPath) {
return;
}
if (!args.quietArg.isPresent()) {
std::cout << TextAttribute::Bold << "Loading JavaScript file \"" << jsPath << "\" ..." << Phrases::EndFlush;
}
// print warnings later via the usual helper function for consistent formatting
engine.setOutputWarningsToStandardError(false);
QObject::connect(&engine, &QQmlEngine::warnings, &engine,
[this, context = std::string("loading JavaScript")](const auto &warnings) { addWarnings(diag, context, warnings); });
// assign utility object and load specified JavaScript file as module
engine.globalObject().setProperty(QStringLiteral("utility"), engine.newQObject(utility));
module = engine.importModule(QString::fromUtf8(jsPath));
if (module.isError()) {
std::cerr << Phrases::Error << "Unable to load the specified JavaScript file \"" << jsPath << "\":" << Phrases::End;
std::cerr << "Uncaught exception at line " << module.property(QStringLiteral("lineNumber")).toInt() << ':' << ' '
<< module.toString().toStdString() << '\n';
std::exit(EXIT_FAILURE);
}
main = module.property(QStringLiteral("main"));
if (!main.isCallable()) {
std::cerr << Phrases::Error << "The specified JavaScript file \"" << jsPath << "\" does not export a main() function." << Phrases::End;
std::exit(EXIT_FAILURE);
}
// assign settings specified via CLI argument
auto settings = engine.newObject();
if (args.jsSettingsArg.isPresent()) {
for (const auto *const setting : args.jsSettingsArg.values()) {
if (const auto *const value = std::strchr(setting, '=')) {
settings.setProperty(
QString::fromUtf8(setting, static_cast<QString::size_type>(value - setting)), QJSValue(QString::fromUtf8(value + 1)));
} else {
settings.setProperty(QString::fromUtf8(setting), QJSValue());
}
}
}
engine.globalObject().setProperty(QStringLiteral("settings"), settings);
// print warnings
printDiagMessages(diag, "Diagnostic messages:", args.verboseArg.isPresent(), &args.pedanticArg);
diag.clear();
}
/*!
* \brief Calls the JavaScript's main() function for the specified \a mediaFileInfo populating \a diag.
* \returns Returns what the main() function has returned.
*/
QJSValue JavaScriptProcessor::callMain(MediaFileInfo &mediaFileInfo, Diagnostics &diag)
{
auto fileInfoObject = MediaFileInfoObject(mediaFileInfo, diag, &engine, args.quietArg.isPresent());
auto fileInfoObjectValue = engine.newQObject(&fileInfoObject);
auto context = argsToString("executing JavaScript for ", mediaFileInfo.fileName());
utility->setDiag(&context, &diag);
QObject::connect(
&engine, &QQmlEngine::warnings, &fileInfoObject, [&diag, &context](const auto &warnings) { addWarnings(diag, context, warnings); });
diag.emplace_back(DiagLevel::Information, "entering main() function", context);
auto res = main.call(QJSValueList({ fileInfoObjectValue }));
if (res.isError()) {
diag.emplace_back(DiagLevel::Fatal,
argsToString(res.toString().toStdString(), " at line ", res.property(QStringLiteral("lineNumber")).toInt(), '.'), context);
} else if (!res.isUndefined()) {
diag.emplace_back(DiagLevel::Information, argsToString("done with return value: ", res.toString().toStdString()), context);
} else {
diag.emplace_back(DiagLevel::Debug, "done without return value", context);
}
return res;
}
/*!
* \brief Adds the \a warnings to the specified \a diag object with the specified \a context.
*/
void JavaScriptProcessor::addWarnings(Diagnostics &diag, const string &context, const QList<QQmlError> &warnings)
{
for (const auto &warning : warnings) {
diag.emplace_back(DiagLevel::Warning, warning.toString().toStdString(), context);
}
}
#endif
/*!
* \brief Implements the "set"-operation of the CLI.
*/
void setTagInfo(const SetTagInfoArgs &args)
{
CMD_UTILS_START_CONSOLE;
// check whether files have been specified
if (!args.filesArg.isPresent() || args.filesArg.values().empty()) {
std::cerr << Phrases::Error << "No files have been specified." << Phrases::EndFlush;
std::exit(EXIT_FAILURE);
}
if (args.outputFilesArg.isPresent() && args.outputFilesArg.values().size() != args.filesArg.values().size()) {
std::cerr << Phrases::Error << "The number of output files does not match the number of input files." << Phrases::EndFlush;
std::exit(EXIT_FAILURE);
}
// get output files
auto &outputFiles = args.outputFilesArg.isPresent() ? args.outputFilesArg.values() : vector<const char *>();
auto currentOutputFile = outputFiles.cbegin(), noMoreOutputFiles = outputFiles.cend();
// parse field denotations and check whether there's an operation to be done (changing fields or some other settings)
auto fields = parseFieldDenotations(args.valuesArg, false);
if (fields.empty() && (!args.removeTargetArg.isPresent() || args.removeTargetArg.values().empty())
&& (!args.addAttachmentArg.isPresent() || args.addAttachmentArg.values().empty())
&& (!args.updateAttachmentArg.isPresent() || args.updateAttachmentArg.values().empty())
&& (!args.removeAttachmentArg.isPresent() || args.removeAttachmentArg.values().empty())
&& (!args.docTitleArg.isPresent() || args.docTitleArg.values().empty()) && !args.id3v1UsageArg.isPresent() && !args.id3v2UsageArg.isPresent()
&& !args.id3v2VersionArg.isPresent() && !args.jsArg.isPresent()) {
if (!args.layoutOnlyArg.isPresent()) {
std::cerr << Phrases::Error << "No fields/attachments have been specified." << Phrases::End
<< "note: This is usually a mistake. Use --layout-only to prevent this error and apply file layout options only." << endl;
std::exit(EXIT_FAILURE);
}
} else if (args.layoutOnlyArg.isPresent()) {
std::cerr << Phrases::Error << "Fields/attachments and --layout-only have been specified." << Phrases::End
<< "note: Don't use --layout-only if you actually want to alter tag information or attachments." << endl;
std::exit(EXIT_FAILURE);
}
auto settings = TagCreationSettings();
settings.flags = TagCreationFlags::None;
// determine targets to remove
auto targetsToRemove = std::vector<TagTarget>();
for (size_t i = 0, max = args.removeTargetArg.occurrences(); i != max; ++i) {
auto &target = targetsToRemove.emplace_back();
for (const auto &targetDenotation : args.removeTargetArg.values(i)) {
if (!applyTargetConfiguration(target, targetDenotation)) {
std::cerr << Phrases::Error << "The given target specification \"" << targetDenotation << "\" is invalid." << Phrases::EndFlush;
std::exit(EXIT_FAILURE);
}
}
}
// parse ID3v2 version
if (args.id3v2VersionArg.isPresent()) {
try {
settings.id3v2MajorVersion = stringToNumber<std::uint8_t>(args.id3v2VersionArg.values().front());
if (settings.id3v2MajorVersion < 1 || settings.id3v2MajorVersion > 4) {
throw ConversionException();
}
} catch (const ConversionException &) {
settings.id3v2MajorVersion = 3;
std::cerr << Phrases::Error << "The specified ID3v2 version \"" << args.id3v2VersionArg.values().front() << "\" is invalid."
<< Phrases::End << "note: Valid versions are 1, 2, 3 and 4." << endl;
std::exit(EXIT_FAILURE);
}
}
// parse flags
if (args.treatUnknownFilesAsMp3FilesArg.isPresent()) {
settings.flags |= TagCreationFlags::TreatUnknownFilesAsMp3Files;
}
if (args.id3InitOnCreateArg.isPresent()) {
settings.flags |= TagCreationFlags::Id3InitOnCreate;
}
if (args.id3TransferOnRemovalArg.isPresent()) {
settings.flags |= TagCreationFlags::Id3TransferValuesOnRemoval;
}
if (args.mergeMultipleSuccessiveTagsArg.isPresent()) {
settings.flags |= TagCreationFlags::MergeMultipleSuccessiveId3v2Tags;
}
if (!args.id3v2VersionArg.isPresent()) {
settings.flags |= TagCreationFlags::KeepExistingId3v2Version;
}
// parse other settings
const auto denotedEncoding = parseEncodingDenotation(args.encodingArg, TagTextEncoding::Utf8);
settings.id3v1usage = parseUsageDenotation(args.id3v1UsageArg, TagUsage::KeepExisting);
settings.id3v2usage = parseUsageDenotation(args.id3v2UsageArg, TagUsage::Always);
// setup media file info
auto fileInfo = MediaFileInfo();
auto tags = std::vector<Tag *>();
fileInfo.setMinPadding(parseUInt64(args.minPaddingArg, 0));
fileInfo.setMaxPadding(parseUInt64(args.maxPaddingArg, 0));
fileInfo.setPreferredPadding(parseUInt64(args.prefPaddingArg, 0));
fileInfo.setTagPosition(parsePositionDenotation(args.tagPosArg, args.tagPosValueArg, ElementPosition::BeforeData));
fileInfo.setForceTagPosition(args.forceTagPosArg.isPresent());
fileInfo.setIndexPosition(parsePositionDenotation(args.indexPosArg, args.indexPosValueArg, ElementPosition::BeforeData));
fileInfo.setForceIndexPosition(args.forceIndexPosArg.isPresent());
fileInfo.setForceRewrite(args.forceRewriteArg.isPresent());
fileInfo.setWritingApplication(APP_NAME " v" APP_VERSION);
if (args.preserveMuxingAppArg.isPresent()) {
fileInfo.setFileHandlingFlags(fileInfo.fileHandlingFlags() | MediaFileHandlingFlags::PreserveMuxingApplication);
}
if (args.preserveWritingAppArg.isPresent()) {
fileInfo.setFileHandlingFlags(fileInfo.fileHandlingFlags() | MediaFileHandlingFlags::PreserveWritingApplication);
}
if (!args.preserveTotalFieldsArg.isPresent()) {
fileInfo.setFileHandlingFlags(fileInfo.fileHandlingFlags() | MediaFileHandlingFlags::ConvertTotalFields);
}
// set backup path
if (args.backupDirArg.isPresent()) {
fileInfo.setBackupDirectory(std::string(args.backupDirArg.values().front()));
}
// initialize JavaScript processing if --java-script argument is present
#ifdef TAGEDITOR_USE_JSENGINE
auto js = args.jsArg.isPresent() ? std::make_unique<JavaScriptProcessor>(args) : std::unique_ptr<JavaScriptProcessor>();
#else
if (args.jsArg.isPresent()) {
std::cerr << Phrases::Error << "A JavaScript has been specified but support for this has been disabled at compile-time." << Phrases::EndFlush;
std::exit(EXIT_FAILURE);
}
#endif
// iterate through all specified files
const auto quiet = args.quietArg.isPresent();
auto fileIndex = 0u;
static auto context = std::string("setting tags");
const auto continueWithNextFile = [&](Diagnostics &diag) {
printDiagMessages(diag, "Diagnostic messages:", args.verboseArg.isPresent(), &args.pedanticArg);
++fileIndex;
if (currentOutputFile != noMoreOutputFiles) {
++currentOutputFile;
}
};
for (const char *file : args.filesArg.values()) {
auto diag = Diagnostics();
auto parsingProgress = AbortableProgressFeedback(); // FIXME: actually use the progress object
try {
// parse tags and tracks (tracks are relevant because track meta-data such as language can be changed as well)
if (!quiet) {
cout << TextAttribute::Bold << "Setting tag information for \"" << file << "\" ..." << Phrases::EndFlush;
}
fileInfo.setPath(std::string(file));
fileInfo.parseContainerFormat(diag, parsingProgress);
fileInfo.parseTags(diag, parsingProgress);
fileInfo.parseTracks(diag, parsingProgress);
fileInfo.parseAttachments(diag, parsingProgress);
// remove tags with the specified targets
if (!targetsToRemove.empty()) {
tags.clear();
fileInfo.tags(tags);
for (auto *const tag : tags) {
if (find(targetsToRemove.cbegin(), targetsToRemove.cend(), tag->target()) != targetsToRemove.cend()) {
fileInfo.removeTag(tag);
}
}
}
// select the relevant values for the current file index
for (auto &fieldDenotation : fields) {
FieldValues &denotedValues = fieldDenotation.second;
std::vector<FieldValue *> &relevantDenotedValues = denotedValues.relevantValues;
relevantDenotedValues.clear();
unsigned int currentFileIndex = 0;
for (FieldValue &denotatedValue : denotedValues.allValues) {
if ((denotatedValue.fileIndex <= fileIndex)
&& (relevantDenotedValues.empty() || (denotatedValue.fileIndex >= currentFileIndex))) {
if (currentFileIndex != denotatedValue.fileIndex) {
currentFileIndex = denotatedValue.fileIndex;
relevantDenotedValues.clear();
}
relevantDenotedValues.push_back(&denotatedValue);
}
}
}
// determine required targets
settings.requiredTargets.clear();
for (const auto &fieldDenotation : fields) {
const FieldScope &scope = fieldDenotation.first;
if (scope.isTrack() || !scope.exactTargetMatching) {
continue;
}
const std::vector<FieldValue *> &relevantDenotedValues = fieldDenotation.second.relevantValues;
auto hasNonEmptyValues = false;
for (const auto &value : relevantDenotedValues) {
if (!value->value.empty()) {
hasNonEmptyValues = true;
break;
}
}
if (hasNonEmptyValues
&& std::find(settings.requiredTargets.cbegin(), settings.requiredTargets.cend(), scope.tagTarget)
== settings.requiredTargets.cend()) {
settings.requiredTargets.emplace_back(scope.tagTarget);
}
}
// create new tags according to settings
fileInfo.createAppropriateTags(settings);
auto container = fileInfo.container();
if (args.docTitleArg.isPresent() && !args.docTitleArg.values().empty()) {
if (container && container->supportsTitle()) {
size_t segmentIndex = 0, segmentCount = container->titles().size();
for (const auto &newTitle : args.docTitleArg.values()) {
if (segmentIndex < segmentCount) {
container->setTitle(newTitle, segmentIndex);
} else {
diag.emplace_back(DiagLevel::Warning,
argsToString(
"The specified document title \"", newTitle, "\" can not be set because the file has not that many segments."),
context);
}
++segmentIndex;
}
} else {
diag.emplace_back(DiagLevel::Warning, "Setting the document title is not supported for the file.", context);
}
}
// process tag fields via the specified JavaScript
#ifdef TAGEDITOR_USE_JSENGINE
if (js) {
const auto res = js->callMain(fileInfo, diag);
if (res.isError() || diag.has(DiagLevel::Fatal)) {
if (!quiet) {
std::cout << " - Skipping file due to fatal error when executing JavaScript.\n";
}
continueWithNextFile(diag);
continue;
}
if (!res.isUndefined() && !res.toBool()) {
if (!quiet) {
std::cout << " - Skipping file because JavaScript returned a falsy value other than undefined.\n";
}
continueWithNextFile(diag);
continue;
}
}
#endif
// alter tags
tags.clear();
fileInfo.tags(tags);
if (tags.empty()) {
diag.emplace_back(DiagLevel::Critical, "Can not create appropriate tags for file.", context);
} else {
// iterate through all tags
const auto willWriteAnId3v2Tag = fileInfo.hasId3v2Tag();
for (auto *tag : tags) {
// clear current values if option is present
if (args.removeOtherFieldsArg.isPresent()) {
tag->removeAllFields();
}
// determine required information for deciding whether specified values match the scope of the current tag
const auto tagType = tag->type();
const bool targetSupported = tag->supportsTarget();
const auto tagTarget = tag->target();
// determine the encoding to store text values
TagTextEncoding usedEncoding = denotedEncoding;
if (!tag->canEncodingBeUsed(denotedEncoding)) {
usedEncoding = tag->proposedTextEncoding();
if (args.encodingArg.isPresent()) {
diag.emplace_back(DiagLevel::Warning,
argsToString("Can't use specified encoding \"", args.encodingArg.values().front(), "\" in ", tagName(tag),
" because the tag format/version doesn't support it."),
context);
}
}
// iterate through all denoted field values
for (const auto &fieldDenotation : fields) {
const FieldScope &denotedScope = fieldDenotation.first;
// skip values which scope does not match the current tag
if (denotedScope.isTrack() || !(denotedScope.tagType == TagType::Unspecified || (denotedScope.tagType & tagType))
|| !(!targetSupported || (tagType == TagType::OggVorbisComment && denotedScope.tagTarget.isEmpty())
|| (denotedScope.exactTargetMatching ? denotedScope.tagTarget == tagTarget
: denotedScope.tagTarget.matches(tagTarget)))) {
continue;
}
// convert the values to TagValue
auto convertedValues = std::vector<TagValue>();
auto convertedId3v2CoverValues = std::vector<Id3v2Cover>();
for (const FieldValue *relevantDenotedValue : fieldDenotation.second.relevantValues) {
// assign an empty TagValue to remove the field if denoted value is empty
if (relevantDenotedValue->value.empty()) {
convertedValues.emplace_back();
continue;
}
// add text value
if (relevantDenotedValue->type != DenotationType::File) {
try {
convertedValues.emplace_back(relevantDenotedValue->value, TagTextEncoding::Utf8, usedEncoding);
} catch (const ConversionException &e) {
diag.emplace_back(DiagLevel::Critical,
argsToString("Unable to parse value specified for field \"", denotedScope.field.name(), "\": ", e.what()),
context);
}
continue;
}
// add value from file
const auto denotedValue = relevantDenotedValue->value;
const auto firstPartIsDriveLetter = denotedValue.size() >= 2 && denotedValue[1] == ':' ? 1u : 0u;
const auto maxParts = std::size_t(3u + firstPartIsDriveLetter);
const auto parts = splitStringSimple<std::vector<std::string_view>>(
denotedValue, args.coverTypeDelimiterArg.firstValueOr(":"), static_cast<int>(maxParts));
const auto path = parts.empty()
? std::string_view()
: (firstPartIsDriveLetter ? std::string_view(denotedValue.data(), parts[0].size() + parts[1].size() + 1)
: parts.front());
const auto fieldType = denotedScope.field.knownFieldForTag(tag, tagType);
const auto dataType = fieldType == KnownField::Cover ? TagDataType::Picture : TagDataType::Text;
try {
// assume the file refers to a picture
auto value = TagValue();
if (!path.empty()) {
auto coverFileInfo = MediaFileInfo(path);
auto coverDiag = Diagnostics();
auto coverProgress = AbortableProgressFeedback(); // FIXME: actually use the progress object
coverFileInfo.open(true);
coverFileInfo.parseContainerFormat(coverDiag, coverProgress);
auto buff = make_unique<char[]>(coverFileInfo.size());
coverFileInfo.stream().seekg(static_cast<streamoff>(0));
coverFileInfo.stream().read(buff.get(), static_cast<streamoff>(coverFileInfo.size()));
value = TagValue(std::move(buff), coverFileInfo.size(), dataType, TagTextEncoding::Utf8);
value.setMimeType(coverFileInfo.mimeType());
}
auto description = std::optional<std::string_view>();
if (parts.size() > 2u + firstPartIsDriveLetter) {
description = parts[2 + firstPartIsDriveLetter];
value.setDescription(description.value(), TagTextEncoding::Utf8);
}
if (parts.size() > 1u + firstPartIsDriveLetter && fieldType == KnownField::Cover
&& (tagType == TagType::Id3v2Tag || tagType == TagType::VorbisComment || tagType == TagType::OggVorbisComment)) {
const auto typeSpec = parts[1 + firstPartIsDriveLetter];
const auto coverType = id3v2CoverType(typeSpec);
if (coverType == invalidCoverType) {
diag.emplace_back(DiagLevel::Warning,
argsToString("Specified cover type \"", typeSpec, "\" is invalid. Ignoring the specified field/value."),
context);
} else {
convertedId3v2CoverValues.emplace_back(std::move(value), coverType, description);
}
} else {
if (parts.size() > 1u + firstPartIsDriveLetter) {
diag.emplace_back(
tag->type() == TagType::Id3v1Tag && fileInfo.hasId3v2Tag() ? DiagLevel::Information : DiagLevel::Warning,
argsToString("Ignoring cover type \"", parts[1 + firstPartIsDriveLetter], "\" for ", tag->typeName(),
". It is only supported by the cover field and the tag formats ID3v2 and Vorbis Comment."),
context);
}
convertedValues.emplace_back(std::move(value));
}
} catch (const TagParser::Failure &) {
diag.emplace_back(DiagLevel::Critical,
argsToString("Unable to parse specified file \"", path, "\": unable to determine MIME type"), context);
} catch (const ConversionException &e) {
diag.emplace_back(
DiagLevel::Critical, argsToString("Unable to parse specified file \"", path, "\": ", e.what()), context);
} catch (const std::ios_base::failure &e) {
diag.emplace_back(DiagLevel::Critical,
argsToString("An IO error occurred when parsing the specified file \"", path, "\": ", e.what()), context);
}
}
// finally set/clear the values
try {
// add error if field is not supported unless it is just ID3v1 and we are writing an ID3v2 tag as well
if ((!convertedValues.empty() || convertedId3v2CoverValues.empty())
&& !denotedScope.field.setValues(tag, tagType, convertedValues)
&& (tagType != TagType::Id3v1Tag || !willWriteAnId3v2Tag)) {
diag.emplace_back(DiagLevel::Critical,
argsToString(
"Unable set field \"", denotedScope.field.name(), "\": field is not supported for ", tag->typeName()),
context);
}
} catch (const ConversionException &e) {
diag.emplace_back(DiagLevel::Critical,
argsToString("Unable to parse denoted field ID \"", denotedScope.field.name(), "\": ", e.what()), context);
}
if (!convertedId3v2CoverValues.empty()) {
switch (tagType) {
case TagType::Id3v2Tag:
setId3v2CoverValues(static_cast<Id3v2Tag *>(tag), std::move(convertedId3v2CoverValues));
break;
case TagType::VorbisComment:
case TagType::OggVorbisComment:
setId3v2CoverValues(static_cast<VorbisComment *>(tag), std::move(convertedId3v2CoverValues));
break;
default:;
}