Skip to content

Commit

Permalink
Add Auto Crop Detection #102
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkl58 committed Feb 25, 2023
1 parent 6cc8194 commit c0eeca5
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 9 deletions.
19 changes: 10 additions & 9 deletions NotEnoughAV1Encodes/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,21 @@
</TabItem>
<TabItem x:Name="TabItemFilters" Header="{lex:Loc}">
<Grid Background="{Binding ElementName=ListBoxAudioTracks, Path=Background, UpdateSourceTrigger=PropertyChanged}">
<GroupBox x:Name="GroupBoxCrop" Header="{lex:Loc}" HorizontalAlignment="Left" Height="397" Margin="293,10,0,0" VerticalAlignment="Top" Width="640" mah:ControlsHelper.ContentCharacterCasing="Normal">
<GroupBox x:Name="GroupBoxCrop" Header="{lex:Loc}" HorizontalAlignment="Left" Height="397" Margin="293,10,0,0" VerticalAlignment="Top" Width="625" mah:ControlsHelper.ContentCharacterCasing="Normal">
<Grid IsEnabled="{Binding IsOn, ElementName=ToggleSwitchFilterCrop}">
<TextBox x:Name="TextBoxFiltersCropLeft" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Height="26" Margin="6,0,0,0" TextWrapping="Wrap" Text="{Binding FilterCropLeft}" VerticalAlignment="Center" Width="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="ToggleSwitchFilterCrop_Toggled"/>
<TextBox x:Name="TextBoxFiltersCropRight" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Height="26" Margin="564,0,0,0" TextWrapping="Wrap" Text="{Binding FilterCropRight}" VerticalAlignment="Center" Width="56" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="ToggleSwitchFilterCrop_Toggled"/>
<TextBox x:Name="TextBoxFiltersCropRight" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Height="26" Margin="552,0,0,0" TextWrapping="Wrap" Text="{Binding FilterCropRight}" VerticalAlignment="Center" Width="56" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="ToggleSwitchFilterCrop_Toggled"/>
<TextBox x:Name="TextBoxFiltersCropTop" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Height="26" Margin="260,11,0,0" TextWrapping="Wrap" Text="{Binding FilterCropTop}" VerticalAlignment="Top" Width="55" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="ToggleSwitchFilterCrop_Toggled" />
<TextBox x:Name="TextBoxFiltersCropBottom" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Height="26" Margin="260,326,0,0" TextWrapping="Wrap" Text="{Binding FilterCropBottom}" VerticalAlignment="Top" Width="56" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="ToggleSwitchFilterCrop_Toggled"/>
<Label x:Name="LabelBottom" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="320,326,0,0" VerticalAlignment="Top" Width="56" VerticalContentAlignment="Center" />
<Label x:Name="LabelTop" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="320,11,0,0" VerticalAlignment="Top" Width="55" VerticalContentAlignment="Center" />
<Label x:Name="LabelLeft" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="8,149,0,0" VerticalAlignment="Top" Height="26" Width="52" HorizontalContentAlignment="Center" />
<Label x:Name="LabelRight" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="566,149,0,0" VerticalAlignment="Top" Height="28" Width="54" HorizontalContentAlignment="Center" />
<Image x:Name="ImageCropPreview" HorizontalAlignment="Left" Height="270" Margin="67,44,0,0" VerticalAlignment="Top" Width="490" Source="/resources/img/videoplaceholder.jpg"/>
<Label x:Name="LabelCropPreview" Content="1/4" HorizontalAlignment="Left" Margin="501,11,0,0" VerticalAlignment="Top" Width="40" HorizontalContentAlignment="Center" RenderTransformOrigin="0.775,0.579"/>
<Button x:Name="ButtonCropPreviewForward" Content="&gt;" HorizontalAlignment="Left" Margin="535,10,0,0" VerticalAlignment="Top" Width="22" Click="ButtonCropPreviewForward_Click"/>
<Button x:Name="ButtonCropPreviewBackward" Content="&lt;" HorizontalAlignment="Left" Margin="485,10,0,0" VerticalAlignment="Top" Width="22" Click="ButtonCropPreviewBackward_Click"/>
<Label x:Name="LabelLeft" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="6,142,0,0" VerticalAlignment="Top" Height="26" Width="54" HorizontalContentAlignment="Center" />
<Label x:Name="LabelRight" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="552,140,0,0" VerticalAlignment="Top" Height="28" Width="56" HorizontalContentAlignment="Center" />
<Image x:Name="ImageCropPreview" HorizontalAlignment="Left" Height="270" Margin="67,44,0,0" VerticalAlignment="Top" Width="480" Source="/resources/img/videoplaceholder.jpg"/>
<Label x:Name="LabelCropPreview" Content="1/4" HorizontalAlignment="Left" Margin="491,11,0,0" VerticalAlignment="Top" Width="40" HorizontalContentAlignment="Center" RenderTransformOrigin="0.775,0.579"/>
<Button x:Name="ButtonCropPreviewForward" Content="&gt;" HorizontalAlignment="Left" Margin="525,10,0,0" VerticalAlignment="Top" Width="22" Click="ButtonCropPreviewForward_Click"/>
<Button x:Name="ButtonCropPreviewBackward" Content="&lt;" HorizontalAlignment="Left" Margin="475,10,0,0" VerticalAlignment="Top" Width="22" Click="ButtonCropPreviewBackward_Click"/>
<Button x:Name="ButtonCropAutoDetect" Content="{lex:Loc}" HorizontalAlignment="Left" Margin="67,10,0,0" VerticalAlignment="Top" Width="120" mah:ControlsHelper.ContentCharacterCasing="Normal" Click="ButtonCropAutoDetect_Click"/>
</Grid>
</GroupBox>
<GroupBox x:Name="GroupBoxResize" Header="{lex:Loc}" HorizontalAlignment="Left" Height="203" Margin="14,204,0,0" VerticalAlignment="Top" Width="274" mah:ControlsHelper.ContentCharacterCasing="Normal">
Expand Down Expand Up @@ -271,7 +272,7 @@
</ComboBox>
</Grid>
</GroupBox>
<mah:ToggleSwitch x:Name="ToggleSwitchFilterCrop" IsOn="{Binding FilterCrop}" Header="" Height="61" Margin="850,-15,0,0" VerticalAlignment="Top" Width="100" Toggled="ToggleSwitchFilterCrop_Toggled" />
<mah:ToggleSwitch x:Name="ToggleSwitchFilterCrop" IsOn="{Binding FilterCrop}" Header="" Height="61" Margin="830,-16,0,0" VerticalAlignment="Top" Width="100" Toggled="ToggleSwitchFilterCrop_Toggled" />
<mah:ToggleSwitch x:Name="ToggleSwitchFilterResize" IsOn="{Binding FilterResize}" Header="" Height="62" Margin="202,179,0,0" VerticalAlignment="Top" Width="100" />
<mah:ToggleSwitch x:Name="ToggleSwitchFilterRotate" IsOn="{Binding FilterRotate}" Header="" Height="100" Margin="202,-15,0,0" VerticalAlignment="Top" Width="159" />
<mah:ToggleSwitch x:Name="ToggleSwitchFilterDeinterlace" IsOn="{Binding FilterDeinterlace}" Header="" Height="100" Margin="202,81,0,0" VerticalAlignment="Top" Width="132" />
Expand Down
74 changes: 74 additions & 0 deletions NotEnoughAV1Encodes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ private void ToggleSwitchFilterCrop_Toggled(object sender, RoutedEventArgs e)
CreateCropPreviewsOnLoad();
}

