Skip to content

Commit

Permalink
Merge pull request #249 from darkpaul91/IFME_fix_trimming_VideoAudio
Browse files Browse the repository at this point in the history
fix Video/Audio Trimming issues:
  • Loading branch information
Anime4000 authored Oct 8, 2024
2 parents 77c2d01 + ab4fcbe commit bb1d7d7
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 76 deletions.
93 changes: 93 additions & 0 deletions IFME/CustomForms/TimespanTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace IFME
{
internal class TimespanTextBox : TextBox
{
private string lastValidText = "";
public TimeSpan? MaxValue { get; set; } = null;

public TimeSpan TimeSpan
{
get
{
TimeSpan.TryParse(this.Text, out var timeStart);
return timeStart;
}
set
{
setValueTimespan(value);
}
}

public TimespanTextBox()
{
this.KeyPress += new KeyPressEventHandler(this.textBox_KeyPress);
this.TextChanged += new EventHandler(this.textBox_Validating);
}

public TimespanTextBox(TimeSpan maxValue) : base()
{
this.MaxValue = maxValue;
}

public void setValueSeconds(float seconds)
{
setValueTimespan(TimeSpan.FromSeconds((double)(new decimal(seconds))));
}

private void setValueTimespan(TimeSpan timespan)
{
string sign = (timespan.TotalMilliseconds < 0) ? "-" : "";
this.Text = $"{sign}{timespan:hh\\:mm\\:ss\\.fff}";
}

private void textBox_Validating(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(this.Text) || this.Text.Equals("0"))
this.Text = "00:00:00.000";

var rTime = new Regex(@"-?[0-9][0-9]\:[0-6][0-9]\:[0-6][0-9]\.[0-9]{1,3}");

if (!rTime.IsMatch(this.Text))
{
MessageBox.Show("Please provide the time in hh:mm:ss.xxx format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Text = lastValidText;
}
else if (this.MaxValue != null && this.TimeSpan > this.MaxValue)
{
MessageBox.Show("Time set exceeds file duration", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Text = lastValidText;
}
else
{
lastValidText = this.Text;
}

if (this.Text.Contains("-"))
{
this.BackColor = Color.PaleVioletRed;
this.ForeColor = Color.DarkRed;
} else
{
this.BackColor = TextBox.DefaultBackColor;
this.ForeColor = TextBox.DefaultForeColor;
}
}

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar.ToString() == ":") || (e.KeyChar.ToString() == "."))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}
}
3 changes: 3 additions & 0 deletions IFME/IFME.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<Compile Include="BackgroundWorker2.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CustomForms\TimespanTextBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Extensions.cs" />
<Compile Include="Fonts\FontAwesome.cs" />
<Compile Include="Fonts\Fonts.cs" />
Expand Down
36 changes: 13 additions & 23 deletions IFME/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 46 additions & 53 deletions IFME/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ private void lstFile_SelectedIndexChanged(object sender, EventArgs e)
if (data.Attachment.Count > 0)
lstAttach.Items[0].Selected = true;

chkAdvTrim.Checked = data.Trim.Enable;
refreshTrimFieldsEnabled();
chkAdvTrim.Checked = chkAdvTrim.Enabled && data.Trim.Enable;
grpAdvTrim.Enabled = chkAdvTrim.Checked;
txtAdvTrimStart.Text = data.Trim.Start;
txtAdvTrimEnd.Text = data.Trim.End;
txtAdvTrimDuration.Text = data.Trim.Duration;
Expand All @@ -390,6 +392,17 @@ private void lstFile_SelectedIndexChanged(object sender, EventArgs e)
txtAdvCropStart.Text = data.Crop.Start;
txtAdvCropDuration.Text = data.Crop.Duration;

// Advanced Trim and Cut
if (txtAdvTrimEnd.TimeSpan.TotalMilliseconds == 0 && txtAdvTrimDuration.TimeSpan.TotalMilliseconds == 0)
{
TimeSpan auxValue = TimeSpan.FromSeconds((double)(new decimal(data.Duration)));
txtAdvTrimEnd.TimeSpan = auxValue;
txtAdvTrimDuration.TimeSpan = auxValue;
txtAdvTrimEnd.MaxValue = auxValue;
txtAdvTrimDuration.MaxValue = auxValue;
txtAdvTrimStart.MaxValue = auxValue;
}

// Media Info
txtMediaInfo.Text = FFmpeg.MediaInfo.Print(data.Info);
}
Expand All @@ -405,6 +418,7 @@ private void lstFile_SelectedIndexChanged(object sender, EventArgs e)
chkAudioMP4Compt.Checked = false;
chkSubHard.Checked = false;
chkAdvTrim.Checked = false;
refreshTrimFieldsEnabled();
}
}

Expand Down Expand Up @@ -1979,7 +1993,12 @@ private void tsmiFileAddAttachEmbed_Click(object sender, EventArgs e)

