mirror of https://github.com/FFmpeg/FFmpeg.git
Enables writing TTML documents or encoded TTML paragraphs as such documents. Additionally, a test for the combined TTML encoder and muxer has been added to validate that the components still work. Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>pull/371/head
parent
18713d22a2
commit
64af14555b
8 changed files with 304 additions and 3 deletions
@ -0,0 +1,174 @@ |
||||
/*
|
||||
* TTML subtitle muxer |
||||
* Copyright (c) 2020 24i |
||||
* |
||||
* This file is part of FFmpeg. |
||||
* |
||||
* FFmpeg is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* FFmpeg is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with FFmpeg; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* TTML subtitle muxer |
||||
* @see https://www.w3.org/TR/ttml1/
|
||||
* @see https://www.w3.org/TR/ttml2/
|
||||
* @see https://www.w3.org/TR/ttml-imsc/rec
|
||||
*/ |
||||
|
||||
#include "avformat.h" |
||||
#include "internal.h" |
||||
#include "libavcodec/ttmlenc.h" |
||||
#include "libavutil/internal.h" |
||||
|
||||
enum TTMLPacketType { |
||||
PACKET_TYPE_PARAGRAPH, |
||||
PACKET_TYPE_DOCUMENT, |
||||
}; |
||||
|
||||
typedef struct TTMLMuxContext { |
||||
enum TTMLPacketType input_type; |
||||
unsigned int document_written; |
||||
} TTMLMuxContext; |
||||
|
||||
static const char ttml_header_text[] = |
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
||||
"<tt\n" |
||||
" xmlns=\"http://www.w3.org/ns/ttml\"\n" |
||||
" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"\n" |
||||
" xmlns:tts=\"http://www.w3.org/ns/ttml#styling\"\n" |
||||
" xml:lang=\"%s\">\n" |
||||
" <body>\n" |
||||
" <div>\n"; |
||||
|
||||
static const char ttml_footer_text[] = |
||||
" </div>\n" |
||||
" </body>\n" |
||||
"</tt>\n"; |
||||
|
||||
static void ttml_write_time(AVIOContext *pb, const char tag[], |
||||
int64_t millisec) |
||||
{ |
||||
int64_t sec, min, hour; |
||||
sec = millisec / 1000; |
||||
millisec -= 1000 * sec; |
||||
min = sec / 60; |
||||
sec -= 60 * min; |
||||
hour = min / 60; |
||||
min -= 60 * hour; |
||||
|
||||
avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"", |
||||
tag, hour, min, sec, millisec); |
||||
} |
||||
|
||||
static int ttml_write_header(AVFormatContext *ctx) |
||||
{ |
||||
TTMLMuxContext *ttml_ctx = ctx->priv_data; |
||||
ttml_ctx->document_written = 0; |
||||
|
||||
if (ctx->nb_streams != 1 || |
||||
ctx->streams[0]->codecpar->codec_id != AV_CODEC_ID_TTML) { |
||||
av_log(ctx, AV_LOG_ERROR, "Exactly one TTML stream is required!\n"); |
||||
return AVERROR(EINVAL); |
||||
} |
||||
|
||||
{ |
||||
AVStream *st = ctx->streams[0]; |
||||
AVIOContext *pb = ctx->pb; |
||||
|
||||
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, |
||||
0); |
||||
const char *printed_lang = (lang && lang->value) ? lang->value : ""; |
||||
|
||||
// Not perfect, but decide whether the packet is a document or not
|
||||
// by the existence of the lavc ttmlenc extradata.
|
||||
ttml_ctx->input_type = (st->codecpar->extradata && |
||||
st->codecpar->extradata_size >= TTMLENC_EXTRADATA_SIGNATURE_SIZE && |
||||
!memcmp(st->codecpar->extradata, |
||||
TTMLENC_EXTRADATA_SIGNATURE, |
||||
TTMLENC_EXTRADATA_SIGNATURE_SIZE)) ? |
||||
PACKET_TYPE_PARAGRAPH : |
||||
PACKET_TYPE_DOCUMENT; |
||||
|
||||
avpriv_set_pts_info(st, 64, 1, 1000); |
||||
|
||||
if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) |
||||
avio_printf(pb, ttml_header_text, printed_lang); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt) |
||||
{ |
||||
TTMLMuxContext *ttml_ctx = ctx->priv_data; |
||||
AVIOContext *pb = ctx->pb; |
||||
|
||||
switch (ttml_ctx->input_type) { |
||||
case PACKET_TYPE_PARAGRAPH: |
||||
// write out a paragraph element with the given contents.
|
||||
avio_printf(pb, " <p\n"); |
||||
ttml_write_time(pb, " begin", pkt->pts); |
||||
avio_w8(pb, '\n'); |
||||
ttml_write_time(pb, " end", pkt->pts + pkt->duration); |
||||
avio_printf(pb, ">"); |
||||
avio_write(pb, pkt->data, pkt->size); |
||||
avio_printf(pb, "</p>\n"); |
||||
break; |
||||
case PACKET_TYPE_DOCUMENT: |
||||
// dump the given document out as-is.
|
||||
if (ttml_ctx->document_written) { |
||||
av_log(ctx, AV_LOG_ERROR, |
||||
"Attempting to write multiple TTML documents into a " |
||||
"single document! The XML specification forbids this " |
||||
"as there has to be a single root tag.\n"); |
||||
return AVERROR(EINVAL); |
||||
} |
||||
avio_write(pb, pkt->data, pkt->size); |
||||
ttml_ctx->document_written = 1; |
||||
break; |
||||
default: |
||||
av_log(ctx, AV_LOG_ERROR, |
||||
"Internal error: invalid TTML input packet type: %d!\n", |
||||
ttml_ctx->input_type); |
||||
return AVERROR_BUG; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int ttml_write_trailer(AVFormatContext *ctx) |
||||
{ |
||||
TTMLMuxContext *ttml_ctx = ctx->priv_data; |
||||
AVIOContext *pb = ctx->pb; |
||||
|
||||
if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) |
||||
avio_printf(pb, ttml_footer_text); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
AVOutputFormat ff_ttml_muxer = { |
||||
.name = "ttml", |
||||
.long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"), |
||||
.extensions = "ttml", |
||||
.mime_type = "text/ttml", |
||||
.priv_data_size = sizeof(TTMLMuxContext), |
||||
.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | |
||||
AVFMT_TS_NONSTRICT, |
||||
.subtitle_codec = AV_CODEC_ID_TTML, |
||||
.write_header = ttml_write_header, |
||||
.write_packet = ttml_write_packet, |
||||
.write_trailer = ttml_write_trailer, |
||||
}; |
@ -0,0 +1,122 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<tt |
||||
xmlns="http://www.w3.org/ns/ttml" |
||||
xmlns:ttm="http://www.w3.org/ns/ttml#metadata" |
||||
xmlns:tts="http://www.w3.org/ns/ttml#styling" |
||||
xml:lang=""> |
||||
<body> |
||||
<div> |
||||
<p |
||||
begin="00:00:00.000" |
||||
end="00:00:00.000">Don't show this text it may be used to insert hidden data</p> |
||||
<p |
||||
begin="00:00:01.500" |
||||
end="00:00:04.500">SubRip subtitles capability tester 1.3o by ale5000<br/>Use VLC 1.1 or higher as reference for most things and MPC Home Cinema for others<br/>This text should be blue<br/>This text should be red<br/>This text should be black<br/>If you see this with the normal font, the player don't (fully) support font face</p> |
||||
<p |
||||
begin="00:00:04.500" |
||||
end="00:00:04.500">Hidden</p> |
||||
<p |
||||
begin="00:00:04.501" |
||||
end="00:00:07.500">This text should be small<br/>This text should be normal<br/>This text should be big</p> |
||||
<p |
||||
begin="00:00:07.501" |
||||
end="00:00:11.500">This should be an E with an accent: È<br/>日本語<br/>This text should be bold, italics and underline<br/>This text should be small and green<br/>This text should be small and red<br/>This text should be big and brown</p> |
||||
<p |
||||
begin="00:00:11.501" |
||||
end="00:00:14.500">This line should be bold<br/>This line should be italics<br/>This line should be underline<br/>This line should be strikethrough<br/>Both lines<br/>should be underline</p> |
||||
<p |
||||
begin="00:00:14.501" |
||||
end="00:00:17.500">><br/>It would be a good thing to<br/>hide invalid html tags that are closed and show the text in them<br/>but show un-closed invalid html tags<br/>Show not opened tags<br/><</p> |
||||
<p |
||||
begin="00:00:17.501" |
||||
end="00:00:20.500">and also<br/>hide invalid html tags with parameters that are closed and show the text in them<br/>but show un-closed invalid html tags<br/>This text should be showed underlined without problems also: 2<3,5>1,4<6<br/>This shouldn't be underlined</p> |
||||
<p |
||||
begin="00:00:20.501" |
||||
end="00:00:21.500">This text should be in the normal position...</p> |
||||
<p |
||||
begin="00:00:21.501" |
||||
end="00:00:22.500">This text should NOT be in the normal position</p> |
||||
<p |
||||
begin="00:00:22.501" |
||||
end="00:00:24.500">Implementation is the same of the ASS tag<br/>This text should be at the<br/>top and horizontally centered</p> |
||||
<p |
||||
begin="00:00:22.501" |
||||
end="00:00:24.500">This text should be at the<br/>middle and horizontally centered</p> |
||||
<p |
||||
begin="00:00:22.501" |
||||
end="00:00:24.500">This text should be at the<br/>bottom and horizontally centered</p> |
||||
<p |
||||
begin="00:00:24.501" |
||||
end="00:00:26.500">This text should be at the<br/>top and horizontally at the left</p> |
||||
<p |
||||
begin="00:00:24.501" |
||||
end="00:00:26.500">This text should be at the<br/>middle and horizontally at the left<br/>(The second position must be ignored)</p> |
||||
<p |
||||
begin="00:00:24.501" |
||||
end="00:00:26.500">This text should be at the<br/>bottom and horizontally at the left</p> |
||||
<p |
||||
begin="00:00:26.501" |
||||
end="00:00:28.500">This text should be at the<br/>top and horizontally at the right</p> |
||||
<p |
||||
begin="00:00:26.501" |
||||
end="00:00:28.500">This text should be at the<br/>middle and horizontally at the right</p> |
||||
<p |
||||
begin="00:00:26.501" |
||||
end="00:00:28.500">This text should be at the<br/>bottom and horizontally at the right</p> |
||||
<p |
||||
begin="00:00:28.501" |
||||
end="00:00:31.500">This could be the most difficult thing to implement</p> |
||||
<p |
||||
begin="00:00:31.501" |
||||
end="00:00:50.500">First text</p> |
||||
<p |
||||
begin="00:00:33.500" |
||||
end="00:00:35.500">Second, it shouldn't overlap first</p> |
||||
<p |
||||
begin="00:00:35.501" |
||||
end="00:00:37.500">Third, it should replace second</p> |
||||
<p |
||||
begin="00:00:36.501" |
||||
end="00:00:50.500">Fourth, it shouldn't overlap first and third</p> |
||||
<p |
||||
begin="00:00:40.501" |
||||
end="00:00:45.500">Fifth, it should replace third</p> |
||||
<p |
||||
begin="00:00:45.501" |
||||
end="00:00:50.500">Sixth, it shouldn't be<br/>showed overlapped</p> |
||||
<p |
||||
begin="00:00:50.501" |
||||
end="00:00:52.500">TEXT 1 (bottom)</p> |
||||
<p |
||||
begin="00:00:50.501" |
||||
end="00:00:52.500">text 2</p> |
||||
<p |
||||
begin="00:00:52.501" |
||||
end="00:00:54.500">Hide these tags:<br/>also hide these tags:<br/>but show this: {normal text}</p> |
||||
<p |
||||
begin="00:00:54.501" |
||||
end="00:01:00.500"><br/>\ N is a forced line break<br/>\ h is a hard space<br/>Normal spaces at the start and at the end of the line are trimmed while hard spaces are not trimmed.<br/>The\hline\hwill\hnever\hbreak\hautomatically\hright\hbefore\hor\hafter\ha\hhard\hspace.\h:-D</p> |
||||
<p |
||||
begin="00:00:54.501" |
||||
end="00:00:56.500"><br/>\h\h\h\h\hA (05 hard spaces followed by a letter)<br/>A (Normal spaces followed by a letter)<br/>A (No hard spaces followed by a letter)</p> |
||||
<p |
||||
begin="00:00:56.501" |
||||
end="00:00:58.500">\h\h\h\h\hA (05 hard spaces followed by a letter)<br/>A (Normal spaces followed by a letter)<br/>A (No hard spaces followed by a letter)<br/>Show this: \TEST and this: \-)</p> |
||||
<p |
||||
begin="00:00:58.501" |
||||
end="00:01:00.500"><br/>A letter followed by 05 hard spaces: A\h\h\h\h\h<br/>A letter followed by normal spaces: A<br/>A letter followed by no hard spaces: A<br/>05 hard spaces between letters: A\h\h\h\h\hA<br/>5 normal spaces between letters: A A<br/><br/>^--Forced line break</p> |
||||
<p |
||||
begin="00:01:00.501" |
||||
end="00:01:02.500">Both line should be strikethrough,<br/>yes.<br/>Correctly closed tags<br/>should be hidden.</p> |
||||
<p |
||||
begin="00:01:02.501" |
||||
end="00:01:04.500">It shouldn't be strikethrough,<br/>not opened tag showed as text.<br/>Not opened tag showed as text.</p> |
||||
<p |
||||
begin="00:01:04.501" |
||||
end="00:01:06.500">Three lines should be strikethrough,<br/>yes.<br/>Not closed tags showed as text</p> |
||||
<p |
||||
begin="00:01:06.501" |
||||
end="00:01:08.500">Both line should be strikethrough but<br/>the wrong closing tag should be showed</p> |
||||
</div> |
||||
</body> |
||||
</tt> |
Loading…
Reference in new issue