* support guessing idx/sub language from .idx file contents

This commit is contained in:
Reinhard Pointner 2014-01-02 15:49:20 +00:00
parent 9e4b38ea9a
commit a61fb4e70a
3 changed files with 26 additions and 14 deletions

View File

@ -94,18 +94,18 @@ public class Language implements Serializable {
return null;
}
public static Language getLanguageByName(String name) {
public static Language findLanguage(String lang) {
for (Language it : availableLanguages()) {
if (name.equalsIgnoreCase(it.getName()))
if (lang.equalsIgnoreCase(it.getISO2()) || lang.equalsIgnoreCase(it.getISO3()) || lang.equalsIgnoreCase(it.getName())) {
return it;
}
}
return null;
}
public static String getISO3LanguageCodeByName(String languageName) {
try {
return Language.getLanguageByName(languageName).getISO3();
return Language.findLanguage(languageName).getISO3();
} catch (Exception e) {
return null;
}

View File

@ -908,16 +908,11 @@ public class CmdlineOperations implements CmdlineInterface {
private Language getLanguage(String lang) throws Exception {
// try to look up by language code
Language language = Language.getLanguage(lang);
Language language = Language.findLanguage(lang);
if (language == null) {
// try too look up by language name
language = Language.getLanguageByName(lang);
if (language == null) {
// unable to lookup language
throw new Exception("Illegal language code: " + lang);
}
// unable to lookup language
throw new Exception("Illegal language code: " + lang);
}
return language;

View File

@ -25,6 +25,8 @@ import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.filebot.Cache;
import net.sourceforge.filebot.Language;
@ -436,11 +438,11 @@ public class MediaBindingBean {
// exclude VobSub from any normal text-based subtitle processing
if (hasExtension(mediaFile, "idx")) {
return null;
return Language.getLanguage(grepLanguageFromSUBIDX(mediaFile));
} else if (hasExtension(mediaFile, "sub")) {
for (File idx : mediaFile.getParentFile().listFiles(new ExtensionFileFilter("idx"))) {
if (isDerived(idx, mediaFile)) {
return null;
return Language.getLanguage(grepLanguageFromSUBIDX(idx));
}
}
}
@ -827,6 +829,21 @@ public class MediaBindingBean {
return hash;
}
private Locale grepLanguageFromSUBIDX(File idx) throws IOException {
String text = new String(readFile(idx), "UTF-8");
// # English
// id: en, index: 0
Pattern pattern = Pattern.compile("^id: (\\w+), index: (\\d+)", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return new Locale(matcher.group(1));
} else {
return null;
}
}
private String getOriginalFileName(File file) {
if (useExtendedFileAttributes()) {
try {