diff mbox series

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

Message ID 20220419005829.1886458-1-james.darnley@gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter/vf_subtitles: add an option to choose sub stream by language | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 fail Make failed
andriy/make_x86 fail Make failed
andriy/make_armv7_RPi4 fail Make failed

Commit Message

James Darnley April 19, 2022, 12:58 a.m. UTC
---
 doc/filters.texi           |  5 +++++
 libavfilter/vf_subtitles.c | 23 ++++++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

Comments

Michael Niedermayer April 20, 2022, 6:25 p.m. UTC | #1
On Tue, Apr 19, 2022 at 02:58:29AM +0200, James Darnley wrote:
> ---
>  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
>  
> +@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.
> +@end table

that @end table looks a bit like its not matched 

thx

[...]
Marton Balint June 8, 2022, 10:35 p.m. UTC | #2
On Tue, 19 Apr 2022, James Darnley wrote:

> ---
> 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
>
> +@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.
> +@end table

Using a stream specifier to select a stream would be more general.

Regards,
Marton

> +
> 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
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
diff mbox series

Patch

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
 
+@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.
+@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);