Skip to content

Commit

Permalink
subtitle names: simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
courville committed May 11, 2024
1 parent f86af5d commit a5edca5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

package com.archos.mediacenter.video.browser.subtitlesmanager;

import static com.archos.mediacenter.video.browser.subtitlesmanager.SubtitleManager.convertYTSSubNamingExceptions;
import static com.archos.mediacenter.video.browser.subtitlesmanager.SubtitleManager.getSubLanguageFromSubPath;

import android.content.Context;

import com.archos.mediacenter.video.utils.VideoUtils;
Expand All @@ -32,10 +29,7 @@ public class ISO639codes {
private static final Logger log = LoggerFactory.getLogger(ISO639codes.class);

static public String replaceLanguageCodeInString(Context context, String string) {
// treat specific yts external subtitles naming for external subtitles
String result = convertYTSSubNamingExceptions(string);
if (string.equals(result))
result = com.archos.mediacenter.utils.ISO639codes.replaceLanguageCodeInString(getSubLanguageFromSubPath(context, string));
String result = com.archos.mediacenter.utils.ISO639codes.replaceLanguageCodeInString(string);
if (result.startsWith("s_"))
result = VideoUtils.getLanguageString(context, result).toString();
log.debug("replaceLanguageCodeInString: exception string={} result={}", string, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public List<SubtitleFile> listLocalAndRemotesSubtitles(Uri video, boolean addAll
String subtitleFileName = null;
if (SubtitleExtensions.contains(fileExtension.toLowerCase(Locale.US))&&(!fileExtension.toLowerCase(Locale.US).equals("idx") || includeIdx)) {
subtitleFileName = stripExtensionFromName(getName(file.getName()));
subtitleName = getSubLanguageFromSubPath(mContext, file.getUri().getPath());
subtitleName = getSubLanguageFromSubPathAndVideoPath(mContext, file.getUri().getPath(), video.getPath());
if (subtitleFileName.equals(subtitleName)) subtitleName = "SRT";
subList.add(new SubtitleFile(file, subtitleName));
log.trace("listLocalAndRemotesSubtitles: add external " + file.getUri().toString() + " (" + subtitleName +")");
Expand Down Expand Up @@ -545,25 +545,38 @@ public static String convertYTSSubNamingExceptions(String name) {
}
}

public static String getSubLanguageFromSubPath(Context context, String path) {
String subFilenameWithoutExtension = stripExtensionFromName(getName(path));
log.debug("getSubLanguageFromSubPath: " + path + " -> " + subFilenameWithoutExtension);
if (subFilenameWithoutExtension == null) return path;
// get 2 or 3 letter code for language
String lang = convertYTSSubNamingExceptions(subFilenameWithoutExtension);
if (subFilenameWithoutExtension.equals(lang))
lang = getLanguage3(subFilenameWithoutExtension);
public static String getSubLanguageFromSubPathAndVideoPath(Context context, String subPath, String videoPath) {
String subFilenameWithoutExtension = stripExtensionFromName(getName(subPath));
String videoFilenameWithoutExtension = stripExtensionFromName(getName(videoPath));
if (subFilenameWithoutExtension.equals(videoFilenameWithoutExtension)) {
log.debug("getSubLanguageFromSubPathAndVideoPath: video and sub have same name " + subFilenameWithoutExtension + " -> SRT");
return "SRT";
}
// subtract video name from sub name if they start the same (they should) but there could be Subs/en.srt too
String lastPart = null;
if (subFilenameWithoutExtension.startsWith(videoFilenameWithoutExtension + ".")) {
lastPart = subFilenameWithoutExtension.substring(videoFilenameWithoutExtension.length() + 1);
} else lastPart = subFilenameWithoutExtension;
log.debug("getSubLanguageFromSubPathAndVideoPath: ({} - {})={}", subPath, videoPath, lastPart);
// treat yts Simplified.chi.srt Traditional.chi.srt Latin American.spa.srt English.srt Brazilian.por.srt and reuse s_ special strings for this
String lang = convertYTSSubNamingExceptions(lastPart);
String subLanguageName = null;
if (lastPart.equals(lang))
// get 2 or 3 letter code for language
lang = getLanguage3(lastPart);
if (lang != null) {
subLanguageName = getLanguageNameForLetterCode(context, lang);
if (lang.equals(subLanguageName)) lang = null; // match was not a valid 2 or 3 letter code
}
if (lang != null) {
// treat yts SDH and HI as hearing impaired e.g. SDH.eng.HI.srt
// note that subLanguageName is already set to proper value
// treat yts SDH and HI as hearing impaired e.g. SDH.eng.HI.srt: add it to the language name
if (isSubtitleHearingImpaired(subFilenameWithoutExtension))
subLanguageName = getLanguageNameForLetterCode(context, lang) + " (HI)";
else subLanguageName = getLanguageNameForLetterCode(context, lang);
subLanguageName = subLanguageName + " (HI)";
} else { // subLanguageName is likely subFilenameWithoutExtension but cannot compare here because video filename not available
subLanguageName = subFilenameWithoutExtension;
}
log.debug("getSubLanguageFromSubPath: " + path + " -> " + subLanguageName);
log.debug("getSubLanguageFromSubPathAndVideoPath: " + subPath + " -> " + subLanguageName);
return subLanguageName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ public static String getAudioTrackString(VideoMetadata videoMetadata, Resources
VideoMetadata.AudioTrack audio = videoMetadata.getAudioTrack(i);
if (audioTrackNb == 1) { // name of the track only if there is only one
String language = replaceLanguageCodeInString(audio.name);
if (! language.equals("")) sb.append(language).append(SEP); // avoid adding space if language is unknown
if (!language.isEmpty()) sb.append(language).append(SEP); // avoid adding space if language is unknown
}
if (audioTrackNb > 1) { // number and name of the track only if there are more than one track
sb.append(Integer.toString(i + 1)).append('.').append(SEP);
String language = replaceLanguageCodeInString(audio.name);
if (! language.equals("")) sb.append(language).append(SEP); // avoid adding space if language is unknown
if (!language.isEmpty()) sb.append(language).append(SEP); // avoid adding space if language is unknown
}
sb.append(audio.format).append(SEP);
sb.append(audio.channels).append(SEP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,11 @@
import java.util.Map;

import static com.archos.environment.ArchosFeatures.isChromeOS;
import static com.archos.filecorelibrary.FileUtils.getName;
import static com.archos.filecorelibrary.FileUtils.hasManageExternalStoragePermission;
import static com.archos.filecorelibrary.FileUtils.stripExtensionFromName;
import static com.archos.mediacenter.utils.ISO639codes.findLanguageInString;
import static com.archos.mediacenter.utils.ISO639codes.isLanguageInString;
import static com.archos.mediacenter.video.browser.subtitlesmanager.ISO639codes.replaceLanguageCodeInString;
import static com.archos.mediacenter.video.browser.subtitlesmanager.SubtitleManager.getLanguage3;
import static com.archos.mediacenter.video.browser.subtitlesmanager.SubtitleManager.getSubLanguageFromSubPath;
import static com.archos.mediacenter.video.browser.subtitlesmanager.SubtitleManager.getSubLanguageFromSubPathAndVideoPath;
import static com.archos.mediacenter.video.utils.MiscUtils.isEmulator;
import static com.archos.mediacenter.video.utils.VideoPreferencesCommon.DEFAULT_MAX_IFRAME_SIZE;
import static com.archos.mediacenter.video.utils.VideoPreferencesCommon.DEFAULT_STREAM_BUFFER_SIZE;
Expand Down Expand Up @@ -3618,40 +3615,25 @@ public void onSubtitleMetadataUpdated(VideoMetadata vMetadata, int newSubtitleTr
for (int i = 0; i < nbTrack; ++i) {
// name comes from IMediaPlayer (avos) and if not internal it says SRT thus if name="SRT" infer the name from path
// infer language from path if path is provided
lang = getSubLanguageFromSubPath(mContext, getName(vMetadata.getSubtitleTrack(i).path));
log.debug("onSubtitleMetadataUpdated: name={}, path={}, isExternal={}, langFromPath={}", vMetadata.getSubtitleTrack(i).name, vMetadata.getSubtitleTrack(i).path, vMetadata.getSubtitleTrack(i).isExternal, lang);
if (vMetadata.getSubtitleTrack(i).name == null || vMetadata.getSubtitleTrack(i).name.isEmpty()) {
if (vMetadata.getSubtitleTrack(i).isExternal) {
// external subtitle get name from file
lang = getSubLanguageFromSubPathAndVideoPath(mContext, vMetadata.getSubtitleTrack(i).path, vMetadata.getFile().getPath());
log.debug("onSubtitleMetadataUpdated: extsub name={}, path={}, videoPath={}, isExternal={}, langFromPath={}", vMetadata.getSubtitleTrack(i).name, vMetadata.getSubtitleTrack(i).path, vMetadata.getFile().getPath(), vMetadata.getSubtitleTrack(i).isExternal, lang);
if (lang != null) {
log.debug("onSubtitleMetadataUpdated: name is null set track name to lang=" + lang);
log.debug("onSubtitleMetadataUpdated: extsub name is null set track name to lang=" + lang);
mSubtitleInfoController.addTrack(lang);
} else {
log.debug("onSubtitleMetadataUpdated: name and lang are null, set track name to unknown");
} else { // this should never happen
log.warn("onSubtitleMetadataUpdated: extsub name and lang are null, set track name to unknown");
mSubtitleInfoController.addTrack(getText(R.string.unknown_track_name));
}
} else { // name is not null
if ( ! vMetadata.getSubtitleTrack(i).isExternal && (vMetadata.getSubtitleTrack(i).name.equals("SRT") || vMetadata.getSubtitleTrack(i).name.equals("hi"))) {
// name is not meaningful
if (lang != null) {
log.debug("onSubtitleMetadataUpdated: int sub set track name to lang=" + lang);
mSubtitleInfoController.addTrack(lang);
} else {
log.debug("onSubtitleMetadataUpdated: int sub lang is null, set track name to name=" + vMetadata.getSubtitleTrack(i).name);
mSubtitleInfoController.addTrack(replaceLanguageCodeInString(mContext, vMetadata.getSubtitleTrack(i).name));
}
} else { // name can be meaningful add it only if not external
// if ext sub and name is SRT it means that the sub filename is the same as the language reported thus we can use SRT without ambiguity
if (! vMetadata.getSubtitleTrack(i).name.equalsIgnoreCase("srt") && lang != null) {
if (vMetadata.getSubtitleTrack(i).isExternal) {
log.debug("onSubtitleMetadataUpdated: ext sub set track name to lang=" + lang);
mSubtitleInfoController.addTrack(lang);
} else {
log.debug("onSubtitleMetadataUpdated: int sub set track name to name+lang=" + vMetadata.getSubtitleTrack(i).name + " " + lang);
mSubtitleInfoController.addTrack(replaceLanguageCodeInString(mContext, vMetadata.getSubtitleTrack(i).name) + " " + lang);
}
} else {
log.debug("onSubtitleMetadataUpdated: lang is null, set track name to name=" + vMetadata.getSubtitleTrack(i).name);
mSubtitleInfoController.addTrack(replaceLanguageCodeInString(mContext, vMetadata.getSubtitleTrack(i).name));
}
} else {
// internal subtitle get name from name
if (vMetadata.getSubtitleTrack(i).name == null || vMetadata.getSubtitleTrack(i).name.isEmpty()) {
log.debug("onSubtitleMetadataUpdated: intsub name are null/empty, set track name to unknown");
mSubtitleInfoController.addTrack(getText(R.string.unknown_track_name));
} else { // name is not null use it
log.debug("onSubtitleMetadataUpdated: intsub set track name to name=" + vMetadata.getSubtitleTrack(i).name);
mSubtitleInfoController.addTrack(replaceLanguageCodeInString(mContext, vMetadata.getSubtitleTrack(i).name));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private void onResumeReady(Context context, Video video, final String mimeType,
// add support for 3rd party player subs both for local and remote
// for vlc https://wiki.videolan.org/Android_Player_Intents/ subtitles_location path
// for mxplayer/justplayer http://mx.j2inter.com/api subs android.net.Uri[], subs.name String[], subs.filename String[]
Boolean subFound = false;
boolean subFound = false;
String subPath;
File subFile;
String subLanguage;
Expand All @@ -340,7 +340,7 @@ private void onResumeReady(Context context, Video video, final String mimeType,
while (n < listOfSubtitles.size()) {
subPath = listOfSubtitles.get(n);
MxSubFileList.add(subPath);
subLanguage = SubtitleManager.getSubLanguageFromSubPath(context, subPath);
subLanguage = SubtitleManager.getSubLanguageFromSubPathAndVideoPath(context, subPath, video.getFriendlyPath());
MxSubNameList.add(subLanguage);
if (EXTERNAL_PLAYER_HTTP_SUBS) {
subUri = Uri.parse(subPath); // these files are in local nova cache not accessible from 3rd party players
Expand Down

0 comments on commit a5edca5

Please sign in to comment.