diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 9b29756ba..941744a19 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -23692,14 +23692,17 @@ namespace Nikse.SubtitleEdit.Forms else if (mediaPlayer.VideoPlayer is LibMpvDynamic libMpv) { openSecondSubtitleToolStripMenuItem.Visible = true; - int numberOfTracks = libMpv.AudioTrackCount; + var audioTracks = libMpv.AudioTracks; VideoAudioTrackNumber = libMpv.AudioTrackNumber; - if (numberOfTracks > 1) + if (audioTracks.Count > 1) { toolStripMenuItemSetAudioTrack.DropDownItems.Clear(); - for (int i = 0; i < numberOfTracks; i++) + for (int i = 0; i < audioTracks.Count; i++) { - toolStripMenuItemSetAudioTrack.DropDownItems.Add((i + 1).ToString(CultureInfo.InvariantCulture), null, ChooseAudioTrack); + var at = audioTracks[i]; + var trackText = string.IsNullOrWhiteSpace(at.Value) ? at.Key.ToString(CultureInfo.InvariantCulture) : "Track " + at.Key + " - " + char.ToUpper(at.Value[0]) + at.Value.Substring(1); + toolStripMenuItemSetAudioTrack.DropDownItems.Add(trackText, null, ChooseAudioTrack); + toolStripMenuItemSetAudioTrack.DropDownItems[toolStripMenuItemSetAudioTrack.DropDownItems.Count - 1].Tag = at.Key.ToString(CultureInfo.InvariantCulture); if (i == VideoAudioTrackNumber) { toolStripMenuItemSetAudioTrack.DropDownItems[toolStripMenuItemSetAudioTrack.DropDownItems.Count - 1].Select(); @@ -23745,7 +23748,7 @@ namespace Nikse.SubtitleEdit.Forms else if (mediaPlayer.VideoPlayer is LibMpvDynamic libMpv) { var item = sender as ToolStripItem; - int number = int.Parse(item.Text); + int number = int.Parse(item.Tag.ToString()); number--; libMpv.AudioTrackNumber = number; VideoAudioTrackNumber = number; diff --git a/src/Logic/VideoPlayers/LibMpvDynamic.cs b/src/Logic/VideoPlayers/LibMpvDynamic.cs index 77f4a7e06..703c9e3dc 100644 --- a/src/Logic/VideoPlayers/LibMpvDynamic.cs +++ b/src/Logic/VideoPlayers/LibMpvDynamic.cs @@ -326,29 +326,30 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers public override bool IsPlaying => !IsPaused; - private List _audioTrackIds; - public int AudioTrackCount + private List> _audioTrackIds; + public List> AudioTracks { get { if (_audioTrackIds == null) { - _audioTrackIds = new List(); + _audioTrackIds = new List>(); var lpBuffer = IntPtr.Zero; _mpvGetPropertyString(_mpvHandle, GetUtf8Bytes("track-list"), MpvFormatString, ref lpBuffer); string trackListJson = Marshal.PtrToStringAnsi(lpBuffer); foreach (var json in Json.ReadObjectArray(trackListJson)) { string trackType = Json.ReadTag(json, "type"); - string id = Json.ReadTag(json, "id"); + int id = int.Parse(Json.ReadTag(json, "id")); if (trackType == "audio") { - _audioTrackIds.Add(id); + string lang = Json.ReadTag(json, "lang"); + _audioTrackIds.Add(new KeyValuePair(id, lang)); } } _mpvFree(lpBuffer); } - return _audioTrackIds.Count; + return _audioTrackIds; } } @@ -358,21 +359,18 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers { var lpBuffer = IntPtr.Zero; _mpvGetPropertyString(_mpvHandle, GetUtf8Bytes("aid"), MpvFormatString, ref lpBuffer); - string str = Marshal.PtrToStringAnsi(lpBuffer); - int number = 0; - if (AudioTrackCount > 1 && _audioTrackIds.Contains(str)) - { - number = _audioTrackIds.IndexOf(str); - } + int id = int.Parse(Marshal.PtrToStringAnsi(lpBuffer)); + int idx = _audioTrackIds.FindIndex(x => x.Key == id); + int number = AudioTracks.Count > 1 && idx != -1 ? idx : 0; _mpvFree(lpBuffer); return number; } set { string id = "1"; - if (AudioTrackCount > 1 && value >= 0 && value < _audioTrackIds.Count) + if (AudioTracks.Count > 1 && value >= 0 && value < _audioTrackIds.Count) { - id = _audioTrackIds[value]; + id = _audioTrackIds[value].Key.ToString(); } DoMpvCommand("set", "aid", id); } diff --git a/src/Logic/VideoPlayers/LibMpvMono.cs b/src/Logic/VideoPlayers/LibMpvMono.cs index 6b507c0ec..90869214e 100644 --- a/src/Logic/VideoPlayers/LibMpvMono.cs +++ b/src/Logic/VideoPlayers/LibMpvMono.cs @@ -194,28 +194,29 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers public override bool IsPlaying => !IsPaused; - private List _audioTrackIds; - public int AudioTrackCount + private List> _audioTrackIds; + public List> AudioTracks { get { if (_audioTrackIds == null) { - _audioTrackIds = new List(); + _audioTrackIds = new List>(); var lpBuffer = NativeMethods.mpv_get_property_string(_mpvHandle, GetUtf8Bytes("track-list")); string trackListJson = Marshal.PtrToStringAnsi(lpBuffer); foreach (var json in Json.ReadObjectArray(trackListJson)) { string trackType = Json.ReadTag(json, "type"); - string id = Json.ReadTag(json, "id"); + int id = int.Parse(Json.ReadTag(json, "id")); if (trackType == "audio") { - _audioTrackIds.Add(id); + string lang = Json.ReadTag(json, "lang"); + _audioTrackIds.Add(new KeyValuePair(id, lang)); } } NativeMethods.mpv_free(lpBuffer); } - return _audioTrackIds.Count; + return _audioTrackIds; } } @@ -224,21 +225,18 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers get { var lpBuffer = NativeMethods.mpv_get_property_string(_mpvHandle, GetUtf8Bytes("aid")); - string str = Marshal.PtrToStringAnsi(lpBuffer); - int number = 0; - if (AudioTrackCount > 1 && _audioTrackIds.Contains(str)) - { - number = _audioTrackIds.IndexOf(str); - } + int id = int.Parse(Marshal.PtrToStringAnsi(lpBuffer)); + int idx = _audioTrackIds.FindIndex(x => x.Key == id); + int number = AudioTracks.Count > 1 && idx != -1 ? idx : 0; NativeMethods.mpv_free(lpBuffer); return number; } set { string id = "1"; - if (AudioTrackCount > 1 && value >= 0 && value < _audioTrackIds.Count) + if (AudioTracks.Count > 1 && value >= 0 && value < _audioTrackIds.Count) { - id = _audioTrackIds[value]; + id = _audioTrackIds[value].Key.ToString(); } DoMpvCommand("set", "aid", id); }