[FFmpeg-devel] [PATCH] libavformat/hlsenc: Add option to correct subtitles duration in webvtt subtitles
Vladimir Kharchevin
vkharchevin at gmail.com
Tue Dec 10 01:28:11 EET 2024
Hi Jan,
Thanks for your respond.
You’re right, it is really makes sense to process this behavior at the WebVTT muxer.
Unfortunately WebVTT specs don’t foresee the feature like ’show until next’.
So it would look like in the patch below.
Thanks,
Vladimir
Signed-off-by: Vladimir Kharchevin <vkharchevin at gmail.com>
---
libavformat/webvttenc.c | 47 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 45 insertions(+), 2 deletions(-)
diff --git a/libavformat/webvttenc.c b/libavformat/webvttenc.c
index e71a6224ae..e9f4b4ab11 100644
--- a/libavformat/webvttenc.c
+++ b/libavformat/webvttenc.c
@@ -28,6 +28,10 @@
#include "internal.h"
#include "mux.h"
+typedef struct WebVTTContext {
+ AVPacket *prev_pkt;
+} WebVTTContext;
+
static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
{
int64_t sec, min, hour;
@@ -59,9 +63,10 @@ static int webvtt_write_header(AVFormatContext *ctx)
static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
{
AVIOContext *pb = ctx->pb;
+ WebVTTContext *webvtt = ctx->priv_data;
size_t id_size, settings_size;
int id_size_int, settings_size_int;
- uint8_t *id, *settings;
+ uint8_t *id, *settings, needsunref = 0;
avio_printf(pb, "\n");
@@ -71,6 +76,21 @@ static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
if (id_size > INT_MAX)
return AVERROR(EINVAL);
+ if(pkt->duration == -1 ||
+ pkt->duration == (uint32_t)-1) {
+ AVPacket *clone_pkt;
+ if(!webvtt->prev_pkt) {
+ webvtt->prev_pkt = av_packet_clone(pkt);
+ return 0;
+ }
+
+ clone_pkt = av_packet_clone(pkt);
+ pkt = webvtt->prev_pkt;
+ webvtt->prev_pkt = clone_pkt;
+ pkt->duration = clone_pkt->pts - pkt->pts;
+ needsunref = 1;
+ }
+
id_size_int = id_size;
if (id && id_size_int > 0)
avio_printf(pb, "%.*s\n", id_size_int, id);
@@ -82,8 +102,11 @@ static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
&settings_size);
- if (settings_size > INT_MAX)
+ if (settings_size > INT_MAX) {
+ if(needsunref)
+ av_packet_unref(pkt);
return AVERROR(EINVAL);
+ }
settings_size_int = settings_size;
if (settings && settings_size_int > 0)
@@ -94,9 +117,26 @@ static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
avio_write(pb, pkt->data, pkt->size);
avio_printf(pb, "\n");
+ if(needsunref)
+ av_packet_unref(pkt);
+
+ return 0;
+}
+
+static int webvtt_init(AVFormatContext *s)
+{
+ WebVTTContext *webvtt = s->priv_data;
+ webvtt->prev_pkt = NULL;
return 0;
}
+static void webvtt_deinit(AVFormatContext *s)
+{
+ WebVTTContext *webvtt = s->priv_data;
+ if(webvtt->prev_pkt)
+ av_packet_free(&webvtt->prev_pkt);
+}
+
const FFOutputFormat ff_webvtt_muxer = {
.p.name = "webvtt",
.p.long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
@@ -108,6 +148,9 @@ const FFOutputFormat ff_webvtt_muxer = {
.p.subtitle_codec = AV_CODEC_ID_WEBVTT,
.write_header = webvtt_write_header,
.write_packet = webvtt_write_packet,
+ .init = webvtt_init,
+ .deinit = webvtt_deinit,
+ .priv_data_size = sizeof(WebVTTContext),
.flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
};
--
2.43.0
> 09.12.2024, в 15:53, Jan Ekström <jeebjp at gmail.com> написал(а):
>
> On Mon, Dec 9, 2024 at 1:24 PM Vladimir Kharchevin
> <vkharchevin at gmail.com> wrote:
>>
>> When importing text subtitles from libzvbi_teletext stream the duration of subtitle
>> frames is -1 by default or fixed value per codec setting.
>> For hls webvtt stream it makes disappear time always 1193:02:47.295 for every subtitle.
>> Suggested to add hls option fix_teletext_durations to fix this behavior and set subtitle
>> duration to the next subtitle packet time.
>>
>> Signed-off-by: Vladimir Kharchevin <vkharchevin at gmail.com>
>
> I would point you towards `fix_sub_duration` together with the related
> heartbeat option for this for CLI usage.
>
> Also as a note, there are already multiple formats that support "show
> until next" functionality:
>
> - US captions
> - ARIB captions from Japan and South America
> - DVB Teletext
>
> So this is not specific to one specific format or decoder.
>
> Finally, the way to support actual pass-through of show-until-next
> values with webvtt would be to check if the specification supports
> handling show-until-next subtitles. This could then be then
> implemented in the WebVTT encoder and muxer. Not in the HLS muxer.
>
> Best regards,
> Jan
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
More information about the ffmpeg-devel
mailing list