Skip to content

Commit

Permalink
Merge pull request #347 from MediaPortal/MP1-5227-Fix_Subtitle_Langua…
Browse files Browse the repository at this point in the history
…ge_Name_in_case_of_difference_between_current_video_file_and_subtitle_engine

MP1-5227: Core: VideoPlayerVMR7, ISubEngine: Enhance subtitle language/suffix extraction in SubtitleLanguage/Name functions
  • Loading branch information
andrewjswan authored Nov 9, 2024
2 parents 11e32f7 + fb61b5a commit 579df78
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 52 deletions.
5 changes: 5 additions & 0 deletions mediaportal/Core/Player/FFDShow/FFDShowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FFDShowEngine : SubSettings, ISubEngine, IPostProcessingEngine
private FFDShowAPI ffdshowAPI;
private bool hasPostProcessing = false;
protected int audiodelayInterval;
private string _SubtitleFilename = null;

public static void DisableFFDShowSubtitles(IGraphBuilder graphBuilder)
{
Expand Down Expand Up @@ -161,6 +162,8 @@ public void SetDevice(IntPtr device) {}

public bool LoadSubtitles(IGraphBuilder graphBuilder, string filename)
{
this._SubtitleFilename = filename;

LoadSettings();

//remove DirectVobSub
Expand Down Expand Up @@ -348,6 +351,8 @@ public bool AutoShow
set { autoShow = value; }
}

public string FileName { get => this._SubtitleFilename; }

#endregion

#region IPostProcessing Members
Expand Down
5 changes: 5 additions & 0 deletions mediaportal/Core/Player/Subtitles/DirectVobSubEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ public class DirectVobSubEngine : SubSettings, ISubEngine
private List<string> SubtitleNames = new List<string>();
private int extCount;
private int current;
private string _SubtitleFilename = null;

#region ISubEngine Members

public void SetDevice(IntPtr device) {}

public bool LoadSubtitles(IGraphBuilder graphBuilder, string filename)
{
this._SubtitleFilename = filename;

FreeSubtitles();
LoadSettings();

Expand Down Expand Up @@ -383,6 +386,8 @@ public bool AutoShow
}
}

public string FileName { get => this._SubtitleFilename; }

#endregion
}

