[FFmpeg-devel] [PATCH] avformat/assdec: output ASS packets

Clément Bœsch u at pkh.me
Sun Sep 14 10:01:55 CEST 2014


After this the order from the original file is stored through readorder
when doing ffmpeg -i input.ass -c copy output.mkv.

And now that the ASS muxer honors the ReadOrder, extracting the ass back
(without transcoding) restores the original order.

TODO: micro bump?
---
 libavformat/assdec.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/libavformat/assdec.c b/libavformat/assdec.c
index a5f792a..0d2c57b 100644
--- a/libavformat/assdec.c
+++ b/libavformat/assdec.c
@@ -29,6 +29,7 @@
 
 typedef struct ASSContext {
     FFDemuxSubtitlesQueue q;
+    unsigned readorder;
 } ASSContext;
 
 static int ass_probe(AVProbeData *p)
@@ -85,10 +86,35 @@ static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
     return pos;
 }
 
+static int reformat_line(ASSContext *ass, AVBPrint *dst, const AVBPrint *src)
+{
+    av_bprint_clear(dst);
+    if (strcmp(src->str, "Dialogue: ")) {
+        char *p = src->str + 10;
+
+        /* extract Layer or Marked */
+        long layer = strtol(p, &p, 10);
+        if (*p != ',')
+            return -1;
+        p++;
+
+        p = strchr(p, ','); if (!p) return -1; p++; // skip Start
+        p = strchr(p, ','); if (!p) return -1; p++; // skip End
+
+        av_bprintf(dst, "%u,%ld,%s", ass->readorder++, layer, p);
+
+        while (dst->str[dst->len - 1] == '\r' ||
+               dst->str[dst->len - 1] == '\n')
+            dst->str[--dst->len] = 0;
+        return 0;
+    }
+    return -1;
+}
+
 static int ass_read_header(AVFormatContext *s)
 {
     ASSContext *ass = s->priv_data;
-    AVBPrint header, line;
+    AVBPrint header, line, rline;
     int header_remaining, res = 0;
     AVStream *st;
     FFTextReader tr;
@@ -99,12 +125,13 @@ static int ass_read_header(AVFormatContext *s)
         return AVERROR(ENOMEM);
     avpriv_set_pts_info(st, 64, 1, 100);
     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
-    st->codec->codec_id   = AV_CODEC_ID_SSA;
+    st->codec->codec_id   = AV_CODEC_ID_ASS;
 
     header_remaining = INT_MAX;
 
     av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
     av_bprint_init(&line,   0, AV_BPRINT_SIZE_UNLIMITED);
+    av_bprint_init(&rline,  0, AV_BPRINT_SIZE_UNLIMITED);
 
     for (;;) {
         int64_t pos = get_line(&line, &tr);
@@ -125,9 +152,10 @@ static int ass_read_header(AVFormatContext *s)
             int duration = -1;
             AVPacket *sub;
 
-            if (read_ts(line.str, &ts_start, &duration) < 0)
+            if (read_ts(line.str, &ts_start, &duration) < 0 ||
+                reformat_line(ass, &rline, &line) < 0)
                 continue;
-            sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
+            sub = ff_subtitles_queue_insert(&ass->q, rline.str, rline.len, 0);
             if (!sub) {
                 res = AVERROR(ENOMEM);
                 goto end;
@@ -139,6 +167,7 @@ static int ass_read_header(AVFormatContext *s)
     }
 
     av_bprint_finalize(&line, NULL);
+    av_bprint_finalize(&rline, NULL);
 
     res = avpriv_bprint_to_extradata(st->codec, &header);
     if (res < 0)
-- 
2.1.0



More information about the ffmpeg-devel mailing list