1 /* GStreamer mpl2 format subtitle parser
2 * Copyright (C) 2006 Kamil Pawlowski <kamilpe gmail com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "mpl2parse.h"
25 /* From http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html
27 * [123][456] Sample subtitle
28 * [1234][5678] Line 1|Line 2
29 * [12345][67890] /Italic|Normal
30 * [12345][67890] /Italic|/Italic
31 * [12345][67890] Normal|/Italic
33 * (The space between the last ']' bracket and the text appears to be optional)
37 mpl2_parse_line (ParserState * state, const gchar * line, guint line_num)
40 gint dc_start, dc_stop;
42 /* parse subtitle file line */
43 if (sscanf (line, "[%u][%u]", &dc_start, &dc_stop) != 2) {
44 GST_WARNING ("failed to extract timestamps for line '%s'", line);
48 GST_LOG ("line format %u %u", dc_start, dc_stop);
49 state->start_time = GST_SECOND / 10 * dc_start;
50 state->duration = (GST_SECOND / 10 * dc_stop) - state->start_time;
52 /* skip brackets with timestamps */
53 line = strchr (line, ']') + 1;
54 line = strchr (line, ']') + 1;
56 markup = g_string_new (NULL);
60 gchar *line_chunk_escaped;
63 /* skip leading white spaces */
64 while (*line == ' ' || *line == '\t')
67 /* a '/' at the beginning indicates italics */
70 g_string_append (markup, "<i>");
76 if ((sep = strchr (line, '|')))
77 line_chunk_escaped = g_markup_escape_text (line, sep - line);
79 line_chunk_escaped = g_markup_escape_text (line, -1);
81 GST_LOG ("escaped line: %s", line_chunk_escaped);
82 g_string_append (markup, line_chunk_escaped);
84 g_free (line_chunk_escaped);
87 g_string_append (markup, "</i>");
91 /* move after the '|' and append another line */
92 g_string_append (markup, "\n");
96 return g_strstrip (g_string_free (markup, FALSE));
100 parse_mpl2 (ParserState * state, const gchar * line)
104 ret = mpl2_parse_line (state, line, state->state);