File: transcodingjob.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (155 lines) | stat: -rw-r--r-- 4,099 bytes parent folder | download
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
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 */

#include "transcodingjob.h"
#include "device.h"
#include <QRegularExpression>
#include <QStringList>

TranscodingJob::TranscodingJob(const Encoders::Encoder& enc, int val, const QString& src, const QString& dest, const DeviceOptions& d, int co, const Song& s)
	: CopyJob(src, dest, d, co, s), encoder(enc), value(val), process(nullptr), duration(-1)
{
}

TranscodingJob::~TranscodingJob()
{
	delete process;
}

void TranscodingJob::run()
{
	QString src(updateTagsLocal());
	if (src.isEmpty()) {
		return;
	}

	if (stopRequested) {
		emit result(Device::Cancelled);
	}
	else {
		QStringList parameters = encoder.params(value, src, destFile);
		process = new QProcess;
		process->setProcessChannelMode(QProcess::MergedChannels);
		process->setReadChannel(QProcess::StandardOutput);
		connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput()));
		connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
		QString cmd = parameters.takeFirst();
		process->start(cmd, parameters);
	}
}

void TranscodingJob::stop()
{
	if (process) {
		process->close();
		process->deleteLater();
		process = nullptr;
		emit result(Device::Cancelled);
	}
}

void TranscodingJob::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
	Q_UNUSED(exitStatus)
	if (!process) {
		return;
	}
	if (stopRequested) {
		emit result(Device::Cancelled);
		return;
	}
	if (0 == exitCode) {
		updateTagsDest();
		copyCover(srcFile);
	}
	emit result(0 == exitCode ? Device::Ok : Device::TranscodeFailed);
}

void TranscodingJob::processOutput()
{
	if (stopRequested) {
		emit result(Device::Cancelled);
		return;
	}
	QString output = process->readAllStandardOutput().data();
	if (output.simplified().isEmpty()) {
		return;
	}

	if (!data.isEmpty()) {
		output = data + output;
	}
	if (-1 == duration) {
		duration = computeDuration(output);
	}

	if (duration > 0) {
		qint64 prog = computeProgress(output);
		if (prog > -1) {
			setPercent((prog * 100) / duration);
		}
	}

	if (!output.endsWith('\n') && !output.endsWith('\r')) {
		int last = output.lastIndexOf('\n');
		if (-1 == last) {
			last = output.lastIndexOf('\r');
		}
		if (last > -1) {
			data = output.mid(last + 1);
		}
		else {
			data = output;
		}
	}
}

inline qint64 TranscodingJob::computeDuration(const QString& output)
{
	//We match something like "Duration: 00:04:33.60"
	static const QRegularExpression durationRegexp("Duration: (\\d{2,}):(\\d{2}):(\\d{2})\\.(\\d{2})");
	QRegularExpressionMatch match;
	if (output.contains(durationRegexp, &match)) {
		//duration is in csec
		return match.captured(1).toLong() * 60 * 60 * 100 + match.captured(2).toInt() * 60 * 100 + match.captured(3).toInt() * 100 + match.captured(4).toInt();
	}
	else {
		return -1;
	}
}

inline qint64 TranscodingJob::computeProgress(const QString& output)
{
	//Output is like size=     323kB time=18.10 bitrate= 146.0kbits/s
	//We're going to use the "time" column, which counts the elapsed time in seconds.
	static const QRegularExpression timeRegexp("time=(\\d+)\\.(\\d{2})");
	QRegularExpressionMatch match;

	if (output.contains(timeRegexp, &match)) {
		return match.captured(1).toLong() * 100 + match.captured(2).toInt();
	}
	else {
		return -1;
	}
}

#include "moc_transcodingjob.cpp"