-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegthreads.cpp
More file actions
627 lines (531 loc) · 18.6 KB
/
ffmpegthreads.cpp
File metadata and controls
627 lines (531 loc) · 18.6 KB
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
#include "ffmpegthreads.h"
#include <QDebug>
#include <QRegularExpression>
#include <QFile>
#include <QStandardPaths>
#include <QRandomGenerator>
ffmpegThreads::ffmpegThreads(QObject *parent): QObject{parent}
{
tcpServer = nullptr;
clientSocket = nullptr;
socketPort = "0";
// 使用 Qt::DirectConnection 确保在正确的线程中执行
connect(&FFMPEG_Process, &QProcess::finished, this, [this]() {
FFMPEG_Process.close();
}, Qt::DirectConnection);
connect(&FFMPEG_Process, &QProcess::readyReadStandardOutput, this, &ffmpegThreads::ffmpeg_Process);
connect(&FFMPEG_Process, &QProcess::readyReadStandardError, this, &ffmpegThreads::ffmpeg_Process);
}
QObject *ffmpegThreads::getInstance()
{
static ffmpegThreads * obj = new ffmpegThreads();
return obj;
}
QString ffmpegThreads::getFfmpeg_Version() const
{
return ffmpeg_Version;
}
void ffmpegThreads::setFfmpeg_Version(const QString &newFfmpeg_Version)
{
if (ffmpeg_Version == newFfmpeg_Version)
return;
ffmpeg_Version = newFfmpeg_Version;
emit ffmpeg_VersionChanged();
}
//参数sourceCommand定义;
QString ffmpegThreads::getSourceCommand() const
{
return sourceCommand;
}
void ffmpegThreads::setSourceCommand(const QString &newSourceCommand)
{
if (sourceCommand == newSourceCommand)
return;
sourceCommand = newSourceCommand;
emit sourceCommandChanged();
}
QString ffmpegThreads::getVideoFrameRate() const
{
return videoFrameRate;
}
void ffmpegThreads::setVideoFrameRate(const QString &newVideoFrameRate)
{
if (videoFrameRate == newVideoFrameRate)
return;
videoFrameRate = newVideoFrameRate;
emit videoFrameRateChanged();
}
QString ffmpegThreads::getSourceMusicCodec() const
{
return sourceMusicCodec;
}
void ffmpegThreads::setSourceMusicCodec(const QString &newSourceMusicCodec)
{
if (sourceMusicCodec == newSourceMusicCodec)
return;
sourceMusicCodec = newSourceMusicCodec;
emit sourceMusicCodecChanged();
}
QString ffmpegThreads::getSourceVideoCodec() const
{
return sourceVideoCodec;
}
void ffmpegThreads::setSourceVideoCodec(const QString &newSourceVideoCodec)
{
if (sourceVideoCodec == newSourceVideoCodec)
return;
sourceVideoCodec = newSourceVideoCodec;
emit sourceVideoCodecChanged();
}
QString ffmpegThreads::getSourceVideoHeight() const
{
return sourceVideoHeight;
}
void ffmpegThreads::setSourceVideoHeight(const QString &newSourceVideoHeight)
{
if (sourceVideoHeight == newSourceVideoHeight)
return;
sourceVideoHeight = newSourceVideoHeight;
emit sourceVideoHeightChanged();
}
QString ffmpegThreads::getSourceVideoWidth() const
{
return sourceVideoWidth;
}
void ffmpegThreads::setSourceVideoWidth(const QString &newSourceVideoWidth)
{
if (sourceVideoWidth == newSourceVideoWidth)
return;
sourceVideoWidth = newSourceVideoWidth;
emit sourceVideoWidthChanged();
}
//参数delFileUrl定义;
QString ffmpegThreads::getDelFileUrl() const
{
return delFileUrl;
}
void ffmpegThreads::setDelFileUrl(const QString &newDelFileUrl)
{
if (delFileUrl == newDelFileUrl)
return;
delFileUrl = newDelFileUrl;
emit delFileUrlChanged();
}
//参数videoFileUrl定义;
QString ffmpegThreads::getVideoFileUrl() const
{
return videoFileUrl;
}
void ffmpegThreads::setVideoFileUrl(const QString &newVidepFileUrl)
{
if (videoFileUrl == newVidepFileUrl)
return;
videoFileUrl = newVidepFileUrl;
emit videoFileUrlChanged();
}
QString ffmpegThreads::getUnusedPort()
{
// 尝试多个端口,直到找到可用的
for (int i = 0; i < 10; i++) {
int port = QRandomGenerator::global()->bounded(10000, 60000);
// 检查端口是否可用
QTcpServer testServer;
if (testServer.listen(QHostAddress::LocalHost, port)) {
testServer.close();
setSocketPort(QString::number(port));
return QString::number(port);
}
}
// 如果找不到可用端口,使用默认值
qWarning() << "Could not find available port, using default 19083";
setSocketPort("19083");
return "19083";
}
QString ffmpegThreads::getSourceCommandPreview() const
{
return sourceCommandPreview;
}
void ffmpegThreads::setSourceCommandPreview(const QString &newSourceCommandPreview)
{
if (sourceCommandPreview == newSourceCommandPreview)
return;
sourceCommandPreview = newSourceCommandPreview;
emit sourceCommandPreviewChanged();
}
void ffmpegThreads::mergesubMessage(const QString &title, const QString &body, const QString &icon, int timeout)
{
QDBusConnection mergeSub = QDBusConnection::sessionBus();
if(!mergeSub.isConnected()){
qWarning() << "无法连接到D-Bus会话总线:"<< mergeSub.lastError().message();
}
mergeSub_Message = QDBusMessage::createMethodCall(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify"
);
QString appName = "MergeSub";
uint replacesId = 0;
QVariantMap hints;
QStringList actions;
mergeSub_Message << appName
<< replacesId
<< icon
<< title
<< body
<< actions
<< hints
<< timeout;
QDBusMessage reply = mergeSub.call(mergeSub_Message);
if (reply.type() == QDBusMessage::ErrorMessage) {
qWarning() << "D-Bus 调用失败:" << reply.errorName() << reply.errorMessage();
}
// 处理返回值(通知ID),如果需要的话
if (reply.arguments().count() > 0) {
quint32 notificationId = reply.arguments().at(0).toUInt();
qDebug() << "通知发送成功,ID:" << notificationId;
} else {
qDebug() << "通知发送成功(未返回ID)。";
}
}
bool ffmpegThreads::startTcpServer()
{
if (tcpServer) {
tcpServer->close();
delete tcpServer;
tcpServer = nullptr;
}
tcpServer = new QTcpServer(this);
// 连接新连接信号
connect(tcpServer, &QTcpServer::newConnection, this, &ffmpegThreads::onNewConnection);
// 尝试监听指定端口
if (!tcpServer->listen(QHostAddress::LocalHost, socketPort.toInt())) {
QString error = tcpServer->errorString();
qDebug() << "Failed to start TCP server on port" << socketPort << ":" << error;
emit tcpServerStarted(false, error);
// 尝试使用备用端口
if (socketPort != "19083") {
qDebug() << "Trying fallback port 19083";
setSocketPort("19083");
return startTcpServer();
}
return false;
}
qDebug() << "TCP server started on port:" << socketPort;
emit tcpServerStarted(true);
return true;
}
QString ffmpegThreads::getVideo_Current_Duration() const
{
return video_Current_Duration;
}
void ffmpegThreads::setVideo_Current_Duration(const QString &newVideo_Current_Duration)
{
if (video_Current_Duration == newVideo_Current_Duration)
return;
video_Current_Duration = newVideo_Current_Duration;
emit video_Current_DurationChanged();
}
void ffmpegThreads::onNewConnection()
{
if (clientSocket) {
// 已有连接,拒绝新连接
QTcpSocket *newSocket = tcpServer->nextPendingConnection();
newSocket->close();
newSocket->deleteLater();
return;
}
clientSocket = tcpServer->nextPendingConnection();
connect(clientSocket, &QTcpSocket::disconnected, this, &ffmpegThreads::onClientDisconnected);
qDebug() << "Client connected to TCP server";
emit clientConnected();
// 立即开始读取和转发FFmpeg输出(如果FFmpeg已经在运行)
if (FFMPEG_Process.state() == QProcess::Running) {
// 读取并发送所有已缓冲的输出
QByteArray data = FFMPEG_Process.readAllStandardOutput();
if (!data.isEmpty() && clientSocket->state() == QAbstractSocket::ConnectedState) {
clientSocket->write(data);
clientSocket->flush();
}
data = FFMPEG_Process.readAllStandardError();
if (!data.isEmpty() && clientSocket->state() == QAbstractSocket::ConnectedState) {
clientSocket->write(data);
clientSocket->flush();
}
}
}
void ffmpegThreads::onClientDisconnected()
{
if (clientSocket) {
clientSocket->deleteLater();
clientSocket = nullptr;
qDebug() << "Client disconnected from TCP server";
emit clientDisconnected();
}
}
QString ffmpegThreads::getSocketPort() const
{
return socketPort;
}
void ffmpegThreads::setSocketPort(const QString newSocketPort)
{
if (socketPort == newSocketPort)
return;
socketPort = newSocketPort;
emit socketPortChanged();
}
// 修改预览编码方法
void ffmpegThreads::on_Encoding_Preview()
{
// 先启动TCP服务器
if (!startTcpServer()) {
qDebug() << "Failed to start TCP server";
return;
}
// 设置FFmpeg命令
qprocess_Program_Name = "ffmpeg";
qprocess_Command_Name = sourceCommandPreview;
if (FFMPEG_Process_Preview.state() == QProcess::Running) {
FFMPEG_Process_Preview.terminate();
if (!FFMPEG_Process_Preview.waitForFinished(1000)) {
FFMPEG_Process_Preview.kill();
}
}
// 启动FFmpeg进程
FFMPEG_Process_Preview.start(qprocess_Program_Name, QProcess::splitCommand(qprocess_Command_Name));
if (!FFMPEG_Process_Preview.waitForStarted()) {
qDebug() << "Failed to start FFmpeg process";
return;
}
qDebug() << "FFmpeg preview process started";
// 连接FFmpeg输出到TCP客户端
connect(&FFMPEG_Process_Preview, &QProcess::readyReadStandardOutput, [this]() {
if (clientSocket && clientSocket->state() == QAbstractSocket::ConnectedState) {
QByteArray data = FFMPEG_Process_Preview.readAllStandardOutput();
clientSocket->write(data);
clientSocket->flush();
}
});
connect(&FFMPEG_Process_Preview, &QProcess::readyReadStandardError, [this]() {
if (clientSocket && clientSocket->state() == QAbstractSocket::ConnectedState) {
QByteArray data = FFMPEG_Process_Preview.readAllStandardError();
clientSocket->write(data);
clientSocket->flush();
}
// 记录错误信息
QString errorOutput = QString::fromUtf8(FFMPEG_Process_Preview.readAllStandardError());
qDebug() << "FFmpeg error:" << errorOutput;;
QRegularExpression timeRegex("time=(\\d{2}:\\d{2}:\\d{2}\\.\\d+)");
QRegularExpressionMatch match = timeRegex.match(errorOutput);
if (match.hasMatch()) {
setVideo_Current_Duration(match.captured(1));
}
});
// 处理进程结束
connect(&FFMPEG_Process_Preview, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "FFmpeg preview process finished with code:" << exitCode;
if (exitCode != 0) {
qDebug() << FFMPEG_Process_Preview.exitStatus();
qDebug() << "FFmpeg preview process exited with error";
}
});
qDebug() << FFMPEG_Process_Preview.state();
qDebug() << sourceCommandPreview;
}
//调用本机ffmpeg执行信号,方便其他不同信号调用
void ffmpegThreads::ffmpeg_Process()
{
if (QThread::currentThread() != this->thread()) {
// 如果不在主线程,使用 QMetaObject::invokeMethod 切换到主线程
QMetaObject::invokeMethod(this, "ffmpeg_Process", Qt::QueuedConnection);
return;
}
FFMPEG_Process.start(qprocess_Program_Name,QProcess::splitCommand(qprocess_Command_Name));
QByteArray output = FFMPEG_Process.readAllStandardOutput();
QByteArray outputerror = FFMPEG_Process.readAllStandardError();
QString content;
if(qprocess_Program_Name == "ffmpeg" && qprocess_Command_Name == sourceCommand){
content = QString::fromUtf8(outputerror);
setLogMessage(content);
qDebug() << content;
QRegularExpression timeRegex("time=(\\d{2}:\\d{2}:\\d{2}\\.\\d+)");
QRegularExpressionMatch match = timeRegex.match(content);
if (match.hasMatch()) {
setVideo_Duration(match.captured(1));
}
}
else if(qprocess_Program_Name == "ffmpeg" && qprocess_Command_Name == "-version"){
content = QString::fromUtf8(output);
QRegularExpression versionRegex("(\\d+\\.\\d+(\\.\\d+)?(\\.\\d+)?)");
QRegularExpressionMatch match = versionRegex.match(content);
if (match.hasMatch()) {
setFfmpeg_Version(match.captured(1));
}
}
else if(qprocess_Program_Name == "ffprobe" && qprocess_Command_Name == videoFileUrl){
content = QString::fromUtf8(outputerror);
qDebug() << "Video info:" << content;
QRegularExpression durationRegex("Duration: (\\d{2}:\\d{2}:\\d{2}\\.\\d+)");
QRegularExpression videoSizeRegex("Video:.*?, (\\d+)x(\\d+)");
QRegularExpression videoCodecRegex("Video: ([^,]+)");
QRegularExpression audioCodecRegex("Audio: ([^,]+)");
QRegularExpression fpsRegex("\\b(\\d+(?:\\.\\d+)?)\\s?(?:fps|tbr)\\b");
QRegularExpressionMatch match = durationRegex.match(content);
if (match.hasMatch()) {
setVideo_Total_Duration(match.captured(1));
}
QRegularExpressionMatch sizeMatch = videoSizeRegex.match(content);
if (sizeMatch.hasMatch()) {
setSourceVideoWidth(sizeMatch.captured(1));
setSourceVideoHeight(sizeMatch.captured(2));
qDebug() << "Video size:" << sourceVideoWidth << "x" << sourceVideoHeight;
}
QRegularExpressionMatch fpsMatch = fpsRegex.match(content);
if (fpsMatch.hasMatch()) {
QString fpsValue = fpsMatch.captured(1);
setVideoFrameRate(fpsValue);
qDebug() << "Video FPS:" << fpsValue;
}
QRegularExpressionMatch videoCodecMatch = videoCodecRegex.match(content);
if (videoCodecMatch.hasMatch()) {
QString rawVideoCodec = videoCodecMatch.captured(1);
qDebug() << "Raw video codec:" << rawVideoCodec;
// 提取基础编码名称(去除括号中的内容)
QString baseCodec = rawVideoCodec.split(' ').first().toLower();
// 定义视频编码映射表
static QMap<QString, QString> videoCodecMap = {
{"h264", "libx264"},
{"h265", "libx265"},
{"hevc", "libx265"},
{"vp9", "libvpx-vp9"},
{"av1", "libaom-av1"},
{"mpeg4", "mpeg4"},
{"mpeg2", "mpeg2video"},
{"mjpeg", "mjpeg"},
{"prores", "prores"},
{"dnxhd", "dnxhd"}
};
// 查找映射
if (videoCodecMap.contains(baseCodec)) {
setSourceVideoCodec(videoCodecMap.value(baseCodec));
} else {
// 如果没有找到映射,使用原始编码名称
setSourceVideoCodec(baseCodec);
}
qDebug() << "Mapped video codec:" << sourceVideoCodec;
}
// 获取音频编码并进行映射
QRegularExpressionMatch audioCodecMatch = audioCodecRegex.match(content);
if (audioCodecMatch.hasMatch()) {
QString rawAudioCodec = audioCodecMatch.captured(1);
qDebug() << "Raw audio codec:" << rawAudioCodec;
// 提取基础编码名称(去除括号中的内容)
QString baseCodec = rawAudioCodec.split(' ').first().toLower();
// 定义音频编码映射表
static QMap<QString, QString> audioCodecMap = {
{"aac", "aac"},
{"mp3", "libmp3lame"},
{"ac3", "ac3"},
{"flac", "flac"},
{"opus", "libopus"},
{"vorbis", "libvorbis"},
{"pcm", "pcm_s16le"}
};
// 查找映射
if (audioCodecMap.contains(baseCodec)) {
sourceMusicCodec = audioCodecMap.value(baseCodec);
} else {
sourceMusicCodec = baseCodec;
}
qDebug() << "Mapped audio codec:" << sourceMusicCodec;
}
}
else{
content = QString::fromUtf8(outputerror);
}
}
//程序启动时获取ffmpeg版本号
void ffmpegThreads::check_Ffmpeg_Version()
{
qprocess_Program_Name = "ffmpeg";
qprocess_Command_Name = "-version";
ffmpegThreads::ffmpeg_Process();
}
//点击开始压制按钮后执行信号on_Start_Encoding()
void ffmpegThreads::on_Start_Encoding()
{
qprocess_Program_Name = "ffmpeg";
qprocess_Command_Name = sourceCommand;
ffmpegThreads::ffmpeg_Process();
}
//点击取消按钮后执行信号on_Cancel_Encoding()
void ffmpegThreads::on_Cancel_Encoding()
{
if (FFMPEG_Process.state() == QProcess::Running) {
FFMPEG_Process.terminate();
if (!FFMPEG_Process.waitForFinished(1000)) {
FFMPEG_Process.kill();
}
}
QFile::remove(delFileUrl.replace('"', ""));
}
void ffmpegThreads::on_Cancel_Encoding_Preview()
{
if (FFMPEG_Process_Preview.state() == QProcess::Running) {
FFMPEG_Process_Preview.terminate();
if (!FFMPEG_Process_Preview.waitForFinished(1000)) {
FFMPEG_Process_Preview.kill();
}
}
if (tcpServer) {
tcpServer->close();
delete tcpServer;
tcpServer = nullptr;
}
if (clientSocket) {
clientSocket->close();
clientSocket->deleteLater();
clientSocket = nullptr;
}
}
//选择视频文件后执行信号get_Video_Info()
void ffmpegThreads::get_Video_Info()
{
qprocess_Program_Name = "ffprobe";
qprocess_Command_Name = videoFileUrl;
ffmpegThreads::ffmpeg_Process();
}
QString ffmpegThreads::getVideo_Total_Duration() const
{
return video_Total_Duration;
}
void ffmpegThreads::setVideo_Total_Duration(const QString &newVideo_Total_Duration)
{
if (video_Total_Duration == newVideo_Total_Duration)
return;
video_Total_Duration = newVideo_Total_Duration;
emit video_Total_DurationChanged();
}
QString ffmpegThreads::getVideo_Duration() const
{
return video_Duration;
}
void ffmpegThreads::setVideo_Duration(const QString &newVideo_Duration)
{
if (video_Duration == newVideo_Duration)
return;
video_Duration = newVideo_Duration;
emit video_DurationChanged();
}
QString ffmpegThreads::getLogMessage() const
{
return logMessage;
}
void ffmpegThreads::setLogMessage(const QString &newLogMessage)
{
if (logMessage == newLogMessage)
return;
logMessage = newLogMessage;
emit logMessageChanged();
}