-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollage.cpp
More file actions
633 lines (525 loc) · 23.1 KB
/
collage.cpp
File metadata and controls
633 lines (525 loc) · 23.1 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
627
628
629
630
631
632
633
#include "collage.h"
#include <QMediaPlayer>
#include <QMediaMetaData>
#include <QEventLoop>
#include <QVideoSink>
#include <QVideoFrame>
#include <QDebug>
#include <QPainter>
#include <QFontMetrics>
#include <QtConcurrent>
#include <QCoreApplication>
#include <QMutex>
#include <QMutexLocker>
#include <QTimer>
#include <QDir>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <atomic>
// Timeout for external process completion (5 minutes per video)
// This allows for processing of large video files on slower systems
static constexpr int PROCESS_TIMEOUT_MS = 300000;
// Static tracking for running child processes
//
// These variables manage the lifecycle of child processes spawned for collage creation.
// They are shared across all Collage instances in this process.
//
// Threading / locking:
// - s_processMutex must be held whenever s_runningProcesses is modified or
// iterated, to protect against concurrent access from worker threads.
// - s_shuttingDown is an atomic flag that can be safely read from any thread.
// It is set to true during application shutdown to signal that no new child
// processes should be started and existing ones may be terminated.
//
// Ownership / lifetime:
// - s_runningProcesses contains pointers to heap-allocated QProcess instances.
// Each process is removed from the list when it finishes. On shutdown, any
// remaining running processes are forcefully killed.
static QMutex s_processMutex;
static QList<QProcess *> s_runningProcesses;
static std::atomic<bool> s_shuttingDown{false};
Collage::Collage() {
// Set up thread pool with limited concurrency
m_threadPool.setMaxThreadCount(MAX_CONCURRENT);
qDebug() << "Collage thread pool initialized with" << MAX_CONCURRENT << "max threads";
// Connect watcher signals
connect(&m_watcher, &QFutureWatcher<CollageResult>::resultReadyAt,
this, [this](int resultIndex) {
CollageResult result = m_watcher.resultAt(resultIndex);
emit collageCompleted(result.index, result.outputPath, result.success);
});
connect(&m_watcher, &QFutureWatcher<CollageResult>::finished,
this, &Collage::onFutureFinished);
// Connect to application shutdown for true global shutdown handling
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, [this]() {
s_shuttingDown = true;
m_shuttingDown = true;
});
}
Collage::~Collage() {
// Signal instance-level shutdown (does not affect other instances)
m_shuttingDown = true;
// Cancel pending work
m_watcher.cancel();
// Terminate all running child processes to allow quick shutdown
// The worker threads are blocked on waitForFinished(), so we need to
// kill the processes here to unblock them
{
QMutexLocker locker(&s_processMutex);
for (QProcess *process : s_runningProcesses) {
if (process && process->state() != QProcess::NotRunning) {
qDebug() << "Terminating child process on shutdown";
process->kill();
}
}
}
// Wait for our tasks to complete (they will exit quickly now that processes are killed)
m_watcher.waitForFinished();
}
void Collage::toCollage(const QList<QUrl> &paths) {
// Cancel any running operation
if (m_watcher.isRunning()) {
m_watcher.cancel();
m_watcher.waitForFinished();
}
// Reset shutdown flag for new operation
m_shuttingDown = false;
m_currentPaths = paths;
emit collageStarted(paths.size());
if (paths.isEmpty()) {
emit collageFinished(0, 0);
return;
}
// Create list of index-path pairs for mapping
QList<QPair<int, QUrl> > indexedPaths;
for (int i = 0; i < paths.size(); ++i) {
indexedPaths.append({i, paths[i]});
}
// Use QtConcurrent::mapped with custom thread pool
// Each video is processed in a SEPARATE PROCESS for complete isolation
QFuture<CollageResult> future = QtConcurrent::mapped(
&m_threadPool,
indexedPaths,
[this](const QPair<int, QUrl> &pair) -> CollageResult {
return processVideoExternal(pair.first, pair.second);
}
);
m_watcher.setFuture(future);
}
QString Collage::createCollageSingle(const QUrl &path) {
// This is called from CLI mode - does the actual collage work synchronously
qDebug() << "Creating collage for:" << path.toString();
auto meta = collectImages(path);
auto collageImage = drawCollage(meta);
if (collageImage.isNull()) {
qDebug() << "Collage image is null for path:" << path.toString();
return QString();
}
// Get the source file info
QFileInfo sourceFile(path.toLocalFile());
// Create output filename: originalname_collage.jpg
QString outputFilename = sourceFile.completeBaseName() + "_collage.jpg";
// Get the directory where the source file is located
QString outputPath = sourceFile.dir().filePath(outputFilename);
if (collageImage.save(outputPath, "JPEG", 100)) {
qDebug() << "Collage saved to:" << outputPath;
return outputPath;
}
qDebug() << "Failed to save collage to:" << outputPath;
return QString();
}
CollageResult Collage::processVideoExternal(int index, const QUrl &path) {
// Check if we're shutting down before starting
if (m_shuttingDown || s_shuttingDown) {
return CollageResult{index, path.toLocalFile(), QString(), false};
}
// Emit progress signal (Qt handles cross-thread delivery)
emit collageProgress(index, path.toString());
// Spawn a separate process to create the collage
// This completely isolates the heavy media processing from the main app
qDebug() << "Spawning external process for video" << index << ":" << path.toString();
QString appPath = QCoreApplication::applicationFilePath();
QString inputPath = path.toLocalFile();
// Use heap-allocated QProcess to ensure valid pointer lifetime while in s_runningProcesses
auto process = new QProcess();
process->setProgram(appPath);
process->setArguments({"--collage", inputPath});
// Set environment variable to indicate subprocess mode (suppresses spinner output)
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("GAV_SUBPROCESS", "1");
process->setProcessEnvironment(env);
// Register process for cleanup on shutdown
{
QMutexLocker locker(&s_processMutex);
s_runningProcesses.append(process);
}
process->start();
// Wait for process to finish
bool finished = process->waitForFinished(PROCESS_TIMEOUT_MS);
// Unregister process from tracking list
{
QMutexLocker locker(&s_processMutex);
s_runningProcesses.removeOne(process);
}
// Check if we were killed due to shutdown
if (m_shuttingDown || s_shuttingDown) {
if (process->state() != QProcess::NotRunning) {
process->kill();
process->waitForFinished(1000);
}
delete process;
return CollageResult{index, inputPath, QString(), false};
}
if (!finished) {
qDebug() << "Process timeout for video" << index;
process->kill();
process->waitForFinished(1000);
delete process;
return CollageResult{index, inputPath, QString(), false};
}
int exitCode = process->exitCode();
QString stdOut = QString::fromUtf8(process->readAllStandardOutput()).trimmed();
QString stdErr = QString::fromUtf8(process->readAllStandardError()).trimmed();
qDebug() << "Process finished for video" << index << "with exit code" << exitCode;
qDebug() << "stdout:" << stdOut;
if (!stdErr.isEmpty()) {
qDebug() << "stderr:" << stdErr;
}
delete process;
// Parse output - format is SUCCESS:<path> or FAILED:<path>
if (exitCode == 0 && stdOut.startsWith("SUCCESS:")) {
QString outputPath = stdOut.mid(8); // Remove "SUCCESS:" prefix
return CollageResult{index, inputPath, outputPath, true};
}
return CollageResult{index, inputPath, QString(), false};
}
void Collage::onFutureFinished() {
int successCount = 0;
int failCount = 0;
// Count results
for (int i = 0; i < m_watcher.future().resultCount(); ++i) {
if (m_watcher.resultAt(i).success) {
successCount++;
} else {
failCount++;
}
}
emit collageFinished(successCount, failCount);
}
ImageMeta Collage::collectImages(const QUrl &path) {
qDebug() << "=== Starting collectImages for:" << path.toString();
QMediaPlayer player;
player.setSource(path);
QEventLoop loop;
QObject::connect(&player, &QMediaPlayer::mediaStatusChanged, &loop, [&](QMediaPlayer::MediaStatus status) {
qDebug() << "Media status changed to:" << status;
if (status == QMediaPlayer::LoadedMedia || status == QMediaPlayer::InvalidMedia) {
loop.quit();
}
});
QObject::connect(&player, &QMediaPlayer::errorOccurred, &loop,
[&loop](QMediaPlayer::Error error, const QString &errorString) {
qDebug() << "Media player error occurred:" << error << errorString;
loop.quit();
});
// Timeout after 2 seconds to prevent blocking indefinitely
QTimer::singleShot(2000, &loop, &QEventLoop::quit);
loop.exec();
qDebug() << "Media loaded. Duration:" << player.duration() << "ms";
auto data = player.metaData();
QList<ImageTime> frames;
// Calculate file size
const QFileInfo fileInfo(path.toLocalFile());
QString fileSize;
if (fileInfo.exists()) {
qint64 sizeInBytes = fileInfo.size();
qDebug() << "File size in bytes:" << sizeInBytes;
if (sizeInBytes < 1024) {
fileSize = QString::number(sizeInBytes) + " B";
} else if (sizeInBytes < 1024 * 1024) {
fileSize = QString::number(sizeInBytes / 1024.0, 'f', 2) + " KB";
} else if (sizeInBytes < 1024 * 1024 * 1024) {
fileSize = QString::number(sizeInBytes / (1024.0 * 1024.0), 'f', 2) + " MB";
} else {
fileSize = QString::number(sizeInBytes / (1024.0 * 1024.0 * 1024.0), 'f', 2) + " GB";
}
qDebug() << "Formatted file size:" << fileSize;
} else {
qDebug() << "File does not exist at:" << path.toLocalFile();
}
// Set up video sink for frame capture
QVideoSink videoSink;
player.setVideoSink(&videoSink);
qDebug() << "Video sink set up";
// Start playing to initialize the video pipeline
player.play();
qDebug() << "Player started playing";
// Wait a bit for the player to start
QEventLoop startLoop;
QTimer::singleShot(300, &startLoop, &QEventLoop::quit);
startLoop.exec();
qDebug() << "Player initialization wait complete";
// Seek over the video and capture frames
qint64 duration = player.duration();
if (duration > 0) {
int numFrames = qMin(16, static_cast<int>(duration / 1000) + 1); // At least 1 frame, up to 10
numFrames = qMax(1, numFrames); // Ensure at least one frame
qDebug() << "Will attempt to capture" << numFrames << "frames";
for (int i = 0; i < numFrames; ++i) {
// Calculate position for this frame
qint64 position = (duration * i) / numFrames;
qDebug() << "Frame" << i << "- seeking to position:" << position << "ms";
player.setPosition(position);
// Brief wait for seek to complete
QEventLoop seekLoop;
QTimer::singleShot(100, &seekLoop, &QEventLoop::quit);
seekLoop.exec();
// Pause to render the frame at this position
player.pause();
// Wait for the frame to be available
QEventLoop frameLoop;
QTimer frameTimer;
frameTimer.setSingleShot(true);
bool frameReady = false;
auto connection = QObject::connect(&videoSink, &QVideoSink::videoFrameChanged, &frameLoop,
[&](const QVideoFrame &frame) {
if (frame.isValid()) {
if (const QImage image = frame.toImage(); !image.isNull()) {
frames.append({.image = image, .timestamp = position});
frameReady = true;
frameLoop.quit();
}
}
});
// Timeout after 800ms per frame
QObject::connect(&frameTimer, &QTimer::timeout, &frameLoop, [&frameLoop]() {
frameLoop.quit();
});
frameTimer.start(800);
frameLoop.exec();
QObject::disconnect(connection);
// If we didn't get a frame via signal, try to get the current frame directly
if (!frameReady) {
qDebug() << "Frame" << i << "- signal did not provide frame, trying direct access";
if (QVideoFrame frame = videoSink.videoFrame(); frame.isValid()) {
qDebug() << "Frame" << i << "- got valid frame directly";
if (QImage image = frame.toImage(); !image.isNull()) {
frames.append({.image = image, .timestamp = position});
frameReady = true;
qDebug() << "Frame" << i << "- successfully converted to image";
} else {
qDebug() << "Frame" << i << "- failed to convert to image";
}
} else {
qDebug() << "Frame" << i << "- no valid frame available";
}
} else {
qDebug() << "Frame" << i << "- captured successfully via signal";
}
// Resume playing for the next seek
if (i < numFrames - 1) {
player.play();
QEventLoop resumeLoop;
QTimer::singleShot(50, &resumeLoop, &QEventLoop::quit);
resumeLoop.exec();
}
}
} else {
qDebug() << "Duration is 0 or invalid, cannot capture frames";
}
player.stop();
qDebug() << "Total frames captured:" << frames.size();
// If no frames were captured, try playing briefly then capturing
if (frames.isEmpty()) {
qDebug() << "No frames captured, trying fallback method";
player.setPosition(0);
player.play();
QEventLoop playLoop;
QTimer::singleShot(500, &playLoop, &QEventLoop::quit);
playLoop.exec();
player.pause();
QEventLoop pauseLoop;
QTimer::singleShot(200, &pauseLoop, &QEventLoop::quit);
pauseLoop.exec();
if (QVideoFrame frame = videoSink.videoFrame(); frame.isValid()) {
qDebug() << "Fallback - got valid frame";
if (QImage image = frame.toImage(); !image.isNull()) {
frames.append({.image = image, .timestamp = player.position()});
qDebug() << "Fallback - successfully captured frame";
} else {
qDebug() << "Fallback - failed to convert frame to image";
}
} else {
qDebug() << "Fallback - no valid frame available";
}
player.stop();
}
auto image = ImageMeta{
.image = frames,
.name = player.source().fileName(),
.duration = data.stringValue(QMediaMetaData::Duration),
.audioCodec = data.stringValue(QMediaMetaData::AudioCodec),
.videoCodec = data.stringValue(QMediaMetaData::VideoCodec),
.size = fileSize,
.resolution = data.stringValue(QMediaMetaData::Resolution)
};
qDebug() << "Collected" << frames.size() << "frames from" << path.toString() << "Name:" << image.name
<< "Duration:" << image.duration << "Audio Codec:" << image.audioCodec
<< "Video Codec:" << image.videoCodec << "Size:" << image.size
<< "Resolution:" << image.resolution;
return image;
}
QImage Collage::drawCollage(const ImageMeta &meta) {
auto images = meta.image;
if (images.isEmpty()) {
qDebug() << "No images to draw collage.";
return {};
}
// Determine if video is portrait or landscape from first frame
bool isPortrait = false;
if (!images.isEmpty()) {
const auto &firstImg = images.first().image;
isPortrait = firstImg.height() > firstImg.width();
}
QList<QImage> processedImages;
// Larger thumbnails for better quality
constexpr int thumbSize = 320;
for (const auto &img: images) {
QImage resized = img.image.scaled(thumbSize, thumbSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
// convert img.timestamp to hh:mm:ss format
const qint64 seconds = img.timestamp / 1000;
const qint64 hh = seconds / 3600;
const qint64 mm = (seconds % 3600) / 60;
const qint64 ss = seconds % 60;
QString timeText = QString("%1:%2:%3")
.arg(hh, 2, 10, QChar('0'))
.arg(mm, 2, 10, QChar('0'))
.arg(ss, 2, 10, QChar('0'));
QPainter painter(&resized);
painter.setRenderHint(QPainter::Antialiasing);
// Draw semi-transparent background for timestamp
QFont font("Arial", 10, QFont::Bold);
painter.setFont(font);
QFontMetrics fm(font);
QRect textRect = fm.boundingRect(timeText);
int padding = 4;
QRect bgRect(resized.width() - textRect.width() - padding * 2 - 2,
resized.height() - textRect.height() - padding * 2 - 2,
textRect.width() + padding * 2,
textRect.height() + padding * 2);
painter.fillRect(bgRect, QColor(0, 0, 0, 160));
// Draw timestamp text
painter.setPen(Qt::white);
painter.drawText(bgRect, Qt::AlignCenter, timeText);
painter.end();
#ifdef QT_DEBUG
// Save image to disk for debugging
if (QString debugFilename = QString("debug_frame_%1ms.jpg").arg(img.timestamp); resized.save(debugFilename)) {
qDebug() << "Saved debug image:" << debugFilename;
} else {
qDebug() << "Failed to save debug image:" << debugFilename;
}
#endif
processedImages.append(resized);
}
// Create description table at the top
constexpr int tableMargin = 10;
constexpr int tableRowHeight = 18;
constexpr int tablePadding = 8;
constexpr int tableRadius = 8;
// Format duration as hh:mm:ss
QString formattedDuration = meta.duration;
bool ok;
qint64 durationMs = meta.duration.toLongLong(&ok);
if (ok && durationMs > 0) {
qint64 totalSeconds = durationMs / 1000;
qint64 hours = totalSeconds / 3600;
qint64 minutes = (totalSeconds % 3600) / 60;
qint64 secs = totalSeconds % 60;
formattedDuration = QString("%1:%2:%3")
.arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0'))
.arg(secs, 2, 10, QChar('0'));
}
// Prepare metadata text
QStringList metaLines;
metaLines << QString("Name: %1").arg(meta.name);
metaLines << QString("Duration: %1").arg(formattedDuration);
metaLines << QString("Resolution: %1").arg(meta.resolution);
metaLines << QString("Size: %1").arg(meta.size);
metaLines << QString("Video Codec: %1").arg(meta.videoCodec);
metaLines << QString("Audio Codec: %1").arg(meta.audioCodec);
const int tableHeight = metaLines.size() * tableRowHeight + 2 * tablePadding;
// Draw collage using QPainter instead of QTableWidget
// Auto-adjust columns: 3 for portrait videos, 4 for landscape
const int cols = isPortrait ? 3 : 4;
const int rows = (processedImages.size() + cols - 1) / cols;
constexpr int paddingX = 5; // Gap between columns
constexpr int paddingY = 5; // Gap between rows
constexpr int marginTop = 5; // Top margin
constexpr int marginBottom = 5; // Bottom margin
constexpr int marginLeft = 5; // Left margin
constexpr int marginRight = 5; // Right margin
// Find the actual max dimensions from processed images
int maxImgWidth = 0;
int maxImgHeight = 0;
for (const auto &img: processedImages) {
maxImgWidth = qMax(maxImgWidth, img.width());
maxImgHeight = qMax(maxImgHeight, img.height());
}
// Use actual image dimensions for cell size
const int cellWidth = maxImgWidth;
const int cellHeight = maxImgHeight;
// Canvas width: left margin + all columns + gaps between columns + right margin
const int canvasWidth = marginLeft + cols * cellWidth + (cols - 1) * paddingX + marginRight;
// Canvas height: top margin + table + margin + all rows + gaps between rows + bottom margin
const int canvasHeight = marginTop + tableHeight + tableMargin + rows * cellHeight + (rows - 1) * paddingY +
marginBottom;
QImage collageImage(canvasWidth, canvasHeight, QImage::Format_ARGB32);
collageImage.fill(Qt::white);
QPainter collagePainter(&collageImage);
// Draw description table background
constexpr int tableX = marginLeft;
constexpr int tableY = marginTop;
const int tableWidth = canvasWidth - marginLeft - marginRight;
collagePainter.setRenderHint(QPainter::Antialiasing);
collagePainter.setPen(QPen(QColor(100, 100, 100), 1));
collagePainter.setBrush(QColor(245, 245, 245));
collagePainter.drawRoundedRect(tableX, tableY, tableWidth, tableHeight, tableRadius, tableRadius);
// Draw table content
collagePainter.setPen(Qt::black);
collagePainter.setFont(QFont("Arial", 10));
for (int i = 0; i < metaLines.size(); ++i) {
constexpr int textX = tableX + tablePadding;
const int textY = tableY + tablePadding + i * tableRowHeight + tableRowHeight / 2 + 5;
collagePainter.drawText(textX, textY, metaLines[i]);
}
// Draw each image in a grid layout
const int imagesStartY = marginTop + tableHeight + tableMargin;
for (int i = 0; i < processedImages.size(); ++i) {
const int row = i / cols;
const int col = i % cols;
// Position: margin + (cell size + gap) * index
const int x = marginLeft + col * (cellWidth + paddingX);
const int y = imagesStartY + row * (cellHeight + paddingY);
// Draw the image centered in the cell
const QImage &img = processedImages[i];
const int imgX = x + (cellWidth - img.width()) / 2;
const int imgY = y + (cellHeight - img.height()) / 2;
collagePainter.drawImage(imgX, imgY, img);
// Draw thin border around thumbnail
collagePainter.setPen(QPen(QColor(180, 180, 180), 1));
collagePainter.setBrush(Qt::NoBrush);
collagePainter.drawRect(imgX, imgY, img.width(), img.height());
}
collagePainter.end();
#ifdef QT_DEBUG
// Save collage image to disk for debugging
if (collageImage.save("debug_collage.jpg")) {
qDebug() << "Saved debug collage image: debug_collage.jpg";
} else {
qDebug() << "Failed to save debug collage image: debug_collage.jpg";
}
#endif
return collageImage;
}