[FFmpeg-devel] [PATCH 3/3] lavf/srtenc: do not print more line breaks than necessary.

Clément Bœsch ubitux at gmail.com
Sun Nov 11 00:44:18 CET 2012


Some SubRip packets might have a trailing \n, this is now taken into
account while adding a separator between events to avoid too much line
breaks.

Note that events in the FATE tests have more than one line breaks at the
end.
---
 libavformat/srtenc.c         | 11 +++++++++--
 tests/ref/fate/sub-subripenc |  2 +-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/libavformat/srtenc.c b/libavformat/srtenc.c
index 97b297e..d3c1b1e 100644
--- a/libavformat/srtenc.c
+++ b/libavformat/srtenc.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <string.h>
 #include "avformat.h"
 #include "internal.h"
 #include "libavutil/log.h"
@@ -81,8 +82,14 @@ static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
                        (int)(e /    1000) % 60, (int)(e %  1000));
     }
     avio_write(avf->pb, pkt->data, pkt->size);
-    if (write_ts)
-        avio_write(avf->pb, "\n\n", 2);
+    if (write_ts) {
+        if (pkt->size > 0 && pkt->data[pkt->size - 1] == '\n')
+            avio_write(avf->pb, "\n", 1);       // add event separator
+        else if (pkt->size > 1 && !memcmp(pkt->data+pkt->size-2, "\r\n", 2))
+            avio_write(avf->pb, "\r\n", 2);     // add dos-like event separator
+        else
+            avio_write(avf->pb, "\n\n", 2);     // ends event and add separator
+    }
     avio_flush(avf->pb);
     srt->index++;
     return 0;
diff --git a/tests/ref/fate/sub-subripenc b/tests/ref/fate/sub-subripenc
index 7daa4f5..e629cd1 100644
--- a/tests/ref/fate/sub-subripenc
+++ b/tests/ref/fate/sub-subripenc
@@ -1 +1 @@
-bd520f85238abf9df292374aed54681a
+f85921384b29ec1fad2199ac5cee9c56
-- 
1.8.0



More information about the ffmpeg-devel mailing list