public static ExternalSubtitlesInfo CollectExternalSubtitles(string movieFilename, bool doJustDetection) { ExternalSubtitlesInfo _result = new ExternalSubtitlesInfo(); List <FileInfo> _temp = new FilesCollector().CollectFiles(Path.GetDirectoryName(movieFilename), false, SubtitlesManager.SubtitlesSupported).ToList <FileInfo>(); if (_temp != null && _temp.Count != 0) { bool _hasIdxSub = false; // detect if .idx file is present foreach (FileInfo _info in _temp) { if (_info.Extension == ".idx") { _hasIdxSub = true; break; } } foreach (FileInfo _info in _temp) { // if we have an .idx file to process skip existing .sub as it does not contain a candidate sub if (_hasIdxSub && _info.Extension == ".sub") { continue; } ExtSubData _data = null; if (_info.Extension == ".idx") { // process special case: .idx/.sub // try to load the file string _input = File.ReadAllText(_info.FullName); if (!string.IsNullOrEmpty(_input)) { Regex _reg = new Regex("id: ([a-zA-Z]+), index:", RegexOptions.Multiline | RegexOptions.IgnoreCase); foreach (Match _match in _reg.Matches(_input)) { string _code = _match.Groups[1].Value.Trim(); if (!string.IsNullOrEmpty(_code) && _code.Length == 2 && !CodeExists(_code, _result.SubFiles)) { _data = new ExtSubData(); _data.Format = "IDX/SUB"; _data.Filename = _info.FullName; _data.TwoLetterLanguageCode = _code; _data.MovieName = Path.GetFileNameWithoutExtension(_info.Name); if (string.Compare(_data.MovieName, Path.GetFileNameWithoutExtension(movieFilename), true, CultureInfo.InvariantCulture) == 0) { _result.SubFiles.Add(_data); if (doJustDetection) { return(_result); } } } } } _input = null; } else { // process standard subtitles // remove the extension string _name = Path.GetFileNameWithoutExtension(_info.Name).ToLowerInvariant(); if (!string.IsNullOrEmpty(_name)) { _data = new ExtSubData(); _data.Format = Path.GetExtension(_info.Name).Trim('.').ToUpperInvariant(); _data.Filename = _info.FullName; // try to get the language code string _code = Path.GetExtension(_name).Trim('.'); if (!string.IsNullOrEmpty(_code)) { CultureInfo _ci = _code.Length == 2 ? Helpers.GetCultureInfo(_code) : Helpers.GetCultureInfoFromEnglishName(_code); if (_ci != null && _ci != CultureInfo.InvariantCulture) { // the file has a language code _data.TwoLetterLanguageCode = _ci.TwoLetterISOLanguageName; // what is before the language code is the moviename _data.MovieName = Path.GetFileNameWithoutExtension(_name); } } if (string.IsNullOrEmpty(_data.MovieName)) { // there is no language code, use the default one OR use Google Translate? _data.TwoLetterLanguageCode = FileManager.Configuration.Options.MovieSheetsOptions.DefaultExternalSubtitlesLanguage; // the movie file name is easy _data.MovieName = Path.GetFileNameWithoutExtension(_info.Name); } if (string.Compare(_data.MovieName, Path.GetFileNameWithoutExtension(movieFilename), true, CultureInfo.InvariantCulture) == 0) { _result.SubFiles.Add(_data); if (doJustDetection) { return(_result); } } } } } } return(_result); }
private static void ProcessSubtitles(MediaInfoData result, MediaInfo mi, string filePath) { // check embedded subtitles // process each sub and add it to the list if (mi != null && !IsISO(filePath)) { try { int _subsCnt = mi.Count_Get(StreamKind.Text); if (_subsCnt != 0) { for (int _i = 0; _i < _subsCnt; _i++) { EmbeddedSubtitle _sub = new EmbeddedSubtitle(); _sub.Format = mi.Get(StreamKind.Text, _i, "Format"); _sub.Language = mi.Get(StreamKind.Text, _i, "Language"); if (string.IsNullOrEmpty(_sub.Language)) { _sub.Language = mi.Get(StreamKind.Text, _i, "Title"); } _sub.Language = string.IsNullOrEmpty(_sub.Language) ? FileManager.Configuration.Options.MovieSheetsOptions.DefaultExternalSubtitlesLanguage : _sub.Language; if (PopulateLanguages(_sub)) { if (!string.IsNullOrEmpty(_sub.Language) && !string.IsNullOrEmpty(_sub.Format)) { result.EmbeddedSubtitles.Add(_sub); } } } } } catch (Exception ex) { Loggy.Logger.DebugException("Embedded subs: ", ex); } } // process also the external subtitles try { ExternalSubtitlesInfo _subtitles = CollectExternalSubtitles(filePath, false); if (_subtitles.HasExternalSubtitles) { foreach (ExtSubData _subData in _subtitles.SubFiles) { EmbeddedSubtitle _sub = new EmbeddedSubtitle(); _sub.Format = _subData.Format; _sub.Language = _subData.TwoLetterLanguageCode; if (PopulateLanguages(_sub)) { if (!string.IsNullOrEmpty(_sub.Language) && !string.IsNullOrEmpty(_sub.Format)) { result.ExternalSubtitlesList.Add(_sub); } } } } } catch (Exception ex) { Loggy.Logger.DebugException("External subs: ", ex); } }