From patchwork Sun Jul 4 16:13:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manolis Stamatogiannakis X-Patchwork-Id: 28759 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp3410203ios; Sun, 4 Jul 2021 09:18:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxCM906gid4PWc34mKusvko8DlyiRPwW3G4Et8DGN/0IOyD607xYG5W0W2xJK8uslMuB9d X-Received: by 2002:a17:907:7b9d:: with SMTP id ne29mr1830767ejc.271.1625415534700; Sun, 04 Jul 2021 09:18:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625415534; cv=none; d=google.com; s=arc-20160816; b=AvrkLr/TSrqzRUuPWOts5qNDWe9KEjN/s43iYYALc8ExB9+26i8xo4ZxPpQbXaTn8A g/bDuZacprQsT2Tu5MsaYgjYtt1TePIsRygjyZ4hvxgZDhPRQZ5iF2yHZTSy/xdKgsCc DeuZs5ZLhFW3Py3Uy7k9i87NqKGeWjZ7kMDpQWPRvrlqQg84R8/UIhzO8z2/Iy2A8X+U fwShY+8IpTXjObZWu/dJDqzT+TgGngu6r90CpNBNDLZXJGSgHhynL4czye3z2n3mm9+B /P1q8fOxNhZGUOkUO0Dohs7cpuRWZNh4vwDwlU11FWofCXD5r369FKn4H1zosuMTidCo h9uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=yzhREqucavybvSPXChp7nWGp5XYtUndbHitpt2NiIfs=; b=WWfkQ9dS9t4VtuYlQbePkbB+zZE5eE6lt3ux9vV0Dlw5ZIc58YvOFrsiWqHgi6augp hawRamCODjLVTmhkSbquPUkxE1mDogiwXY9Ru5ttC6pfe7dAyb4v6TlwXV6+jLlHd6Lp oaOgf44pEHyzj/FcpnEZPlRrkw3oZbuxDy6/N0W8rJE18jF4ijndaWWmyE0KIJ2ToB03 Gj+ebssL6Wse2Nai6ewEoNjsTDyA4uDPVlovb9yFVGtOrmv/VafKOTT/prpW8xkpAF5Q msS+gouYnMMYMjwpTrpNAudJZdV/U9aJorROlU+bcvWtWxmpRxl52qgrjAEw4D16V0fY qqEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=P0BwhZht; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id go44si1247951ejc.266.2021.07.04.09.18.53; Sun, 04 Jul 2021 09:18:54 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=P0BwhZht; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BF18868A324; Sun, 4 Jul 2021 19:18:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C92E968A26A for ; Sun, 4 Jul 2021 19:18:42 +0300 (EEST) Received: by mail-wr1-f53.google.com with SMTP id t6so9041785wrm.9 for ; Sun, 04 Jul 2021 09:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wVJZFSLAzruUSZySLOqqm5p1HNyFPdb78gKP2Q2RYM0=; b=P0BwhZhtHHV1bhF5SfT7bq6FLsVUvtqqiq8MEucnJq+oR6KFNcirOgB7FEHC1ghf+P p/rw0uajboYy6+spOwCVpyQgbMvQKxitucTZ/atZgXAa1rWq+NUOJgJ8+Pw7tmOijq1+ lbvlw058MUgKW2lVJpwXdz+ItmJC8nPfedUcPjKsC/92+63jaj3dt38e8Q0nM7l4E1yg 6166huA9Sm5WRgXcaxFm7IFYOPceoxEI6KjGnWGiznRWYgsniyz67CBnu/BLJm2O9FfU mZ+uPszxr7WGWd+2iSRNJUQvQCAkKmar0U0b/eEzaH1JHRdqM3baftsKXFkz2xLJSdBc wffA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wVJZFSLAzruUSZySLOqqm5p1HNyFPdb78gKP2Q2RYM0=; b=ekxB/bZSyqUtJEs63OrcY5+tpkvjkYugu50dWTW0g7hj2yH8wLlvx+TLe1RJOBwaDn lAyYlG2VEbi0CKBopPi0UMBsb9FtHv6sLgkIoARaF/5McOx6j+2lzfR6U8rrtbaiHfXn +EH3JEUfepRz/rVm3p1cXbRANXpIIrnHkO2N+U63plY5w2Fz6XS5mGk4x5d/YD4xB5Hc qa4/X594oSWTqFB+wGp4YZ1l4Cgtr4eDEizUKmwmlJW1h2CwMbDyc3DSiuCZKEybXLP0 CsW7V0zG3FclCaqs6XpPtRo0sktS6bP65ojGdSBcNCu3xd1/AnF1pmDlisQPhTdach0h rvmA== X-Gm-Message-State: AOAM531fXU8IRtRz0AgJu7R7FAsKYIphySbQjQp3fi+87Gv7FyENb4Oi /FC4MLjerrEAQux95QFqEA8TOJ0UDYhXcw== X-Received: by 2002:a05:6402:5204:: with SMTP id s4mr11235305edd.52.1625415206054; Sun, 04 Jul 2021 09:13:26 -0700 (PDT) Received: from wasteland.vu.local ([145.108.189.179]) by smtp.gmail.com with ESMTPSA id f6sm3186022ejx.124.2021.07.04.09.13.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jul 2021 09:13:25 -0700 (PDT) From: Manolis Stamatogiannakis To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Jul 2021 18:13:15 +0200 Message-Id: <20210704161315.25031-2-mstamat@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210704161315.25031-1-mstamat@gmail.com> References: <20210704161315.25031-1-mstamat@gmail.com> Subject: [FFmpeg-devel] [PATCH v1 2/2] avfilter/vf_subtitles: Added shift option for subtitles/ass filters. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Manolis Stamatogiannakis MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KaYZpCmg6mbK Allows shifting of subtitle display times to align them with the video. This avoids having to rewrite the subtitle file in order to display subtitles correctly when input is seeked (-ss). Also handy for minor subtitle timing corrections without rewriting the subtitles file. Signed-off-by: Manolis Stamatogiannakis --- doc/filters.texi | 11 ++++++++ libavfilter/vf_subtitles.c | 55 +++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 61c4cfc150..eebf455692 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -19474,6 +19474,9 @@ Common @ref{subtitles}/@ref{ass} filter options: @item filename, f Set the filename of the subtitle file to read. It must be specified. +@item shift +Shift subtitles timings by the specified amount. + @item original_size Specify the size of the original video, the video for which the ASS file was composed. For the syntax of this option, check the @@ -19487,6 +19490,9 @@ These fonts will be used in addition to whatever the font provider uses. @item alpha Process alpha channel, by default alpha channel is untouched. + +@item shift +Shift subtitles timings by the specified amount. @end table Additional options for @ref{subtitles} filter: @@ -19533,6 +19539,11 @@ To make the subtitles stream from @file{sub.srt} appear in 80% transparent blue subtitles=sub.srt:force_style='Fontname=DejaVu Serif,PrimaryColour=&HCCFF0000' @end example +To re-sync subtitles after seeking the input e.g. with @code{-ss 20:20}, use: +@example +subtitles=filename=sub.srt:shift='-20\:20' +@end example + @section super2xsai Scale the input by 2x and smooth using the Super2xSaI (Scale and diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c index ab32e1b7f3..2c7ce267e1 100644 --- a/libavfilter/vf_subtitles.c +++ b/libavfilter/vf_subtitles.c @@ -52,6 +52,7 @@ typedef struct AssContext { char *filename; char *fontsdir; char *charenc; + int64_t shift; char *force_style; int stream_index; int alpha; @@ -66,11 +67,12 @@ typedef struct AssContext { #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM #define COMMON_OPTIONS \ - {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ - {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ + {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ + {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ + {"shift", "shift subtitles timing", OFFSET(shift), AV_OPT_TYPE_DURATION, {.i64 = 0 }, INT64_MIN, INT64_MAX, FLAGS }, \ /* libass supports a log level ranging from 0 to 7 */ static const int ass_libavfilter_log_level_map[] = { @@ -103,6 +105,11 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR(EINVAL); } + if (ass->shift != 0) { + ass->shift = av_rescale_q(ass->shift, AV_TIME_BASE_Q, av_make_q(1, 1000)); + av_log(ctx, AV_LOG_INFO, "Shifting subtitles by %0.3fsec.\n", ass->shift/1000.0); + } + ass->library = ass_library_init(); if (!ass->library) { av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n"); @@ -228,6 +235,8 @@ AVFILTER_DEFINE_CLASS(ass); static av_cold int init_ass(AVFilterContext *ctx) { + int eid, nskip; + ASS_Event *event; AssContext *ass = ctx->priv; int ret = init(ctx); @@ -244,6 +253,25 @@ static av_cold int init_ass(AVFilterContext *ctx) ass->filename); return AVERROR(EINVAL); } + + /* Shift subtitles. */ + nskip = 0; + for (eid = 0; eid < ass->track->n_events; eid++) { + event = &ass->track->events[eid]; + event->Start += ass->shift; + if (event->Start + event->Duration < 0) { + ass_free_event(ass->track, eid); + nskip++; + continue; + } else if (nskip > 0) { + av_log(ctx, AV_LOG_INFO, "Skipped %d subtitles out of time range.\n", nskip); + memmove(event - nskip, event, (ass->track->n_events - eid) * sizeof(ASS_Event)); + ass->track->n_events -= nskip; + eid -= nskip; + nskip = 0; + } + } + return 0; } @@ -298,7 +326,7 @@ AVFILTER_DEFINE_CLASS(subtitles); static av_cold int init_subtitles(AVFilterContext *ctx) { - int j, ret, sid; + int j, ret, sid, nskip; int k = 0; AVDictionary *codec_opts = NULL; AVFormatContext *fmt = NULL; @@ -449,6 +477,7 @@ static av_cold int init_subtitles(AVFilterContext *ctx) ass_process_codec_private(ass->track, dec_ctx->subtitle_header, dec_ctx->subtitle_header_size); + nskip = 0; while (av_read_frame(fmt, &pkt) >= 0) { int i, got_subtitle; AVSubtitle sub = {0}; @@ -459,8 +488,18 @@ static av_cold int init_subtitles(AVFilterContext *ctx) av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", av_err2str(ret)); } else if (got_subtitle) { - const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); + /* Shift subtitles. */ + const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)) + ass->shift; const int64_t duration = sub.end_display_time; + + if (start_time + duration < 0) { + nskip++; + goto pkt_end; + } else if (nskip > 0) { + av_log(ctx, AV_LOG_INFO, "Skipped %d subtitles out of time range.\n", nskip); + nskip = 0; + } + for (i = 0; i < sub.num_rects; i++) { char *ass_line = sub.rects[i]->ass; if (!ass_line) @@ -470,6 +509,8 @@ static av_cold int init_subtitles(AVFilterContext *ctx) } } } + +pkt_end: av_packet_unref(&pkt); avsubtitle_free(&sub); }