Skip to content

Instantly share code, notes, and snippets.

@Gilgames13
Created October 31, 2012 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gilgames13/3990108 to your computer and use it in GitHub Desktop.
Save Gilgames13/3990108 to your computer and use it in GitHub Desktop.
OMXLauncher by DP
/*****************************************************************/
/* OMXLauncher for omxplayer Torarin fork with subtitles support */
/* Author: Dimitris Pappas */
/* Version: 0.2 */
/* Date: 30/10/2012 */
/*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
/* Available locales. Currently Chinese (both Traditional and Simplified), Korean and Japanese are
actually inactive, until a more elegant implementation is improvised. Current code was based upon
ISO and Windows encodings, which are most common in non-unicode .srt files. */
enum {LOC_ARABIC, LOC_BALTIC, LOC_CENT_EUR, LOC_CHN_SIM, LOC_CHN_TRD, LOC_CYRILLIC, LOC_GREEK,
LOC_HEBREW, LOC_JAP, LOC_KOR, LOC_TURK, LOC_THAI, LOC_VIETNAM, LOC_DEFAULT};
int locSearch(char *loc);
int srtExists(char *srtName);
int main(int argc,char *argv[])
{
char *tempostring, *finalCommand,*conversionCommand, encodingString[400],*loc,c = '1';
FILE *encodingFile = NULL;
int i=-1,j=0, encodingType = 0;
if (argc == 2)
{
tempostring = malloc((strlen(argv[1])+1)*sizeof(char));
conversionCommand = malloc(3*(strlen(argv[1])+1)*sizeof(char));
finalCommand = malloc((strlen(argv[0])+1)*sizeof(char)+(4*strlen(argv[1])+1)*sizeof(char));
strcpy(tempostring,argv[1]);
do {
i = i + 1;
c = tempostring[i];
}
while(tempostring[i] != '\0');
i = i - 3;
tempostring[i]='s'; //tempostring holds the name of the equivalent .srt file, having the same name as the movie file.
tempostring[i+1]='r';
tempostring[i+2]='t';
/* Trying to copy original file and make it UTF-8,if it's not already */
if(srtExists(tempostring) == 1)
{
strcpy(conversionCommand,"file \"");
strcat(conversionCommand,tempostring);
strcat(conversionCommand,"\" > /tmp/filecmd.txt");
system(conversionCommand);
}
else //File does not have an srt file with the same name, so we launch omxplayer without it.
{
strcpy(finalCommand,"lxterminal --command \"");
strcat(finalCommand,"omxplayer -o hdmi \'");
strcat(finalCommand,argv[1]);
strcat(finalCommand,"\'");
system(finalCommand);
free(tempostring);
free(finalCommand);
free(conversionCommand);
tempostring=NULL;
finalCommand=NULL;
conversionCommand=NULL;
return 0;
}
/* Now we have the .srt encoding written in filecmd.txt in /tmp */
if(!(encodingFile=fopen("/tmp/filecmd.txt","r+")))
printf("Error, could not identify encoding or no subs file found! Using the file unconverted (crossing fingers!)\n");
else
{
fgets(encodingString,400,encodingFile);
if(strstr(encodingString,"ISO-8859"))
encodingType=1; //ISO
else if(strstr(encodingString,"Non-ISO extended-ASCII"))
encodingType=2; //Windows
else
{
encodingType=0; //UTF-8 already
printf("UTF-8 subtitles file found, no conversion necessary.\n");
}
fclose(encodingFile);
system("rm /tmp/filecmd.txt");
}
/* If encoding is not UTF-8, then we convert the .srt and store to /tmp folder. We make an assumption for the .srt file's encoding
judging by the system's locale.
omxplayer will be launched with this temporary subtitle as input. */
if(encodingType != 0)
{
loc = setlocale(LC_ALL,""); //We find out about system's locale.
j=0;
while (loc[j] != '.') //Since the locale can be either in eg el_GR.UTF-8 format or ga.UTF-8, we stop at the dot.
j++;
loc[j] = '\0'; //That way we have strictly the locale eg "el_GR"
printf("Assuming subtitle language by consulting current system locale: %s\n",loc);
if(encodingType == 1)
{
switch( locSearch(loc) ) //ISO encoding
{
case LOC_CENT_EUR:
strcpy(conversionCommand,"iconv -f ISO-8859-2 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-2 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_CYRILLIC:
strcpy(conversionCommand,"iconv -f ISO-8859-5 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-5 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_GREEK:
strcpy(conversionCommand,"iconv -f ISO-8859-7 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-7 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_TURK:
strcpy(conversionCommand,"iconv -f ISO-8859-9 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-9 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_HEBREW:
strcpy(conversionCommand,"iconv -f ISO-8859-8 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-8 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_ARABIC:
strcpy(conversionCommand,"iconv -f ISO-8859-6 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-6 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_BALTIC:
strcpy(conversionCommand,"iconv -f ISO-8859-4 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-4 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_THAI:
strcpy(conversionCommand,"iconv -f ISO-8859-11 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-11 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_DEFAULT:
strcpy(conversionCommand,"iconv -f ISO-8859-1 -t UTF-8//TRANSLIT \"");
printf("ISO-8859 subtitles file found, assuming ISO-8859-1 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
}
}
else if(encodingType == 2) //Windows encoding
{
switch( locSearch(loc) )
{
case LOC_CENT_EUR:
strcpy(conversionCommand,"iconv -f WINDOWS-1250 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1250 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_CYRILLIC:
strcpy(conversionCommand,"iconv -f WINDOWS-1251 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1251 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_GREEK:
strcpy(conversionCommand,"iconv -f WINDOWS-1253 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1253 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_TURK:
strcpy(conversionCommand,"iconv -f WINDOWS-1254 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1254 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_HEBREW:
strcpy(conversionCommand,"iconv -f WINDOWS-1255 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1255 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_ARABIC:
strcpy(conversionCommand,"iconv -f WINDOWS-1256 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1256 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_BALTIC:
strcpy(conversionCommand,"iconv -f WINDOWS-1257 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1257 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_VIETNAM:
strcpy(conversionCommand,"iconv -f WINDOWS-1258 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1258 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_THAI:
strcpy(conversionCommand,"iconv -f WINDOWS-874 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp874 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
case LOC_DEFAULT:
strcpy(conversionCommand,"iconv -f WINDOWS-1252 -t UTF-8//TRANSLIT \"");
printf("Windows ASCII subtitiles found, assuming cp1252 codepage.\nConverted to UTF-8 and stored to /tmp.\n");
break;
}
}
strcat(conversionCommand,tempostring);
strcat(conversionCommand,"\" -o /tmp/tempsub.srt");
system(conversionCommand);
}
if(encodingType != 0) //If it's not UTF-8, then we use the modified temp one.
strcpy(tempostring,"/tmp/tempsub.srt");
strcpy(finalCommand,"lxterminal --command \"");
strcat(finalCommand,"omxplayer -o hdmi \'");
strcat(finalCommand,argv[1]);
strcat(finalCommand,"\' --subtitles \'");
strcat(finalCommand,tempostring);
strcat(finalCommand,"\' --align center\"");
system(finalCommand);
free(tempostring);
free(finalCommand);
free(conversionCommand);
tempostring=NULL;
finalCommand=NULL;
conversionCommand=NULL;
}
else
{
printf("\n\n\t\tOmxlauncher for omxplayer by DP\n\t\t===============================\n");
printf("\nUsage: Just call omxlauncher with the path to the video file.");
printf("\n Omxlauncher will find the same-named .srt subtitle, convert");
printf("\n it to UTF-8 if necessary, and then play it back! Enjoy!\n\n\t\t\tGilgames\n\n");
}
return 0;
}
int locSearch(char * loc)
{
if(strcmp(loc, "sq_AL") == 0 || strcmp(loc, "bs_BA") == 0 || strcmp(loc, "hr_HR") == 0 || strcmp(loc, "cs_CZ") == 0 || strcmp(loc, "hu") == 0 || strcmp(loc, "pl") == 0 || strcmp(loc, "ro_RO") == 0 || strcmp(loc, "sk_SK") == 0 || strcmp(loc, "sl_SI") == 0 )
return LOC_CENT_EUR;
else if(strcmp(loc, "be_BY") == 0 || strcmp(loc, "bg_BG") == 0 || strcmp(loc, "ru_RU") == 0 || strcmp(loc, "sr_CS") == 0 || strcmp(loc, "uk_UA") == 0)
return LOC_CYRILLIC;
else if(strcmp(loc, "el_GR") == 0)
return LOC_GREEK;
else if(strcmp(loc, "tr_TR") == 0)
return LOC_TURK;
else if(strcmp(loc, "he_IL") == 0)
return LOC_HEBREW;
else if(strcmp(loc, "ar_SA") == 0 || strcmp(loc, "fa_IR") == 0)
return LOC_ARABIC;
else if(strcmp(loc, "et_EE") == 0 || strcmp(loc, "lo_LA") == 0 || strcmp(loc, "lt_LT") == 0 || strcmp(loc, "lat") == 0)
return LOC_BALTIC;
else if(strcmp(loc, "vi_VN") == 0)
return LOC_VIETNAM;
else if(strcmp(loc, "zh_CN") == 0)
return LOC_CHN_SIM;
else if(strcmp(loc, "zh_TW") == 0)
return LOC_CHN_TRD;
else if(strcmp(loc, "ja_JP") == 0)
return LOC_JAP;
else if(strcmp(loc, "ko_KR") == 0)
return LOC_KOR;
else if(strcmp(loc, "th_TH") == 0)
return LOC_THAI;
else
return LOC_DEFAULT; // Default is pretty much anything else (mostly Western Europe etc), ISO-8859-1 and WINDOWS-1252
}
int srtExists(char *srtName)
{
FILE *srt;
if ((srt = fopen(srtName, "r")))
{
fclose(srt);
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment