feat(translate): translate a whole playlist so the next episodes are ready (#16)
Co-authored-by: Tri Vo Khac <git@trivk.dev>
This commit was merged in pull request #16.
This commit is contained in:
1 parent
ad50230829
commit
bcfc2f3f6a
13 files changed
+480
-61
No files matched your search
@@ -1,5 +1,7 @@
|
||||
#include "subtitleextractor.h"
|
||||
|
||||
#include "trackselection.h"
|
||||
|
||||
#include <KLocalizedString>
|
||||
|
||||
#include <memory>
|
||||
@@ -138,4 +140,39 @@ ExtractResult extractEmbedded(const QString &path, int streamIndex, const std::a
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
SourceResult findTranslationSource(const QString &path)
|
||||
{
|
||||
AVFormatContext *rawFormat = nullptr;
|
||||
if (const int ret = avformat_open_input(&rawFormat, path.toUtf8().constData(), nullptr, nullptr); ret < 0) {
|
||||
return {-1, i18n("Could not open %1: %2", path, ffmpegError(ret))};
|
||||
}
|
||||
std::unique_ptr<AVFormatContext, FormatContextDeleter> format(rawFormat);
|
||||
|
||||
const auto language = [](const AVStream *stream) {
|
||||
const AVDictionaryEntry *tag = av_dict_get(stream->metadata, "language", nullptr, 0);
|
||||
return tag ? QString::fromUtf8(tag->value) : QString();
|
||||
};
|
||||
// Track ids start at 1, since 0 means "none"; id - 1 is the stream index.
|
||||
QList<TrackSelection::Track> subtitles;
|
||||
QString audioLanguage;
|
||||
bool audioIsDefault = false;
|
||||
for (unsigned i = 0; i < format->nb_streams; ++i) {
|
||||
const AVStream *stream = format->streams[i];
|
||||
const bool isDefault = stream->disposition & AV_DISPOSITION_DEFAULT;
|
||||
if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||||
if (audioLanguage.isEmpty() || (isDefault && !audioIsDefault)) {
|
||||
audioLanguage = language(stream);
|
||||
audioIsDefault = isDefault;
|
||||
}
|
||||
} else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
|
||||
subtitles.append({static_cast<int>(i) + 1,
|
||||
language(stream),
|
||||
QString::fromUtf8(avcodec_get_name(stream->codecpar->codec_id)),
|
||||
bool(stream->disposition & AV_DISPOSITION_FORCED),
|
||||
isDefault});
|
||||
}
|
||||
}
|
||||
return {TrackSelection::pickTranslationSource(subtitles, {QStringLiteral("en"), audioLanguage}, 0) - 1, {}};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user