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

Wolfram Gloger wmglo at dent.med.uni-muenchen.de
Sat May 26 12:18:11 CEST 2012


Michael Niedermayer <michaelni at gmx.at> writes:

> patch? did i miss some patch

Sorry, sent to wrong address.  Below

> why?

Experience :)  I'll try my best, though.

Regards,
Wolfram

Make srtenc accept text-only (type SUBTITLE_TEXT) subtitles.

Signed-off-by: Wolfram Gloger <wmglo at dent.med.uni-muenchen.de>
---
 srtenc.c |   52 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 14 deletions(-)
Index: ffmpeg-HEAD-0da28d6/libavcodec/srtenc.c
===================================================================
--- ffmpeg-HEAD-0da28d6.orig/libavcodec/srtenc.c	2012-05-25 15:32:28.000000000 +0200
+++ ffmpeg-HEAD-0da28d6/libavcodec/srtenc.c	2012-05-25 13:46:02.000000000 +0200
@@ -137,6 +137,8 @@
 {
     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 @@
     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,28 +262,35 @@
 
     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);
+
+            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++) {
-            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)


More information about the ffmpeg-devel mailing list