private void tabConfigAdvance_Enter(object sender, EventArgs e)
{
chkAdvTrim.Enabled = !(chkAudioMP4Compt.Checked || chkVideoMP4Compt.Checked); // prevent user trim when do Fast Remux
refreshTrimFieldsEnabled();
}

private void refreshTrimFieldsEnabled()
{
chkAdvTrim.Enabled = lstFile.SelectedItems.Count > 0 && !(chkAudioMP4Compt.Checked || chkVideoMP4Compt.Checked); // prevent user trim when do Fast Remux
chkAdvCropAuto.Enabled = !chkVideoMP4Compt.Checked; // prevent user crop when do Fast Remux

grpAdvTrim.Enabled = chkAdvTrim.Checked;
Expand All @@ -2005,69 +2024,43 @@ private void chkAdvTrim_CheckedChanged(object sender, EventArgs e)
}
}

private void txtTrim_Validating(object sender, CancelEventArgs e)
private void txtTrim_Event(object sender, EventArgs e)
{
var rTime = new Regex(@"[0-9][0-9]\:[0-6][0-9]\:[0-6][0-9]\.[0-9]{1,}");
var ctrl = sender as TextBox;

if ((sender as TextBox).Text.Length > 0)
var timeStart = txtAdvTrimStart.TimeSpan;
var timeEnd = txtAdvTrimEnd.TimeSpan;
var timeSpan = txtAdvTrimDuration.TimeSpan;


if (ctrl == txtAdvTrimStart || ctrl == txtAdvTrimEnd)
{
if (!rTime.IsMatch((sender as TextBox).Text))
{
MessageBox.Show("Please provide the time in hh:mm:ss.xxx format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
timeSpan = timeEnd - timeStart;
}
}

private void txtTrim_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar.ToString() == ":") || (e.KeyChar.ToString() == "."))
if (ctrl == txtAdvTrimDuration)
{
e.Handled = false;
timeEnd = timeStart + timeSpan;
}
else

if (timeSpan.TotalMilliseconds == 0 && timeEnd.TotalMilliseconds == 0 && (lstFile.SelectedItems.Count > 0))
{
e.Handled = true;
var data = lstFile.SelectedItems[0].Tag as MediaQueue;
txtAdvTrimEnd.setValueSeconds(data.Duration);
txtAdvTrimDuration.setValueSeconds(data.Duration);
timeEnd = txtAdvTrimEnd.TimeSpan;
timeSpan = txtAdvTrimDuration.TimeSpan;
}
}

private void txtTrim_Event(object sender, EventArgs e)
{
var ctrl = sender as TextBox;

var bTimeStart = TimeSpan.TryParse(txtAdvTrimStart.Text, out var timeStart);
var bTimeEnd = TimeSpan.TryParse(txtAdvTrimEnd.Text, out var timeEnd);
var bTimeSpan = TimeSpan.TryParse(txtAdvTrimDuration.Text, out var timeSpan);
txtAdvTrimStart.TimeSpan = timeStart;
txtAdvTrimEnd.TimeSpan = timeEnd;
txtAdvTrimDuration.TimeSpan = timeSpan;

if (bTimeStart && bTimeEnd && bTimeSpan)
foreach (ListViewItem item in lstFile.SelectedItems)
{
if (ctrl.Focused)
{
if (ctrl == txtAdvTrimStart)
{
timeSpan = timeEnd - timeStart;
}

if (ctrl == txtAdvTrimEnd)
{
timeSpan = timeEnd - timeStart;
}

if (ctrl == txtAdvTrimDuration)
{
timeEnd = timeStart + timeSpan;
}

txtAdvTrimStart.Text = $"{timeStart:hh\\:mm\\:ss\\.fff}";
txtAdvTrimEnd.Text = $"{timeEnd:hh\\:mm\\:ss\\.fff}";
txtAdvTrimDuration.Text = $"{timeSpan:hh\\:mm\\:ss\\.fff}";
}

foreach (ListViewItem item in lstFile.SelectedItems)
{
(item.Tag as MediaQueue).Trim.Start = $"{timeStart:hh\\:mm\\:ss\\.fff}";
(item.Tag as MediaQueue).Trim.End = $"{timeEnd:hh\\:mm\\:ss\\.fff}";
(item.Tag as MediaQueue).Trim.Duration = $"{timeSpan:hh\\:mm\\:ss\\.fff}";
}
(item.Tag as MediaQueue).Trim.Start = txtAdvTrimStart.Text;
(item.Tag as MediaQueue).Trim.End = txtAdvTrimEnd.Text;
(item.Tag as MediaQueue).Trim.Duration = txtAdvTrimDuration.Text;
}
}

Expand Down

0 comments on commit bb1d7d7

Please sign in to comment.