Skip to content

Commit

Permalink
Work Better Slightly
Browse files Browse the repository at this point in the history
More Better Good Stuff
  • Loading branch information
FoxCouncil committed Jul 9, 2023
1 parent f447123 commit 5a2a2d9
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 19 deletions.
26 changes: 16 additions & 10 deletions FFmpegTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -8,21 +8,22 @@ namespace FoxingVideo;

public class FFmpegTask
{
public static readonly List<FFmpegTask> RunningTasks = new();
public static readonly ObservableCollection<FFmpegTask> RunningTasks = new();

public string InputFile { get; set; }

public string OutputFile { get; set; }

public string Arguments { get; set; }
public FFmpegProfile Profile { get; set; }

public string Status { get; set; } = "";

public FFmpegTask(string inputFile, string outputFile, string arguments)
public FFmpegTask(FFmpegProfile profile, string inputFile, string outputDir)
{
Profile = profile;

InputFile = inputFile;
OutputFile = outputFile;
Arguments = arguments;
OutputFile = Path.Combine(outputDir, $"{Path.GetFileNameWithoutExtension(inputFile)}_{Profile.Key}");
}

public async Task Run()
Expand All @@ -33,8 +34,8 @@ public async Task Run()
{
StartInfo = new ProcessStartInfo
{
FileName = "ffmpeg.exe",
Arguments = $"-i '{InputFile}' {Arguments} '{OutputFile}'",
FileName = Path.Combine(AppContext.BaseDirectory, "ffmpeg.exe"),
Arguments = $"-i \"{InputFile}\" {Profile.Arguments} \"{OutputFile}.avi\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Expand All @@ -58,14 +59,19 @@ private async Task RunProcessAsync(Process process)
{
process.ErrorDataReceived += (s, e) =>
{
Status = e.Data ?? "";
Status += e.Data ?? "";
};

process.OutputDataReceived += (s, e) =>
{
Status += e.Data ?? "";
};

process.Start();

while (!process.HasExited)
{
await Task.Delay(500); // Update every 500 ms. Adjust this value to your needs.
await Task.Delay(1); // Update every 500 ms. Adjust this value to your needs.
}

if (process.ExitCode != 0)
Expand Down
7 changes: 7 additions & 0 deletions FoxingVideo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageReference Include="System.Data.SQLite" Version="1.0.118" />
</ItemGroup>

<ItemGroup>
<None Update="ffmpeg.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
14 changes: 12 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="70" />
<ColumnDefinition MaxWidth="100" />
<ColumnDefinition MaxWidth="200" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="42" />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="Profiles" Click="ProfileButton_Click"/>
<Label Grid.Row="0" Grid.Column="1" VerticalContentAlignment="Center" Content="{Binding CurrentProfile, ElementName=_this}"/>
<ListBox Grid.Row="1" Grid.ColumnSpan="2" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<Button Grid.Row="0" Grid.Column="1" Content="Output Directory" Click="OutputDirectoryButton_Click"/>
<TextBlock Grid.Row="0" Grid.Column="2" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="10" Text="{Binding CurrentOutputDirectory, ElementName=_this}"/>
<Label Grid.Row="0" Grid.Column="3" VerticalContentAlignment="Center" Content="{Binding CurrentProfile, ElementName=_this}"/>
<ListBox Grid.Row="1" Grid.ColumnSpan="4" x:Name="taskListBox" ItemsSource="{Binding Source={x:Static local:FFmpegTask.RunningTasks}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
122 changes: 115 additions & 7 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,148 @@
using System.Windows;
using Ookii.Dialogs.Wpf;
using System;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Windows;

namespace FoxingVideo;

public partial class MainWindow : Window
{
const string DbConnectionString = "Data Source=foxingvideo.db;Version=3;";

public FFmpegProfile CurrentProfile
{
get { return (FFmpegProfile)GetValue(CurrentProfileProperty); }
set { SetValue(CurrentProfileProperty, value); }
}

// Using a DependencyProperty as the backing store for CurrentProfile. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentProfileProperty =
DependencyProperty.Register("CurrentProfile", typeof(FFmpegProfile), typeof(MainWindow), new PropertyMetadata());
public static readonly DependencyProperty CurrentProfileProperty = DependencyProperty.Register("CurrentProfile", typeof(FFmpegProfile), typeof(MainWindow), new PropertyMetadata());

public string CurrentOutputDirectory
{
get { return (string)GetValue(CurrentOutputDirectoryProperty); }
set { SetValue(CurrentOutputDirectoryProperty, value); }
}

public static readonly DependencyProperty CurrentOutputDirectoryProperty = DependencyProperty.Register("CurrentOutputDirectory", typeof(string), typeof(MainWindow), new PropertyMetadata());


public MainWindow()
{
InitializeComponent();

InitializeDb();

CurrentOutputDirectory = RetrieveCurrentDirectory();

CurrentProfile = FFmpegProfile.ListProfiles().First();
}

private void ProfileButton_Click(object sender, RoutedEventArgs e)
void ProfileButton_Click(object sender, RoutedEventArgs e)
{
var profileWindow = new ProfileWindow(this);

profileWindow.ShowDialog();
}

private async void Window_Drop(object sender, DragEventArgs e)
async void Window_Drop(object sender, DragEventArgs e)
{
if (CurrentOutputDirectory == null)
{
MessageBox.Show("Uh, output directory not set, wtf...");
return;
}

if (CurrentProfile == null)
{
MessageBox.Show("Uh, profile not set, wtf...");
return;
}

var files = (string[])e.Data.GetData(DataFormats.FileDrop);

foreach (var file in files)
{
var task = new FFmpegTask(file, "b.mp4", CurrentProfile.Arguments);
var task = new FFmpegTask(CurrentProfile, file, CurrentOutputDirectory);

await task.Run();
}
}

static void InitializeDb()
{
using var conn = new SQLiteConnection(DbConnectionString);

conn.Open();

string sql = @"
CREATE TABLE IF NOT EXISTS Config
(
Id INTEGER PRIMARY KEY,
CurrentDirectory TEXT
);";

using var cmd = new SQLiteCommand(sql, conn);

cmd.ExecuteNonQuery();
}

static void StoreCurrentDirectory(string directoryPath)
{
using var conn = new SQLiteConnection(DbConnectionString);

conn.Open();

var sql = "INSERT INTO Config (CurrentDirectory) VALUES (@dirPath);";

using var cmd = new SQLiteCommand(sql, conn);

cmd.Parameters.AddWithValue("@dirPath", directoryPath);

cmd.ExecuteNonQuery();
}

static string RetrieveCurrentDirectory()
{
using var conn = new SQLiteConnection(DbConnectionString);

conn.Open();

var sql = "SELECT CurrentDirectory FROM Config ORDER BY Id DESC LIMIT 1;";

using var cmd = new SQLiteCommand(sql, conn);

using var reader = cmd.ExecuteReader();

if (reader.Read())
{
return reader["CurrentDirectory"].ToString();
}

// If no directory was found in the database, get and store the current directory.
var currentDirectory = Directory.GetCurrentDirectory();

StoreCurrentDirectory(currentDirectory);

return currentDirectory;
}

private void OutputDirectoryButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new VistaFolderBrowserDialog();
dialog.Description = "Please select a folder.";
dialog.UseDescriptionForTitle = true; // This applies to the Vista style dialog only, not the old dialog.

if (!VistaFolderBrowserDialog.IsVistaFolderDialogSupported)
{
MessageBox.Show(this, "Because you are not using Windows Vista or later, the regular folder browser dialog will be used. Please use Windows Vista to see the new dialog.", "Sample folder browser dialog");
}

if ((bool)dialog.ShowDialog(this))
{
MessageBox.Show(this, $"The selected folder was:{Environment.NewLine}{dialog.SelectedPath}", "Sample folder browser dialog");
}

}
}
Binary file added ffmpeg.exe
Binary file not shown.
Binary file added ffplay.exe
Binary file not shown.
Binary file added ffprobe.exe
Binary file not shown.

0 comments on commit 5a2a2d9

Please sign in to comment.