mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-21 07:46:38 +00:00
104 lines
2.9 KiB
C
104 lines
2.9 KiB
C
|
/* GStreamer mpl2 format subtitle parser
|
||
|
* Copyright (C) 2006 Kamil Pawlowski <kamilpe gmail com>
|
||
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
|
* version 2 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public
|
||
|
* License along with this library; if not, write to the
|
||
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||
|
* Boston, MA 02111-1307, USA.
|
||
|
*/
|
||
|
|
||
|
#include "mpl2parse.h"
|
||
|
|
||
|
#include <string.h>
|
||
|
|
||
|
/* From http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html
|
||
|
*
|
||
|
* [123][456] Sample subtitle
|
||
|
* [1234][5678] Line 1|Line 2
|
||
|
* [12345][67890] /Italic|Normal
|
||
|
* [12345][67890] /Italic|/Italic
|
||
|
* [12345][67890] Normal|/Italic
|
||
|
*
|
||
|
* (The space between the last ']' bracket and the text appears to be optional)
|
||
|
*/
|
||
|
|
||
|
static gchar *
|
||
|
mpl2_parse_line (ParserState * state, const gchar * line, guint line_num)
|
||
|
{
|
||
|
GString *markup;
|
||
|
gint dc_start, dc_stop;
|
||
|
|
||
|
/* parse subtitle file line */
|
||
|
if (sscanf (line, "[%u][%u]", &dc_start, &dc_stop) != 2) {
|
||
|
GST_WARNING ("failed to extract timestamps for line '%s'", line);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
GST_LOG ("line format %u %u", dc_start, dc_stop);
|
||
|
state->start_time = GST_SECOND / 10 * dc_start;
|
||
|
state->duration = (GST_SECOND / 10 * dc_stop) - state->start_time;
|
||
|
|
||
|
/* skip brackets with timestamps */
|
||
|
line = strchr (line, ']') + 1;
|
||
|
line = strchr (line, ']') + 1;
|
||
|
|
||
|
markup = g_string_new (NULL);
|
||
|
|
||
|
while (1) {
|
||
|
const gchar *format_string;
|
||
|
const gchar *sep;
|
||
|
gchar *line_chunk_escaped;
|
||
|
|
||
|
/* skip leading white spaces */
|
||
|
while (*line == ' ' || *line == '\t')
|
||
|
++line;
|
||
|
|
||
|
/* a '/' at the beginning indicates italics */
|
||
|
if (*line == '/') {
|
||
|
format_string = "<i>%s</i>";
|
||
|
++line;
|
||
|
} else {
|
||
|
format_string = "%s";
|
||
|
}
|
||
|
|
||
|
if ((sep = strchr (line, '|')))
|
||
|
line_chunk_escaped = g_markup_escape_text (line, sep - line);
|
||
|
else
|
||
|
line_chunk_escaped = g_markup_escape_text (line, -1);
|
||
|
|
||
|
GST_LOG ("escaped line: %s", line_chunk_escaped);
|
||
|
g_string_append_printf (markup, format_string, line_chunk_escaped);
|
||
|
|
||
|
g_free (line_chunk_escaped);
|
||
|
|
||
|
if (sep == NULL)
|
||
|
break;
|
||
|
|
||
|
/* move after the '|' and append another line */
|
||
|
g_string_append (markup, "\n");
|
||
|
line = sep + 1;
|
||
|
}
|
||
|
|
||
|
return g_strstrip (g_string_free (markup, FALSE));
|
||
|
}
|
||
|
|
||
|
gchar *
|
||
|
parse_mpl2 (ParserState * state, const gchar * line)
|
||
|
{
|
||
|
gchar *ret;
|
||
|
|
||
|
ret = mpl2_parse_line (state, line, state->state);
|
||
|
++state->state;
|
||
|
return ret;
|
||
|
}
|