* use 3-letter language code
* {lang} binding for running language detection on subtitles
This commit is contained in:
parent
453ac607a9
commit
868578cca4
|
@ -6,14 +6,17 @@ import static net.sourceforge.filebot.MediaTypes.*;
|
|||
import static net.sourceforge.filebot.format.Define.*;
|
||||
import static net.sourceforge.filebot.hash.VerificationUtilities.*;
|
||||
import static net.sourceforge.filebot.web.EpisodeFormat.*;
|
||||
import static net.sourceforge.tuned.FileUtilities.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Scanner;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.Element;
|
||||
import net.sourceforge.filebot.WebServices;
|
||||
import net.sourceforge.filebot.hash.HashType;
|
||||
import net.sourceforge.filebot.mediainfo.MediaInfo;
|
||||
import net.sourceforge.filebot.mediainfo.ReleaseInfo;
|
||||
|
@ -283,6 +286,16 @@ public class MediaBindingBean {
|
|||
}
|
||||
|
||||
|
||||
@Define("lang")
|
||||
public Locale detectSubtitleLanguage() throws Exception {
|
||||
// require subtitle file
|
||||
if (!SUBTITLE_FILES.accept(mediaFile))
|
||||
return null;
|
||||
|
||||
return WebServices.OpenSubtitles.detectLanguage(readFile(mediaFile));
|
||||
}
|
||||
|
||||
|
||||
@Define("media")
|
||||
public Object getGeneralMediaInfo() {
|
||||
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.General, 0));
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.Collection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
@ -613,8 +614,17 @@ class VideoHashSubtitleDownloadDialog extends JDialog {
|
|||
|
||||
|
||||
public String getLanguage() {
|
||||
Language lang = Language.getLanguageByName(subtitle.getLanguageName());
|
||||
return lang != null ? lang.getCode() : null;
|
||||
Language language = Language.getLanguageByName(subtitle.getLanguageName());
|
||||
if (language != null) {
|
||||
try {
|
||||
return new Locale(language.getCode()).getISO3Language();
|
||||
} catch (Exception e) {
|
||||
return language.getCode();
|
||||
}
|
||||
}
|
||||
|
||||
// we won't get here, but just in case
|
||||
return subtitle.getLanguageName().replaceAll("\\W", "");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
package net.sourceforge.filebot.web;
|
||||
|
||||
|
||||
import static java.util.Collections.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
|
@ -208,6 +210,18 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
|
|||
}
|
||||
|
||||
|
||||
public Locale detectLanguage(byte[] data) throws Exception {
|
||||
// require login
|
||||
login();
|
||||
|
||||
// detect language
|
||||
List<String> languages = xmlrpc.detectLanguage(data);
|
||||
|
||||
// return first language
|
||||
return languages.size() > 0 ? new Locale(languages.get(0)) : null;
|
||||
}
|
||||
|
||||
|
||||
protected synchronized void login() throws Exception {
|
||||
if (!xmlrpc.isLoggedOn()) {
|
||||
xmlrpc.loginAnonymous();
|
||||
|
@ -244,8 +258,7 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
|
|||
private static final Map<String, String> subLanguageCache = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
|
||||
private String getSubLanguageID(String languageName) throws Exception {
|
||||
// fetch languages if necessary
|
||||
protected synchronized Map<String, String> getSubLanguageMap() throws Exception {
|
||||
synchronized (subLanguageCache) {
|
||||
if (subLanguageCache.isEmpty()) {
|
||||
for (Entry<String, String> entry : xmlrpc.getSubLanguages().entrySet()) {
|
||||
|
@ -256,15 +269,27 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS
|
|||
// some additional special handling
|
||||
subLanguageCache.put("Brazilian", "pob");
|
||||
}
|
||||
|
||||
return unmodifiableMap(subLanguageCache);
|
||||
}
|
||||
|
||||
String id = subLanguageCache.get(languageName);
|
||||
|
||||
if (id == null) {
|
||||
}
|
||||
|
||||
|
||||
protected String getSubLanguageID(String languageName) throws Exception {
|
||||
if (!getSubLanguageMap().containsKey(languageName)) {
|
||||
throw new IllegalArgumentException(String.format("SubLanguageID for '%s' not found", languageName));
|
||||
}
|
||||
|
||||
return id;
|
||||
return getSubLanguageMap().get(languageName);
|
||||
}
|
||||
|
||||
|
||||
protected String getLanguageName(String subLanguageID) throws Exception {
|
||||
for (Entry<String, String> it : getSubLanguageMap().entrySet()) {
|
||||
if (it.getValue().equals(subLanguageID))
|
||||
return it.getKey();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -132,8 +132,13 @@ public final class FileUtilities {
|
|||
detector.setText(new ByteBufferInputStream(data));
|
||||
|
||||
CharsetMatch charset = detector.detect();
|
||||
if (charset != null)
|
||||
return charset.getString();
|
||||
if (charset != null) {
|
||||
try {
|
||||
return charset.getString();
|
||||
} catch (RuntimeException e) {
|
||||
throw new IOException("Failed to read text", e);
|
||||
}
|
||||
}
|
||||
|
||||
// assume UTF-8 by default
|
||||
return Charset.forName("UTF-8").decode(data).toString();
|
||||
|
|
Loading…
Reference in New Issue