File indexing completed on 2024-05-05 04:22:01

0001 // SPDX-FileCopyrightText: 2012 - 2022 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 #include "VideoLengthExtractor.h"
0007 
0008 #include <MainWindow/FeatureDialog.h>
0009 #include <Utilities/Process.h>
0010 #include <kpabase/Logging.h>
0011 
0012 #include <QDir>
0013 
0014 #define STR(x) QString::fromUtf8(x)
0015 
0016 ImageManager::VideoLengthExtractor::VideoLengthExtractor(QObject *parent)
0017     : QObject(parent)
0018     , m_process(nullptr)
0019 {
0020 }
0021 
0022 void ImageManager::VideoLengthExtractor::extract(const DB::FileName &fileName)
0023 {
0024     m_fileName = fileName;
0025     if (m_process) {
0026         disconnect(m_process, SIGNAL(finished(int)), this, SLOT(processEnded()));
0027         m_process->kill();
0028         delete m_process;
0029         m_process = nullptr;
0030     }
0031 
0032     if (!MainWindow::FeatureDialog::hasVideoThumbnailer()) {
0033         Q_EMIT unableToDetermineLength();
0034         return;
0035     }
0036 
0037     m_process = new Utilities::Process(this);
0038     m_process->setWorkingDirectory(QDir::tempPath());
0039     connect(m_process, SIGNAL(finished(int)), this, SLOT(processEnded()));
0040 
0041     Q_ASSERT(MainWindow::FeatureDialog::hasVideoProber());
0042     QStringList arguments;
0043     // Just look at the length of the container. Some videos have streams without duration entry
0044     arguments << STR("-v") << STR("0") << STR("-show_entries") << STR("format=duration")
0045               << STR("-of") << STR("default=noprint_wrappers=1:nokey=1")
0046               << fileName.absolute();
0047 
0048     qCDebug(ImageManagerLog, "%s %s", qPrintable(MainWindow::FeatureDialog::ffprobeBinary()), qPrintable(arguments.join(QString::fromLatin1(" "))));
0049     m_process->start(MainWindow::FeatureDialog::ffprobeBinary(), arguments);
0050 }
0051 
0052 void ImageManager::VideoLengthExtractor::processEnded()
0053 {
0054     if (!m_process->stdErr().isEmpty())
0055         qCDebug(ImageManagerLog) << m_process->stdErr();
0056 
0057     const QStringList list = m_process->stdOut().split(QChar::fromLatin1('\n'));
0058     // ffprobe -v 0 just prints one line, except if panicking
0059     if (list.count() < 1) {
0060         qCWarning(ImageManagerLog) << "Unable to parse video length from ffprobe output!"
0061                                    << "Output was:\n"
0062                                    << m_process->stdOut();
0063         Q_EMIT unableToDetermineLength();
0064         return;
0065     }
0066     const QString lenStr = list[0].trimmed();
0067 
0068     bool ok = false;
0069     const double length = lenStr.toDouble(&ok);
0070     if (!ok) {
0071         qCWarning(ImageManagerLog) << STR("Unable to convert string \"%1\"to double (for file %2)").arg(lenStr, m_fileName.absolute());
0072         Q_EMIT unableToDetermineLength();
0073         return;
0074     }
0075 
0076     if (length == 0) {
0077         qCWarning(ImageManagerLog) << "video length returned was 0 for file " << m_fileName.absolute();
0078         Q_EMIT unableToDetermineLength();
0079         return;
0080     }
0081 
0082     Q_EMIT lengthFound(int(length));
0083     m_process->deleteLater();
0084     m_process = nullptr;
0085 }
0086 // vi:expandtab:tabstop=4 shiftwidth=4:
0087 
0088 #include "moc_VideoLengthExtractor.cpp"