-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcffmpeg.cpp
205 lines (176 loc) · 6.02 KB
/
cffmpeg.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
/**
* Copyright (C) 2021 Jo2003 (olenka.joerg@gmail.com)
* This file is part of cd2netmd_gui
*
* cd2netmd 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 3 of the License, or
* (at your option) any later version.
*
* cd2netmd 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
*/
#include "cffmpeg.h"
#include "helpers.h"
#include <QApplication>
#include <audio.h>
CFFMpeg::CFFMpeg(QObject *parent)
: CCliProcess(parent)
{
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &CFFMpeg::finishCopy);
}
//--------------------------------------------------------------------------
//! @brief start encoder
//!
//! @param[in] srcFileName The source file name
//! @param[in] trgFileName The target file name
//! @param[in] conversion The conversion settings
//!
//! @return 0 on success
//--------------------------------------------------------------------------
int CFFMpeg::start(const QString& srcFileName, const QString trgFileName, const uint32_t& conversion)
{
QStringList params;
mLog.clear();
params << "-y" << "-i" << srcFileName;
if (conversion & audio::CONV_BPS)
{
params << "-acodec" << "pcm_s16le";
}
if (conversion & audio::CONV_SAMPLERATE)
{
params << "-ar" << "44100";
}
if (conversion & audio::CONV_CHANNELS)
{
params << "-ac" << "2";
}
params << "-f" << "wav" << "-map_metadata" << "-1" << trgFileName;
return start(params);
}
//--------------------------------------------------------------------------
//! @brief start encoder with params
//!
//! @param[in] params the call parameters
//! @param[in] nativeArgs optional native arguments
//!
//! @return 0 on success
//--------------------------------------------------------------------------
int CFFMpeg::start(const QStringList& params, const QString& nativeArgs)
{
#ifdef Q_OS_MAC
// app folder
QString sAppDir = QApplication::applicationDirPath();
// find bundle dir ...
QRegExp rx("^(.*)/MacOS");
if (rx.indexIn(sAppDir) > -1)
{
// found section --> create path names ...
QString encTool = QString("%1/%2").arg(sAppDir).arg(FFMPEG_CLI);
qInfo() << encTool << params;
run(encTool, params, ReadWrite, nativeArgs);
}
#elif defined Q_OS_LINUX
QString ffmpeg = QFile::exists(QString("/usr/bin/%1").arg(FFMPEG_CLI)) ? FFMPEG_CLI : "ffmpeg";
qInfo() << ffmpeg << params << nativeArgs;
run(ffmpeg, params, ReadWrite, nativeArgs);
#else
qInfo() << FFMPEG_CLI << params << nativeArgs;
run(FFMPEG_CLI, params, ReadWrite, nativeArgs);
#endif
return 0;
}
//--------------------------------------------------------------------------
//! @brief concat audio files to supported wave file
//!
//! @param[in] sources The source file names
//! @param[in] target The target file name
//!
//! @return 0 on success
//--------------------------------------------------------------------------
int CFFMpeg::concatFiles(const QStringList& sources, const QString& target)
{
// ffmpeg -i input1.wav -i input2.wav -i input3.wav -i input4.wav
// -filter_complex '[0:0][1:0][2:0][3:0]concat=n=4:v=0:a=1[out]'
// -map '[out]' output.wav
const char* concatTmpl1 = "[%1:0]";
#ifdef Q_OS_WIN
const char* concatTmpl2 = R"("%1concat=n=%2:v=0:a=1[out]")";
#else
const char* concatTmpl2 = "%1concat=n=%2:v=0:a=1[out]";
#endif
QString concatComplex;
int i = 0;
QStringList params;
for(const auto& s : sources)
{
concatComplex += QString(concatTmpl1).arg(i);
params << "-i" << s;
i++;
}
// finish complex filter string
concatComplex = QString(concatTmpl2).arg(concatComplex).arg(sources.size());
// convert to wave, 44.1kHz, 16 bit
params << "-y"
<< "-acodec" << "pcm_s16le"
<< "-ar" << "44100"
<< "-ac" << "2"
<< "-filter_complex";
#ifdef Q_OS_WIN
concatComplex += R"( -map "[out]" -f wav -map_metadata -1 ")" + target + R"(")";
#else
params << concatComplex << "-map" << "[out]" << "-f" << "wav" << "-map_metadata" << "-1" << target;
concatComplex = "";
#endif
return start(params, concatComplex);
}
void CFFMpeg::finishCopy(int exitCode, ExitStatus exitStatus)
{
if ((exitCode == 0) && (exitStatus == ExitStatus::NormalExit))
{
qInfo() << "File successfully decoded!";
}
mLog += QString::fromUtf8(readAllStandardOutput());
if (!mLog.isEmpty())
{
qDebug().noquote() << Qt::endl << static_cast<const char*>(mLog.toUtf8());
}
emit fileDone();
}
//--------------------------------------------------------------------------
//! @brief extract percentage from log file
//--------------------------------------------------------------------------
void CFFMpeg::extractPercent()
{
mLog += QString::fromUtf8(readAllStandardOutput());
int length = 0, currPos = 0, from = 0;
QRegExp rxDuration("Duration:\\s+([0-9]+):([0-9]+):([0-9]+)");
QRegExp rxPosition("time=([0-9]+):([0-9]+):([0-9]+)");
while ((from = mLog.indexOf(rxDuration, from)) > -1)
{
from += rxDuration.matchedLength();
// hours
length += rxDuration.cap(1).toInt() * 3600;
// minutes
length += rxDuration.cap(2).toInt() * 60;
// seconds
length += rxDuration.cap(3).toInt();
}
if (mLog.lastIndexOf(rxPosition) > -1)
{
// hours
currPos += rxPosition.cap(1).toInt() * 3600;
// minutes
currPos += rxPosition.cap(2).toInt() * 60;
// seconds
currPos += rxPosition.cap(3).toInt();
}
if ((currPos != 0) && (length != 0))
{
emit progress((currPos * 100) / length);
}
}