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
#include "metadataparseravconv.h"
#include <QProcess>
#include <QDebug>
// INSANE DATA PARSING FUCK YEAH (that means fuck no)
MetaDataParserAvconv::MetaDataParserAvconv(const AvconvConfig& config) : config(config)
{
}
MetaData MetaDataParserAvconv::parse(QString filename) const {
QProcess process;
process.start(config.command, QStringList() << "-i" << filename);
process.waitForFinished(); // TODO use signals
// read error, because stdout is reserved for piping conversion output data
QString output(process.readAllStandardError());
MetaData m;
// Example output:
//$ avconv -i Higurashi\ no\ Naku\ Koro\ ni\ 1x01.mkv
// avconv version 0.8.6-6:0.8.6-1ubuntu2, Copyright (c) 2000-2013 the Libav developers
// built on Mar 30 2013 22:20:06 with gcc 4.7.2
// [matroska,webm @ 0x112fa20] Estimating duration from bitrate, this may be inaccurate
// Input #0, matroska,webm, from 'Higurashi no Naku Koro ni 1x01.mkv':
// Duration: 00:23:42.04, start: 0.000000, bitrate: N/A
// Chapter #0.0: start 0.105000, end 1422.047000
// Metadata:
// title : 00:00:00.105
// Stream #0.0(eng): Video: h264 (High 10), yuv420p10le, 720x480 [PAR 853:720 DAR 853:480], PAR 186:157 DAR 279:157, 23.98 tbr, 1k tbn, 1073741824 tbc (default)
// Stream #0.1(jpn): Subtitle: [0][0][0][0] / 0x0000 (default)
// Stream #0.2: Audio: aac, 44100 Hz, stereo, s16 (default)
// At least one output file must be specified
m.duration = parseDuration(output);
m.tracks = parseTracks(output);
m.chapters = parseChapters(output);
return m;
}
int MetaDataParserAvconv::parseDuration(QString durationString) const {
QRegExp durationRegex("\\sDuration:\\s([0-9]*):([0-9]*):([0-9]*).([0-9]*),");
durationRegex.setMinimal(true);
bool foundAnything = -1 != durationString.indexOf(durationRegex);
if (foundAnything) {
int hours = durationRegex.cap(1).toInt();
int minutes = durationRegex.cap(2).toInt();
int seconds = durationRegex.cap(3).toInt();
//if (durationRegex.captureCount() == 4) {
// int ms = durationRegex.cap(4).toInt(); // ignore
//}
return (hours * 60 * 60) + (minutes * 60) + seconds;
}
return -1;
}
QList<MetaDataTrack> MetaDataParserAvconv::parseTracks(QString outputString) const {
QRegExp streamRegex("\\sStream \\#([0-9\\.]*)(\\((.*)\\))?: (.*):");
streamRegex.setMinimal(true);
QList<MetaDataTrack> l;
int latestIndex = outputString.indexOf(streamRegex);
while (-1 != latestIndex) {
int nextIndex = outputString.indexOf(streamRegex, latestIndex+1);
latestIndex = outputString.indexOf(streamRegex, latestIndex); // stupid repetition to gain back the captures
MetaDataTrack t;
t.id = streamRegex.cap(1).toFloat();
t.name = streamRegex.cap(3); // skip 2 as it's the optional name with the brackets
QString typeString = streamRegex.cap(4);
QString detailsString = outputString.mid(latestIndex + streamRegex.cap(0).length(), nextIndex != -1 ? (outputString.length() - (nextIndex-latestIndex)) : 0);
if (typeString == "Video") {
t.type = video;
//qDebug() << detailsString;
QRegExp videoDetailsRegex("([0-9]+)x([0-9]+).");
if (-1 != detailsString.indexOf(videoDetailsRegex)) {
t.track.video.resolutionX = videoDetailsRegex.cap(1).toInt();
t.track.video.resolutionY = videoDetailsRegex.cap(2).toInt();
} else {
t.track.video.resolutionX = 0;
t.track.video.resolutionY = 0;
}
} else if (typeString == "Audio") { t.type = audio; }
else if (typeString == "Subtitle") { t.type = subtitle; }
else if (typeString == "Attachment") { t.type = attachment; }
l.append(t);
latestIndex = nextIndex;
}
return l;
}
QList<MetaDataChapter> MetaDataParserAvconv::parseChapters(QString outputString) const {
QList<MetaDataChapter> chapters;
QRegExp chapterHeadRegex("\\sChapter \\#([0-9\\.]*)(\\((.*)\\))?: start ([0-9\\.]*), end ([0-9\\.]*)");
int chapterHead = outputString.indexOf(chapterHeadRegex);
while (-1 != chapterHead) {
int nextIndex = outputString.indexOf(chapterHeadRegex, chapterHead+1);
outputString.indexOf(chapterHeadRegex, chapterHead);
MetaDataChapter c;
c.start = chapterHeadRegex.cap(4).toFloat();
c.end = chapterHeadRegex.cap(5).toFloat();
QRegExp chapterTitleRegex("Metadata:\n(\\s*)title(\\s*):\\s([^\n]*)\n");
int chapterTitle = outputString.indexOf(chapterTitleRegex, chapterHead);
if (-1 != chapterTitle && (nextIndex == -1 || chapterTitle < nextIndex)) {
c.title = chapterTitleRegex.cap(3);
}
chapters.append(c);
chapterHead = nextIndex;
}
return chapters;
}