[FFmpeg-devel] [PATCH] avfilter/vf_subtitles: add an option to choose sub stream by language

James Darnley james.darnley at gmail.com
Tue Apr 19 03:58:29 EEST 2022


---
 doc/filters.texi           |  5 +++++
 libavfilter/vf_subtitles.c | 23 ++++++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index a161754233..cfbc807f16 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -21160,6 +21160,11 @@ Override default style or script info parameters of the subtitles. It accepts a
 string containing ASS style format @code{KEY=VALUE} couples separated by ",".
 @end table
 
+ at item language
+Use first stream with the given language, ISO language code. @code{subtitles}
+filter only. Requires the language metadata to be read from the file.
+ at end table
+
 If the first key is not specified, it is assumed that the first value
 specifies the @option{filename}.
 
diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c
index 82e140e986..95f0a940d9 100644
--- a/libavfilter/vf_subtitles.c
+++ b/libavfilter/vf_subtitles.c
@@ -54,6 +54,7 @@ typedef struct AssContext {
     char *fontsdir;
     char *charenc;
     char *force_style;
+    char *language;
     int stream_index;
     int alpha;
     uint8_t rgba_map[4];
@@ -271,6 +272,7 @@ static const AVOption subtitles_options[] = {
     {"stream_index", "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
     {"si",           "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
     {"force_style",  "force subtitle style",         OFFSET(force_style),  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
+    {"language",     "use first stream of this language", OFFSET(language), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS},
     {NULL},
 };
 
@@ -340,9 +342,8 @@ static av_cold int init_subtitles(AVFilterContext *ctx)
         goto end;
 
     /* Locate subtitles stream */
-    if (ass->stream_index < 0)
-        ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
-    else {
+    /* If the user has specified a particular stream use that. */
+    if (ass->stream_index >= 0) {
         ret = -1;
         if (ass->stream_index < fmt->nb_streams) {
             for (j = 0; j < fmt->nb_streams; j++) {
@@ -357,6 +358,22 @@ static av_cold int init_subtitles(AVFilterContext *ctx)
         }
     }
 
+    /* Otherwise find the first stream with the given language code. */
+    else if (ass->language) {
+        ret = -1;
+        for (j = 0; j < fmt->nb_streams; j++) {
+            const AVDictionaryEntry *lang = av_dict_get(fmt->streams[j]->metadata, "language", NULL, 0);
+            if (lang && !strcmp(lang->value, ass->language)) {
+                ret = j;
+                break;
+            }
+        }
+    }
+
+    /* Finally fall back to the "best" stream. */
+    else
+        ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
+
     if (ret < 0) {
         av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
                ass->filename);
-- 
2.35.1



More information about the ffmpeg-devel mailing list