Skip to content

Commit

Permalink
GUI bug fix 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime4000 committed Jul 13, 2024
1 parent 9820c28 commit bb964bc
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 136 deletions.
11 changes: 10 additions & 1 deletion IFME.OSManager/OS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -198,5 +199,13 @@ public static bool IsExecutable(string filePath)
return false;
}
}
}

public static byte[] ComputeSHA256(string input)
{
using (var sha256 = SHA256.Create())
{
return sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
}
}
}
}
28 changes: 15 additions & 13 deletions IFME/MediaQueue/MediaQueueParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public static class Audio
public static string CmdEncoder { get; set; } = string.Empty;
}

public static void SetDefault(Guid videoId, Guid audioId)
public static void UseDefaultVideo(Guid videoId)
{
if (!videoId.Equals(new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff")))
if (Plugins.Items.Video.ContainsKey(videoId))
{
Video.Id = videoId;
Video.DeInterlace = false;
Expand All @@ -51,19 +51,21 @@ public static void SetDefault(Guid videoId, Guid audioId)
Video.MultiPass = 2;
Video.DeInterlaceMode = 1;
Video.DeInterlaceField = 0;
Video.CmdEncoder = Plugins.Items.Video[videoId].Video.Args.Command;
Video.CmdEncoder = Plugins.Items.Video[videoId].Video.Args.Command;
}

if (!audioId.Equals(new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff")))
}

public static void UseDefaultAudio(Guid audioId)
{
if (Plugins.Items.Audio.ContainsKey(audioId))
{
Audio.Id = audioId;
Audio.Mode = 0;
Audio.Quality = Plugins.Items.Audio[audioId].Audio.Mode[0].Default;
Audio.Channel = Plugins.Items.Audio[audioId].Audio.ChannelDefault;
Audio.SampleRate = Plugins.Items.Audio[audioId].Audio.SampleRateDefault;
Audio.CmdEncoder = Plugins.Items.Audio[audioId].Audio.Args.Command;
}
}
Audio.Mode = 0;
Audio.Quality = Plugins.Items.Audio[audioId].Audio.Mode[0].Default;
Audio.Channel = Plugins.Items.Audio[audioId].Audio.ChannelDefault;
Audio.SampleRate = Plugins.Items.Audio[audioId].Audio.SampleRateDefault;
Audio.CmdEncoder = Plugins.Items.Audio[audioId].Audio.Args.Command;
}
}
}

public static MediaQueueVideo Video(string path, FFmpeg.StreamVideo data, bool isImageSeq = false)
Expand Down
194 changes: 92 additions & 102 deletions IFME/frmMain.Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using IFME.OSManager;
using System.Net.Http.Headers;
using System.Collections;
using Newtonsoft.Json;

namespace IFME
{
Expand Down Expand Up @@ -684,8 +685,8 @@ private void ShowSupportedCodec(string value, bool imgSeq = false)
var videoCodec = new Dictionary<Guid, PluginsVideo>();
var audioCodec = new Dictionary<Guid, PluginsAudio>();

var videoId = new Guid();
var audioId = new Guid();
SaveControlVideo();
SaveControlAudio();

foreach (var item in Plugins.Items.Video)
{
Expand All @@ -698,7 +699,6 @@ private void ShowSupportedCodec(string value, bool imgSeq = false)
if (item.Value.Format.Contains(value))
{
videoCodec.Add(item.Key, item.Value);
videoId = item.Key;
}
}

Expand All @@ -712,15 +712,41 @@ private void ShowSupportedCodec(string value, bool imgSeq = false)
if (item.Value.Format.Contains(exts))
{
audioCodec.Add(item.Key, item.Value);
audioId = item.Key;
}
}

// Compare current and new dict array, if hash not same then load new dict and use default
if (videoCodec.Count > 0)
{
cboVideoEncoder.DataSource = new BindingSource(videoCodec.ToDictionary(p => p.Key, p => p.Value.Name), null);
cboVideoEncoder.DisplayMember = "Value";
cboVideoEncoder.ValueMember = "Key";
var newDataSource = videoCodec.ToDictionary(p => p.Key, p => p.Value.Name);

var currentDataSource = cboVideoEncoder.DataSource as BindingSource;
var currentDataSourceDict = currentDataSource?.DataSource as Dictionary<Guid, string>;

var newDataSourceJson = JsonConvert.SerializeObject(newDataSource);
var currentDataSourceJson = currentDataSourceDict != null ? JsonConvert.SerializeObject(currentDataSourceDict) : string.Empty;

var newDataSourceHash = OS.ComputeSHA256(newDataSourceJson);
var currentDataSourceHash = OS.ComputeSHA256(currentDataSourceJson);

if (newDataSourceHash != currentDataSourceHash)
{
cboVideoEncoder.DataSource = new BindingSource(newDataSource, null);
cboVideoEncoder.DisplayMember = "Value";
cboVideoEncoder.ValueMember = "Key";
}

if (newDataSource.ContainsKey(MediaQueueParse.Gui.Video.Id))
{
LoadControlVideo();
}
else
{
cboVideoEncoder.SelectedIndex = cboVideoEncoder.Items.Count - 1;
MediaQueueParse.Gui.UseDefaultVideo(((KeyValuePair<Guid, string>)cboVideoEncoder.SelectedItem).Key);
LoadControlVideo();

}
}
else
{
Expand All @@ -729,20 +755,68 @@ private void ShowSupportedCodec(string value, bool imgSeq = false)

if (audioCodec.Count > 0)
{
cboAudioEncoder.DataSource = new BindingSource(audioCodec.ToDictionary(p => p.Key, p => p.Value.Name), null);
cboAudioEncoder.DisplayMember = "Value";
cboAudioEncoder.ValueMember = "Key";
var newDataSource = audioCodec.ToDictionary(p => p.Key, p => p.Value.Name);

var currentDataSource = cboAudioEncoder.DataSource as BindingSource;
var currentDataSourceDict = currentDataSource?.DataSource as Dictionary<Guid, string>;

var newDataSourceJson = JsonConvert.SerializeObject(newDataSource);
var currentDataSourceJson = currentDataSourceDict != null ? JsonConvert.SerializeObject(currentDataSourceDict) : string.Empty;

var newDataSourceHash = OS.ComputeSHA256(newDataSourceJson);
var currentDataSourceHash = OS.ComputeSHA256(currentDataSourceJson);

if (newDataSourceHash != currentDataSourceHash)
{
cboAudioEncoder.DataSource = new BindingSource(newDataSource, null);
cboAudioEncoder.DisplayMember = "Value";
cboAudioEncoder.ValueMember = "Key";
}

if (newDataSource.ContainsKey(MediaQueueParse.Gui.Audio.Id))
{
LoadControlAudio();
}
else
{
cboAudioEncoder.SelectedIndex = cboAudioEncoder.Items.Count - 1;
MediaQueueParse.Gui.UseDefaultAudio(((KeyValuePair<Guid, string>)cboAudioEncoder.SelectedItem).Key);
LoadControlAudio();
}
}
else
{
cboAudioEncoder.DataSource = null;
}
}

private void LoadControlVideo()
{
cboVideoEncoder.SelectedValue = MediaQueueParse.Gui.Video.Id;
cboVideoPreset.Text = MediaQueueParse.Gui.Video.Preset;
cboVideoTune.Text = MediaQueueParse.Gui.Video.Tune;
cboVideoRateControl.SelectedIndex = MediaQueueParse.Gui.Video.Mode;
nudVideoRateFactor.Value = MediaQueueParse.Gui.Video.Value;
nudVideoMultiPass.Value = MediaQueueParse.Gui.Video.MultiPass;
chkVideoDeInterlace.Checked = MediaQueueParse.Gui.Video.DeInterlace;
cboVideoDeInterMode.SelectedIndex = MediaQueueParse.Gui.Video.DeInterlaceMode;
cboVideoDeInterField.SelectedIndex = MediaQueueParse.Gui.Video.DeInterlaceField;
}

SetDefinedData(videoId, audioId);
private void LoadControlAudio()
{
cboAudioEncoder.SelectedValue = MediaQueueParse.Gui.Audio.Id;
cboAudioMode.SelectedIndex = MediaQueueParse.Gui.Audio.Mode;
cboAudioQuality.Text = MediaQueueParse.Gui.Audio.Quality;
cboAudioSampleRate.SelectedValue = MediaQueueParse.Gui.Audio.SampleRate;
cboAudioChannel.SelectedValue = MediaQueueParse.Gui.Audio.Channel;
}

private void SetGuiDataVideo()
private void SaveControlVideo()
{
if (cboVideoEncoder.SelectedItem == null)
return;

MediaQueueParse.Gui.Video.Id = ((KeyValuePair<Guid, string>)cboVideoEncoder.SelectedItem).Key;
MediaQueueParse.Gui.Video.Preset = cboVideoPreset.Text;
MediaQueueParse.Gui.Video.Tune = cboVideoTune.Text;
Expand All @@ -754,102 +828,18 @@ private void SetGuiDataVideo()
MediaQueueParse.Gui.Video.DeInterlaceField = cboVideoDeInterField.SelectedIndex;
}

private void SetGuiDataAudio()
private void SaveControlAudio()
{
if (cboAudioEncoder.SelectedItem == null)
return;

MediaQueueParse.Gui.Audio.Id = ((KeyValuePair<Guid, string>)cboAudioEncoder.SelectedItem).Key;
MediaQueueParse.Gui.Audio.Mode = cboAudioMode.SelectedIndex;
MediaQueueParse.Gui.Audio.Quality = cboAudioQuality.Text;
MediaQueueParse.Gui.Audio.SampleRate = ((KeyValuePair<int, string>)cboAudioSampleRate.SelectedItem).Key;
MediaQueueParse.Gui.Audio.Channel = ((KeyValuePair<int, string>)cboAudioChannel.SelectedItem).Key;
}

private void SetDefinedData(Guid videoId, Guid audioId)
{
if (lstFile.Focused)
return;

if (cboProfile.Focused)
return;

if (Plugins.Items.Video.TryGetValue(videoId, out PluginsVideo vData))
{

}

if (Plugins.Items.Audio.TryGetValue(audioId, out PluginsAudio aData))
{

}

if (lstFile.SelectedItems.Count > 0)
{
foreach (ListViewItem queue in lstFile.SelectedItems)
{
foreach (var item in (queue.Tag as MediaQueue).Video)
{
var matchVideo = cboVideoEncoder.Items.Cast<KeyValuePair<Guid, string>>().Any(index => Equals(item.Encoder.Id, index.Key));
if (matchVideo) break;

if (vData != null)
{
if (Guid.TryParse($"{vData.GUID}", out Guid guid))
{
if (Equals(guid, item.Encoder.Id))
continue;

item.Encoder = new MediaQueueVideoEncoder
{
Id = guid,
Preset = vData.Video.PresetDefault,
Tune = vData.Video.TuneDefault,
Mode = 0,
Value = vData.Video.Mode[0].Value.Default,
MultiPass = 2,
Command = string.Empty
};
item.Quality.Command = string.Empty;
}
}
else
{
item.Encoder = new MediaQueueVideoEncoder
{
Id = new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff") // GUID do not encode video
};
}
}

foreach (var item in (queue.Tag as MediaQueue).Audio)
{
var matchAudio = cboAudioEncoder.Items.Cast<KeyValuePair<Guid, string>>().Any(index => Equals(item.Encoder.Id, index.Key));
if (matchAudio) break;

if (aData != null)
{
if (Guid.TryParse($"{aData.GUID}", out Guid guid))
{
item.Encoder = new MediaQueueAudioEncoder
{
Id = guid,
Mode = 0,
Quality = aData.Audio.Mode[0].Default,
SampleRate = aData.Audio.SampleRateDefault,
Channel = aData.Audio.ChannelDefault,
Command = string.Empty,
};
item.Command = string.Empty;
}
}
}
}
}

DisplayProperties_Video();
DisplayProperties_Audio();
DisplayProperties_Subtitle();
DisplayProperties_Attachment();
}

private void SetProfileData(Profiles value)
{
cboVideoEncoder.SelectedIndex = -1;
Expand Down Expand Up @@ -979,8 +969,8 @@ private void SetProfileData(Profiles value)
DisplayProperties_Attachment();
}

SetGuiDataVideo();
SetGuiDataAudio();
SaveControlVideo();
SaveControlAudio();
}
}
}
Loading

0 comments on commit bb964bc

Please sign in to comment.