[vlc-commits] Subtitles: Detect stupid SRT convention for languages in filenames

Jean-Baptiste Kempf git at videolan.org
Mon Feb 17 23:31:13 CET 2014


vlc | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Mon Feb 17 23:20:10 2014 +0100| [5abfaffa787416afe35b95c0171f861214bb271a] | committer: Jean-Baptiste Kempf

Subtitles: Detect stupid SRT convention for languages in filenames

This detects filename.xx.srt, where xx is supposed to be the language
This is a longstanding request...

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5abfaffa787416afe35b95c0171f861214bb271a
---

 modules/demux/subtitle.c |   36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c
index a9c3e45..2a7c191 100644
--- a/modules/demux/subtitle.c
+++ b/modules/demux/subtitle.c
@@ -223,6 +223,7 @@ static int Demux( demux_t * );
 static int Control( demux_t *, int, va_list );
 
 static void Fix( demux_t * );
+static char * get_language_from_filename( const char * );
 
 /*****************************************************************************
  * Module initializer
@@ -542,6 +543,17 @@ static int Open ( vlc_object_t *p_this )
     }
     else
         es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
+
+    /* Stupid language detection in the filename */
+    char * psz_language = get_language_from_filename( p_demux->psz_file );
+
+    if( psz_language )
+    {
+        fmt.psz_language = psz_language;
+        msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
+                 p_demux->psz_location );
+    }
+
     if( unicode )
         fmt.subs.psz_encoding = strdup( "UTF-8" );
     char *psz_description = var_InheritString( p_demux, "sub-description" );
@@ -2216,3 +2228,27 @@ static int  ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         strcat( psz_text, "\n" );
     }
 }
+
+/* Matches filename.xx.srt */
+static char * get_language_from_filename( const char * psz_sub_file )
+{
+    char *psz_ret = NULL;
+    char *psz_tmp, *psz_language_begin;
+
+    if( !psz_sub_file ) return NULL;
+    char *psz_work = strdup( psz_sub_file );
+
+    /* Removing extension, but leaving the dot */
+    psz_tmp = strrchr( psz_work, '.' );
+    if( psz_tmp )
+    {
+        psz_tmp[0] = '\0';
+        psz_language_begin = strrchr( psz_work, '.' );
+        if( psz_language_begin )
+            psz_ret = strdup(++psz_language_begin);
+        psz_tmp[0] = '.';
+    }
+
+    free( psz_work );
+    return psz_ret;
+}



More information about the vlc-commits mailing list