Expand Down
5 changes: 5 additions & 0 deletions mediaportal/Core/Player/Subtitles/MpcEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace MediaPortal.Player.Subtitles
{
public class MpcEngine : SubSettings, ISubEngine
{
private string _SubtitleFilename = null;

protected override void LoadAdvancedSettings(Settings xmlreader)
{
int subPicsBufferAhead = xmlreader.GetValueAsInt("subtitles", "subPicsBufferAhead", 3);
Expand Down Expand Up @@ -117,6 +119,7 @@ public void SetDevice(IntPtr device)

public bool LoadSubtitles(IGraphBuilder graphBuilder, string filename)
{
this._SubtitleFilename = filename;
LoadSettings();
MpcSubtitles.SetDefaultStyle(ref this.defStyle, this.overrideASSStyle);
if (selectionOff)
Expand Down Expand Up @@ -288,6 +291,8 @@ public bool AutoShow
}
}

public string FileName { get => this._SubtitleFilename; }

#endregion

private class MpcSubtitles
Expand Down
7 changes: 7 additions & 0 deletions mediaportal/Core/Player/Subtitles/SubEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public interface ISubEngine
void DelayMinus(int subtitleDelay);

bool AutoShow { get; set; }

string FileName { get; }
}

public class SubEngine
Expand Down Expand Up @@ -123,12 +125,15 @@ public static ISubEngine GetInstance(bool forceinitialize)

public class DummyEngine : ISubEngine
{
private string _SubtitleFilename = null;

#region ISubEngine Members

public void SetDevice(IntPtr device) {}

public bool LoadSubtitles(IGraphBuilder graphBuilder, string filename)
{
this._SubtitleFilename = filename;
DirectVobSubUtil.RemoveFromGraph(graphBuilder);
return false;
}
Expand Down Expand Up @@ -207,6 +212,8 @@ public bool AutoShow
set { }
}

public string FileName { get => this._SubtitleFilename; }

#endregion
}
}
Expand Down
124 changes: 72 additions & 52 deletions mediaportal/Core/Player/VideoPlayerVMR7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,48 +1826,49 @@ public override string SubtitleLanguage(int iStream)
{
string streamName = SubEngine.GetInstance().GetLanguage(iStream);
string langName = SubEngine.GetInstance().GetLanguage(iStream);
string streamNameUND = SubEngine.GetInstance().GetSubtitleName(iStream);
string streamNameUND = SubEngine.GetInstance().GetSubtitleName(iStream);
string strSubtitleFileName = SubEngine.GetInstance().FileName; //filename used to load subtitle engine

if (streamName == null)
{
return Strings.Unknown;
}

//MPC-HC 2.0.0
//"[Local] 4-3 bar test.English-Forced.srt\tEnglish"
Regex regexMPCHC = new Regex(@"^\[([^\]]+)\]\s(?<file>[^\t]+)(\t(?<lng>.+))?");
Match match = regexMPCHC.Match(streamName);
if (match.Success)
{
// Group grLng = match.Groups["lng"];
// if (grLng.Success)
// return grLng.Value;
//
// string strVideNoExt = Path.GetFileNameWithoutExtension(this.m_strCurrentFile);
// string strSubNoExt = Path.GetFileNameWithoutExtension(match.Groups["file"].Value);
// if (strVideNoExt.Equals(strSubNoExt, StringComparison.CurrentCultureIgnoreCase))
// return "Undetermined";

string strVideNoExt = Path.GetFileNameWithoutExtension(this.m_strCurrentFile);
string strSubNoExt = Path.GetFileNameWithoutExtension(match.Groups["file"].Value);
if (strSubNoExt.StartsWith(strVideNoExt, StringComparison.CurrentCultureIgnoreCase))
if (!string.IsNullOrWhiteSpace(strSubtitleFileName))
{
//MPC-HC 2.0.0
//"[Local] 4-3 bar test.English-Forced.srt\tEnglish"
Regex regexMPCHC = new Regex(@"^\[([^\]]+)\]\s(?<file>[^\t]+)(\t(?<lng>.+))?");
Match match = regexMPCHC.Match(streamName);
if (match.Success)
{
if (strSubNoExt.Length > strVideNoExt.Length)
streamName = strSubNoExt.Substring(strVideNoExt.Length + 1);
Group grLng = match.Groups["lng"];
if (grLng.Success)
return grLng.Value; //language parsed by MPC-HC

string strVideNoExt = Path.GetFileNameWithoutExtension(strSubtitleFileName);
string strSubNoExt = Path.GetFileNameWithoutExtension(match.Groups["file"].Value);
if (strVideNoExt.Equals(strSubNoExt, StringComparison.CurrentCultureIgnoreCase))
return "Undetermined"; //no subtitle suffix
else if (strSubNoExt.StartsWith(strVideNoExt, StringComparison.CurrentCultureIgnoreCase))
{
if (strSubNoExt.Length > strVideNoExt.Length)
streamName = strSubNoExt.Substring(strVideNoExt.Length + 1); //Subtitle filename has a suffix
else
streamName = string.Empty;

if (string.IsNullOrWhiteSpace(streamName))
streamName = strVideNoExt;
}
else
streamName = string.Empty;
{
//difference between m_strCurrentFile and ISubEngine.LoadSubtitles call
streamName = strSubNoExt;
}

if (string.IsNullOrWhiteSpace(streamName))
streamName = strVideNoExt;
}
else
{
//difference between m_strCurrentFile and ISubEngine.LoadSubtitles call
streamName = strSubNoExt;
langName = streamName;
streamNameUND = streamName;
}

langName = streamName;
streamNameUND = streamName;
}

// remove prefix, which is added by Haali Media Splitter
Expand Down Expand Up @@ -1941,35 +1942,54 @@ public override string SubtitleName(int iStream)
{
string streamName = SubEngine.GetInstance().GetSubtitleName(iStream);
string streamNameFalse = SubEngine.GetInstance().GetSubtitleName(iStream);
string langName = SubEngine.GetInstance().GetLanguage(iStream);
string langName = SubEngine.GetInstance().GetLanguage(iStream);
string strSubtitleFileName = SubEngine.GetInstance().FileName; //filename used to load subtitle engine

if (streamName == null)
{
return Strings.Unknown;
}

//MPC-HC 2.0.0
//"[Local] 4-3 bar test.English-Forced.srt\tEnglish"
Regex regexMPCHC = new Regex(@"^\[([^\]]+)\]\s(?<file>[^\t]+)(\t(?<lng>.+))?");
Match match = regexMPCHC.Match(streamName);
if (match.Success)
if (!string.IsNullOrWhiteSpace(strSubtitleFileName))
{
string strVideNoExt = Path.GetFileNameWithoutExtension(this.m_strCurrentFile);
string strSubNoExt = Path.GetFileNameWithoutExtension(match.Groups["file"].Value);
if (strSubNoExt.StartsWith(strVideNoExt, StringComparison.CurrentCultureIgnoreCase))
//MPC-HC 2.0.0
//"[Local] 4-3 bar test.English-Forced.srt\tEnglish"
Regex regexMPCHC = new Regex(@"^\[([^\]]+)\]\s(?<file>[^\t]+)(\t(?<lng>.+))?");
Match match = regexMPCHC.Match(streamName);
if (match.Success)
{
if (strSubNoExt.Length > strVideNoExt.Length)
streamName = strSubNoExt.Substring(strVideNoExt.Length + 1);
string strVideNoExt = Path.GetFileNameWithoutExtension(strSubtitleFileName);
string strSubNoExt = Path.GetFileNameWithoutExtension(match.Groups["file"].Value);
if (strSubNoExt.StartsWith(strVideNoExt, StringComparison.CurrentCultureIgnoreCase))
{
if (strSubNoExt.Length > strVideNoExt.Length)
{
//Subtitle filename has a suffix
Group grLng = match.Groups["lng"];
if (grLng.Success)
{
//Try to extract additional suffix following the language
match = Regex.Match(strSubNoExt.Substring(strVideNoExt.Length), @"[-\._](?<lng>[A-Za-z]+)[-\._\[]+(?<suffix>.+?)\]?\z");
if (match.Success)
return match.Groups["suffix"].Value;
else
return string.Empty; //no additional suffix - just language
}

//Unknown subtitle filename suffix
return strSubNoExt.Substring(strVideNoExt.Length + 1);
}
else
return string.Empty;
}
else
streamName = string.Empty;
}
else
{
//difference between m_strCurrentFile and ISubEngine.LoadSubtitles call
streamName = strSubNoExt;
{
//difference between m_strCurrentFile and ISubEngine.LoadSubtitles call
streamName = strSubNoExt;
streamNameFalse = streamName;
langName = streamName;
}
}

streamNameFalse = streamName;
langName = streamName;
}

// remove prefix, which is added by Haali Media Splitter
Expand Down

0 comments on commit 579df78

Please sign in to comment.