File indexing completed on 2025-01-05 03:57:26

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-05-25
0007  * Description : a tool to generate video slideshow from images.
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "vidslidethread.h"
0016 
0017 // Qt include
0018 
0019 #include <QDir>
0020 #include <QDateTime>
0021 
0022 // KDE includes
0023 
0024 #include <klocalizedstring.h>
0025 
0026 // Local includes
0027 
0028 #include "digikam_debug.h"
0029 #include "digikam_config.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 VidSlideThread::VidSlideThread(QObject* const parent)
0035     : ActionThreadBase(parent)
0036 {
0037     setObjectName(QLatin1String("VidSlideThread"));
0038 }
0039 
0040 VidSlideThread::~VidSlideThread()
0041 {
0042     cancel();
0043     wait();
0044 
0045     if (m_settings && QDir().exists(m_settings->tempDir))
0046     {
0047         QDir(m_settings->tempDir).removeRecursively();
0048     }
0049 }
0050 
0051 void VidSlideThread::prepareFrames(VidSlideSettings* const settings)
0052 {
0053     m_settings          = settings;
0054 
0055     m_settings->tempDir = m_settings->outputDir + QDir::separator() + QLatin1Char('.')     +
0056                           QString::number(QDateTime::currentDateTime().toSecsSinceEpoch()) +
0057                           QDir::separator();
0058 
0059     if (!QDir().mkpath(m_settings->tempDir))
0060     {
0061         qCWarning(DIGIKAM_GENERAL_LOG) << "Cannot create temporary directory:" << m_settings->tempDir;
0062 
0063         Q_EMIT signalMessage(i18n("Error while creating temporary directory %1!", m_settings->tempDir), true);
0064 
0065         return;
0066     }
0067 
0068     ActionJobCollection collection;
0069 
0070     VidSlideTask* const t = new VidSlideTask(settings);
0071 
0072     connect(t, SIGNAL(signalProgress(int)),
0073             this, SIGNAL(signalProgress(int)));
0074 
0075     connect(t, SIGNAL(signalMessage(QString,bool)),
0076             this, SIGNAL(signalMessage(QString,bool)));
0077 
0078     connect(t, SIGNAL(signalDone(bool)),
0079             this, SLOT(slotEncodeFrames(bool)));
0080 
0081     collection.insert(t, 0);
0082 
0083     appendJobs(collection);
0084 }
0085 
0086 void VidSlideThread::slotEncodeFrames(bool prepareDone)
0087 {
0088     if (!prepareDone)
0089     {
0090         Q_EMIT signalDone(false);
0091 
0092         return;
0093     }
0094 
0095     Q_EMIT signalMessage(i18n("Encoding frames..."), false);
0096 
0097     if (m_encoder)
0098     {
0099         delete m_encoder;
0100     }
0101 
0102     m_encoder = new FFmpegLauncher(this);
0103     m_encoder->setSettings(m_settings);
0104 
0105     connect(m_encoder, &ProcessLauncher::signalComplete,
0106             this, &VidSlideThread::slotEncodeDone,
0107             Qt::DirectConnection);
0108 
0109     m_encoder->encodeFrames();
0110 }
0111 
0112 void VidSlideThread::slotEncodeDone(bool success, int exitCode)
0113 {
0114     bool b = false;
0115 
0116     if (!success || (exitCode != 0))
0117     {
0118         qCDebug(DIGIKAM_GENERAL_LOG) << "Cannot generate output video" << m_settings->outputFile;
0119     }
0120     else
0121     {
0122         qCDebug(DIGIKAM_GENERAL_LOG) << "Output video is" << m_settings->outputFile;
0123         b = true;
0124     }
0125 
0126     if (b)
0127     {
0128         if (QDir().exists(m_settings->tempDir))
0129         {
0130             QDir(m_settings->tempDir).removeRecursively();
0131         }
0132 
0133         Q_EMIT signalMessage(i18n("Encoding done."), false);
0134     }
0135     else
0136     {
0137         Q_EMIT signalMessage(i18n("Error while encoding frames!"), true);
0138     }
0139 
0140     Q_EMIT signalDone(b);
0141 }
0142 
0143 QString VidSlideThread::encodingTraces() const
0144 {
0145     if (m_encoder)
0146     {
0147         return m_encoder->output();
0148     }
0149 
0150     return QString();
0151 }
0152 
0153 } // namespace Digikam
0154 
0155 #include "moc_vidslidethread.cpp"