[FFmpeg-devel] [updated PATCH] Make srtenc accept text-only (type SUBTITLE_TEXT) subtitles

Wolfram Gloger wmglo at dent.med.uni-muenchen.de
Thu Aug 30 15:08:51 CEST 2012


After 80days, a rebase to current git.

Please apply or comment.

Thanks,
Wolfram

Wolfram Gloger <wmglo at dent.med.uni-muenchen.de> writes:

>> Make srtenc accept text-only (type SUBTITLE_TEXT) subtitles.
>
> Any comments?
>
> Even in the light of recent discussion, this makes srtenc more generic
> and is a prerequisite for my teletext decoder (which can also optionally
> generate bitmaps, but plain UTF-8 text seems much more attractive when
> transcoding to a suitable format such as mkv).

diff --git a/libavcodec/srtenc.c b/libavcodec/srtenc.c
index dfc4e6c..8553b03 100644
--- a/libavcodec/srtenc.c
+++ b/libavcodec/srtenc.c
@@ -137,6 +137,8 @@ static av_cold int srt_encode_init(AVCodecContext *avctx)
 {
     SRTContext *s = avctx->priv_data;
     s->avctx = avctx;
+    if (!avctx->subtitle_header)
+        return 0;
     s->ass_ctx = ff_ass_split(avctx->subtitle_header);
     return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
 }
@@ -219,6 +221,21 @@ static void srt_end_cb(void *priv)
     srt_print(priv, "\r\n\r\n");
 }
 
+static void srt_print_timing(SRTContext *s, int sc, int ec)
+{
+    int sh, sm, ss;
+    int eh, em, es;
+
+    sh = sc/3600000;  sc -= 3600000*sh;
+    sm = sc/  60000;  sc -=   60000*sm;
+    ss = sc/   1000;  sc -=    1000*ss;
+    eh = ec/3600000;  ec -= 3600000*eh;
+    em = ec/  60000;  ec -=   60000*em;
+    es = ec/   1000;  ec -=    1000*es;
+    srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
+              ++s->count, sh, sm, ss, sc, eh, em, es, ec);
+}
+
 static const ASSCodesCallbacks srt_callbacks = {
     .text             = srt_text_cb,
     .new_line         = srt_new_line_cb,
@@ -245,31 +262,39 @@ static int srt_encode_frame(AVCodecContext *avctx,
 
     for (i=0; i<sub->num_rects; i++) {
 
-        if (sub->rects[i]->type != SUBTITLE_ASS) {
-            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
-            return AVERROR(ENOSYS);
-        }
+        switch (sub->rects[i]->type) {
+        case SUBTITLE_TEXT: {
+            int sc0 = av_rescale(sub->pts, 1000, AV_TIME_BASE);
 
+            if (avctx->codec->id == CODEC_ID_SRT)
+                srt_print_timing(s,
+                                 sc0 + sub->start_display_time,
+                                 sc0 + sub->end_display_time);
+            srt_print(s, sub->rects[i]->text);
+            break;
+        }
+        case SUBTITLE_ASS:
+            if (!s->ass_ctx) {
+                av_log(avctx, AV_LOG_ERROR, "Got SUBTITLE_ASS but no header.\n");
+                return AVERROR_INVALIDDATA;
+            }
         dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
         for (; dialog && num--; dialog++) {
             if (avctx->codec->id == CODEC_ID_SRT) {
-                int sh, sm, ss, sc = 10 * dialog->start;
-                int eh, em, es, ec = 10 * dialog->end;
-                sh = sc/3600000;  sc -= 3600000*sh;
-                sm = sc/  60000;  sc -=   60000*sm;
-                ss = sc/   1000;  sc -=    1000*ss;
-                eh = ec/3600000;  ec -= 3600000*eh;
-                em = ec/  60000;  ec -=   60000*em;
-                es = ec/   1000;  ec -=    1000*es;
-                srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
-                          ++s->count, sh, sm, ss, sc, eh, em, es, ec);
+                srt_print_timing(s, 10 * dialog->start, 10 * dialog->end);
             }
             s->alignment_applied = 0;
             s->dialog_start = s->ptr - 2;
             srt_style_apply(s, dialog->style);
             ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
         }
-    }
+        break;
+        default:
+            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_TEXT, SUBTITLE_ASS types supported.\n");
+            return AVERROR(ENOSYS);
+        }
+
+     }
 
     if (s->ptr == s->buffer)
         return 0;


More information about the ffmpeg-devel mailing list