Browse code

lavf/segment: add escaping for filename field of the CSV list file

CSV escaping code is borrowed from ffprobe.c.

Stefano Sabatini authored on 2012/09/01 23:12:29
Showing 2 changed files
... ...
@@ -498,8 +498,8 @@ each line matching the format:
498 498
 @end example
499 499
 
500 500
 @var{segment_filename} is the name of the output file generated by the
501
-muxer according to the provided pattern, and should not contain the
502
-"," character for simplifying parsing operations.
501
+muxer according to the provided pattern. CSV escaping (according to
502
+RFC4180) is applied if required.
503 503
 
504 504
 @var{segment_start_time} and @var{segment_end_time} specify
505 505
 the segment start and end time expressed in seconds.
... ...
@@ -67,6 +67,30 @@ typedef struct {
67 67
     double start_time, end_time;
68 68
 } SegmentContext;
69 69
 
70
+static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
71
+{
72
+    const char *p;
73
+    int quote = 0;
74
+
75
+    /* check if input needs quoting */
76
+    for (p = str; *p; p++)
77
+        if (strchr("\",\n\r", *p)) {
78
+            quote = 1;
79
+            break;
80
+        }
81
+
82
+    if (quote)
83
+        avio_w8(ctx, '"');
84
+
85
+    for (p = str; *p; p++) {
86
+        if (*p == '"')
87
+            avio_w8(ctx, '"');
88
+        avio_w8(ctx, *p);
89
+    }
90
+    if (quote)
91
+        avio_w8(ctx, '"');
92
+}
93
+
70 94
 static int segment_start(AVFormatContext *s)
71 95
 {
72 96
     SegmentContext *seg = s->priv_data;
... ...
@@ -169,7 +193,8 @@ static int segment_end(AVFormatContext *s)
169 169
         if (seg->list_type == LIST_TYPE_FLAT) {
170 170
             avio_printf(seg->list_pb, "%s\n", oc->filename);
171 171
         } else if (seg->list_type == LIST_TYPE_EXT) {
172
-            avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
172
+            print_csv_escaped_str(seg->list_pb, oc->filename);
173
+            avio_printf(seg->list_pb, ",%f,%f\n", seg->start_time, seg->end_time);
173 174
         } else if (seg->list_type == LIST_TYPE_M3U8) {
174 175
             avio_printf(seg->list_pb, "#EXTINF:%f,\n%s\n",
175 176
                         seg->end_time - seg->start_time, oc->filename);