diff --git a/CMakeLists.txt b/CMakeLists.txt index f99c3cf..9c5de58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) option(USE_QTEXTCODEC "Use QTextCodec instead of QStringConverter, in case Qt is not built with ICU" OFF) find_package(Qt6 6.6 COMPONENTS Widgets Multimedia Network LinguistTools REQUIRED) +find_package(FFmpeg COMPONENTS avutil avformat) find_package(TagLib 2.0.0) find_package(KF6Codecs 6.1.0) @@ -96,6 +97,11 @@ if (TARGET KF6::Codecs) target_link_libraries (${EXE_NAME} PRIVATE KF6::Codecs) endif () +if (FFmpeg_FOUND) + target_compile_definitions(${EXE_NAME} PRIVATE HAVE_FFMPEG=1) + target_link_libraries (${EXE_NAME} PRIVATE FFmpeg::avutil FFmpeg::avformat) +endif () + target_link_libraries(${EXE_NAME} PRIVATE Qt::Widgets Qt::Multimedia Qt::Network kissfft::kissfft) if (USE_QTEXTCODEC) diff --git a/mainwindow.cpp b/mainwindow.cpp index f3355d6..33dd9a2 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -271,20 +271,21 @@ void MainWindow::loadFile() m_playlistManager->loadPlaylist(urlList); const QUrl & firstUrl = urlList.first(); - const QString firstFilePath = firstUrl.toLocalFile(); - m_mediaPlayer->setSource(firstUrl); - m_lrcbar->loadLyrics(firstFilePath); - QList> chapters(PlaybackProgressIndicator::tryLoadSidecarChapterFile(firstFilePath)); + loadFile(firstUrl); +} + +void MainWindow::loadFile(const QUrl &url) +{ + const QString filePath = url.toLocalFile(); + m_mediaPlayer->setSource(url); + m_lrcbar->loadLyrics(filePath); + QList> chapters(PlaybackProgressIndicator::tryLoadChapters(filePath)); ui->playbackProgressIndicator->setChapters(chapters); } void MainWindow::loadByModelIndex(const QModelIndex & index) { - m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index)); - QString filePath(m_playlistManager->localFileByIndex(index)); - m_lrcbar->loadLyrics(filePath); - QList> chapters(PlaybackProgressIndicator::tryLoadSidecarChapterFile(filePath)); - ui->playbackProgressIndicator->setChapters(chapters); + loadFile(m_playlistManager->urlByIndex(index)); } void MainWindow::play() diff --git a/mainwindow.h b/mainwindow.h index c86cf65..cbb941f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -56,6 +56,7 @@ protected: void dropEvent(QDropEvent *e) override; void loadFile(); + void loadFile(const QUrl &url); void loadByModelIndex(const QModelIndex &index); void play(); diff --git a/playbackprogressindicator.cpp b/playbackprogressindicator.cpp index 77bfd25..93e1c1f 100644 --- a/playbackprogressindicator.cpp +++ b/playbackprogressindicator.cpp @@ -11,6 +11,15 @@ #include #include +#ifdef HAVE_FFMPEG +extern "C" { +#include +#include +#include +#include // Contains AV_TIME_BASE and AV_TIME_BASE_Q +} // extern "C" +#endif // HAVE_FFMPEG + PlaybackProgressIndicator::PlaybackProgressIndicator(QWidget *parent) : QWidget(parent) { @@ -30,6 +39,7 @@ void PlaybackProgressIndicator::setDuration(qint64 dur) void PlaybackProgressIndicator::setChapters(QList > chapters) { + qDebug() << chapters; m_chapterModel.clear(); for (const std::pair & chapter : chapters) { QStandardItem * chapterItem = new QStandardItem(chapter.second); @@ -39,6 +49,15 @@ void PlaybackProgressIndicator::setChapters(QList > c update(); } +QList > PlaybackProgressIndicator::tryLoadChapters(const QString &filePath) +{ + auto chapters = tryLoadSidecarChapterFile(filePath); + if (chapters.size() == 0) { + chapters = tryLoadChaptersFromMetadata(filePath); + } + return chapters; +} + QList > PlaybackProgressIndicator::tryLoadSidecarChapterFile(const QString &filePath) { if (filePath.endsWith(".chp", Qt::CaseInsensitive)) { @@ -63,6 +82,98 @@ QList > PlaybackProgressIndicator::tryLoadSidecarChap return {}; } +#ifdef HAVE_FFMPEG + +// Helper function to convert FFmpeg time (in time_base units) to milliseconds +qint64 convertTimestampToMilliseconds(int64_t timestamp, AVRational time_base) { + // Convert to seconds first, then to milliseconds and cast to qint64 + return static_cast((double)timestamp * av_q2d(time_base) * 1000.0); +} + +// Helper function to print FFmpeg errors +void printFFmpegError(int errnum) { + char errbuf[AV_ERROR_MAX_STRING_SIZE]; + av_strerror(errnum, errbuf, sizeof(errbuf)); + qCritical() << "FFmpeg error:" << errbuf; +} + +#endif // HAVE_FFMPEG + +QList > PlaybackProgressIndicator::tryLoadChaptersFromMetadata(const QString &filePath) +{ +#ifdef HAVE_FFMPEG + if (!QFile::exists(filePath)) { + qCritical() << "Error: File not found" << filePath; + return {}; + } + + AVFormatContext* format_ctx = nullptr; // FFmpeg format context + int ret = 0; // Return value for FFmpeg functions + + qInfo() << "Attempting to open file:" << filePath; + + // Open the input file and read the header. + // The last two arguments (AVInputFormat*, AVDictionary**) are optional. + // Passing nullptr for them means FFmpeg will try to guess the format + // and no options will be passed to the demuxer. + ret = avformat_open_input(&format_ctx, filePath.toUtf8().constData(), nullptr, nullptr); + if (ret < 0) { + qCritical() << "Could not open input file:" << filePath; + printFFmpegError(ret); + return {}; + } + qInfo() << "File opened successfully."; + + // Read stream information from the file. + // This populates format_ctx->streams and other metadata, including chapters. + ret = avformat_find_stream_info(format_ctx, nullptr); + if (ret < 0) { + qCritical() << "Could not find stream information for file:" << filePath; + printFFmpegError(ret); + avformat_close_input(&format_ctx); // Close the context before returning + return {}; + } + qInfo() << "Stream information found."; + + QList> chapterList; + + // Check if there are any chapters + if (format_ctx->nb_chapters == 0) { + qInfo() << "No chapters found in file:" << filePath; + } else { + qInfo() << "Found" << format_ctx->nb_chapters << "chapters."; + // Iterate through each chapter + for (unsigned int i = 0; i < format_ctx->nb_chapters; ++i) { + AVChapter* chapter = format_ctx->chapters[i]; + + // Chapter timestamps are typically in AV_TIME_BASE units by default + // unless the chapter itself has a specific time_base. + // For simplicity and common cases, we use AV_TIME_BASE_Q. + qint64 start_ms = convertTimestampToMilliseconds(chapter->start, chapter->time_base); + + // Get the chapter title from its metadata. + // av_dict_get(dictionary, key, prev, flags) + // prev is used for iterating through multiple entries with the same key, + // we want the first one so we pass nullptr. flags=0 for case-insensitive. + AVDictionaryEntry* title_tag = av_dict_get(chapter->metadata, "title", nullptr, 0); + QString chapter_title = (title_tag && title_tag->value) ? QString::fromUtf8(title_tag->value) : "Untitled Chapter"; + + chapterList.append(std::make_pair(start_ms, chapter_title)); + } + } + + // Close the input file. + // This also frees the format_ctx and associated data. + avformat_close_input(&format_ctx); + qInfo() << "File closed."; + + return chapterList; +#else + qInfo() << "FFmpeg not found during build."; + return {}; +#endif // HAVE_FFMPEG +} + QList > PlaybackProgressIndicator::parseCHPChapterFile(const QString &filePath) { QList> chapters; diff --git a/playbackprogressindicator.h b/playbackprogressindicator.h index 44e048b..f1de64d 100644 --- a/playbackprogressindicator.h +++ b/playbackprogressindicator.h @@ -28,7 +28,9 @@ public: void setDuration(qint64 dur); void setChapters(QList> chapters); + static QList> tryLoadChapters(const QString & filePath); static QList> tryLoadSidecarChapterFile(const QString & filePath); + static QList> tryLoadChaptersFromMetadata(const QString & filePath); static QList> parseCHPChapterFile(const QString & filePath); static QList> parsePBFChapterFile(const QString & filePath);