private void ButtonCropAutoDetect_Click(object sender, RoutedEventArgs e)
{
AutoCropDetect();
}

private void ButtonCropPreviewForward_Click(object sender, RoutedEventArgs e)
{
if (videoDB.InputPath == null) return;
Expand Down Expand Up @@ -1377,6 +1382,75 @@ private async void CreateCropPreviewsOnLoad()
catch { }
}

private async void AutoCropDetect()
{
if (videoDB.InputPath == null) return;

List<string> cropList = new();

string time = videoDB.MIDuration;

int seconds = Convert.ToInt32(Math.Floor(TimeSpan.Parse(time).TotalSeconds / 4));

// Use the current frame as start point of detection
int index = int.Parse(LabelCropPreview.Content.ToString().Split("/")[0]);

string command = "/C ffmpeg.exe -ss " + (index * seconds).ToString() + " -i \"" + videoDB.InputPath + "\" -vf cropdetect=24:16:0 -t 5 -f null -";

Process ffmpegProcess = new();
ProcessStartInfo startInfo = new()
{
FileName = "cmd.exe",
WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Apps", "FFmpeg"),
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = command,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true
};

ffmpegProcess.StartInfo = startInfo;
ffmpegProcess.Start();

string lastLine;
while (! ffmpegProcess.StandardError.EndOfStream)
{
lastLine = ffmpegProcess.StandardError.ReadLine();
if (lastLine.Contains("crop="))
{
cropList.Add(lastLine.Split("crop=")[1]);
}
}

ffmpegProcess.WaitForExit();

// Get most occuring value
string crop = cropList.Where(c => !string.IsNullOrEmpty(c)).GroupBy(a => a).OrderByDescending(b => b.Key[1].ToString()).First().Key;

try
{
// Translate Output to crop values
int cropTop = int.Parse(crop.Split(":")[3]);
TextBoxFiltersCropTop.Text = cropTop.ToString();

int cropLeft = int.Parse(crop.Split(":")[2]);
TextBoxFiltersCropLeft.Text = cropLeft.ToString();

int cropBottom = videoDB.MIHeight - cropTop - int.Parse(crop.Split(":")[1]);
TextBoxFiltersCropBottom.Text = cropBottom.ToString();

int cropRight = videoDB.MIWidth - cropLeft - int.Parse(crop.Split(":")[0]);
TextBoxFiltersCropRight.Text = cropRight.ToString();

string cropNew = "-vf " + VideoFiltersCrop();
await Task.Run(() => CreateCropPreviews(cropNew));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void CreateCropPreviews(string crop)
{
Directory.CreateDirectory(Path.Combine(Global.Temp, "NEAV1E"));
Expand Down
9 changes: 9 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.Designer.cs

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

3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>Pfade</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>Autom. Erkennung</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>Dossier</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>Détection automatique</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.it-IT.resx
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>Percorsi</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>Rilevamento automatico</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>パス</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>オートディテクト</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>Paths</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>Auto Detect</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,7 @@ Please download and extract 7-zip to the following path:
<data name="Paths" xml:space="preserve">
<value>Пути</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>автоматическое обнаружение</value>
</data>
</root>
3 changes: 3 additions & 0 deletions NotEnoughAV1Encodes/resources/lang/Strings.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,7 @@
<data name="Paths" xml:space="preserve">
<value>路径</value>
</data>
<data name="ButtonCropAutoDetect" xml:space="preserve">
<value>自动检测</value>
</data>
</root>

0 comments on commit c0eeca5

Please sign in to comment.