Get audio track language from MPV

This commit is contained in:
OmrSi 2020-11-04 19:24:57 +02:00
parent a43841ddb0
commit b5629276ce
3 changed files with 32 additions and 33 deletions

View File

@ -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;

View File

@ -326,29 +326,30 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers
public override bool IsPlaying => !IsPaused;
private List<string> _audioTrackIds;
public int AudioTrackCount
private List<KeyValuePair<int, string>> _audioTrackIds;
public List<KeyValuePair<int, string>> AudioTracks
{
get
{
if (_audioTrackIds == null)
{
_audioTrackIds = new List<string>();
_audioTrackIds = new List<KeyValuePair<int, string>>();
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<int, string>(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);
}

View File

@ -194,28 +194,29 @@ namespace Nikse.SubtitleEdit.Logic.VideoPlayers
public override bool IsPlaying => !IsPaused;
private List<string> _audioTrackIds;
public int AudioTrackCount
private List<KeyValuePair<int, string>> _audioTrackIds;
public List<KeyValuePair<int, string>> AudioTracks
{
get
{
if (_audioTrackIds == null)
{
_audioTrackIds = new List<string>();
_audioTrackIds = new List<KeyValuePair<int, string>>();
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<int, string>(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);
}