-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
294 lines (253 loc) · 7.68 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
294 lines (253 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// MainWindow.xaml.cs
using FFMpegCore;
using FFMpegCore.Enums;
using FFMpegCore.Pipes;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Media.AppBroadcasting;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.ViewManagement;
using WinRT.Interop;
namespace Magnesium;
public sealed partial class MainWindow : Window
{
public ObservableCollection<VideoFile> VideoFiles { get; set; } = new();
private string _outputFolder = null; // null - same folder as source
public MainWindow()
{
this.InitializeComponent();
Title = "Magnesium Video Compressor";
ExtendsContentIntoTitleBar = true;
SetTitleBar(null);
// Subscribe to the Closed event
Closed += MainWindow_Closed;
}
private void MainWindow_Closed(object sender, WindowEventArgs args)
{
System.Diagnostics.Debug.WriteLine("MainWindow closed!");
// Clean up temporary files
string tempPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "temp");
if (Directory.Exists(tempPath))
{
try
{
Directory.Delete(tempPath, true);
System.Diagnostics.Debug.WriteLine("Temporary files cleaned up.");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error cleaning up temporary files: {ex.Message}");
}
}
}
private void FileListView_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Link;
}
private async void FileListView_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
await AddFilesToList(items);
}
}
}
private async Task AddFilesToList(IReadOnlyList<IStorageItem> items)
{
foreach (var item in items)
{
if (item is StorageFile file)
{
if (!VideoFiles.Any(vf => vf.FullPath == file.Path))
{
var videoFile = await GetVideoMetadata(file);
if (videoFile != null)
{
VideoFiles.Add(videoFile);
}
}
}
else if (item is StorageFolder folder)
{
await AddFilesToList(await folder.GetItemsAsync());
}
}
}
private async Task<string> GetThumbnail(StorageFile file, double aspectRatio)
{
string tempRelativePath = Path.Combine("temp", Guid.NewGuid().ToString() + ".png");
string tempPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, tempRelativePath);
// Ensure directory exists
if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
}
bool resp = await FFMpeg.SnapshotAsync(file.Path, tempPath, new Size(512, Convert.ToInt32(512*aspectRatio)), TimeSpan.FromSeconds(1));
System.Diagnostics.Debug.WriteLine($"Snapshot for {file.Name} resp: {resp} tempPath: {tempPath}");
return "ms-appdata:///Local/" + tempRelativePath;
}
private async Task<VideoFile> GetVideoMetadata(StorageFile file)
{
try
{
var mediaInfo = await FFProbe.AnalyseAsync(file.Path);
var videoStream = mediaInfo.VideoStreams.FirstOrDefault();
// Not a video file
if (videoStream == null) return null;
var props = await file.GetBasicPropertiesAsync();
var aspectRatio = (double)videoStream.Height / videoStream.Width;
var thumbnailPath = await GetThumbnail(file, aspectRatio);
return new VideoFile
{
Name = file.Name,
FullPath = file.Path,
ThumbnailPath = thumbnailPath,
Extension = file.FileType,
Duration = mediaInfo.Duration,
OriginalSize = FormatBytes(props.Size),
NewSize = "N/A",
SavedPercentage = "N/A",
Fps = Math.Round(videoStream.AvgFrameRate, 2),
AudioTracks = mediaInfo.AudioStreams.Count(),
DateCreated = file.DateCreated.DateTime,
DateModified = props.DateModified.DateTime,
Status = "Pending",
IsConverting = false
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error getting metadata for {file.Name}: {ex.Message}");
return null;
}
}
private static string FormatBytes(ulong bytes)
{
string[] suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return $"{dblSByte:0.##} {suffix[i]}";
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var filePath = button.Tag as string;
var fileToRemove = VideoFiles.FirstOrDefault(f => f.FullPath == filePath);
if (fileToRemove != null)
{
VideoFiles.Remove(fileToRemove);
}
}
private async void SelectFolderButton_Click(object sender, RoutedEventArgs e)
{
var folderPicker = new FolderPicker();
InitializeWithWindow.Initialize(folderPicker, GetWindowHandle());
folderPicker.FileTypeFilter.Add("*");
var folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
_outputFolder = folder.Path;
OutputFolderTextBox.Text = folder.Path;
}
}
private double GetFpsFromComboBox()
{
var selectedItem = FpsComboBox.SelectedItem as ComboBoxItem;
if (selectedItem != null)
{
if (int.TryParse(selectedItem.Tag.ToString(), out int selectedFps))
{
return selectedFps;
}
}
return 0;
}
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
StartButton.IsEnabled = false;
TotalProgressBar.Value = 0;
for (int i = 0; i < VideoFiles.Count; i++)
{
var file = VideoFiles[i];
file.IsConverting = true;
file.Status = "In progress...";
try
{
var originalSize = new FileInfo(file.FullPath).Length;
string outputExtension = Path.GetExtension(file.FullPath);
string outputFileName = $"{Path.GetFileNameWithoutExtension(file.FullPath)}_magnesium{outputExtension}";
string outputDir = _outputFolder ?? Path.GetDirectoryName(file.FullPath);
string outputPath = Path.Combine(outputDir, outputFileName);
// Create FFMPEG
var ffmpegArgumentProcessor = FFMpegArguments
.FromFileInput(file.FullPath)
.OutputToFile(outputPath, true, options =>
{
// Codec setup
switch (CodecComboBox.SelectedIndex)
{
case 0: options.WithVideoCodec(VideoCodec.LibX264); break;
case 1: options.WithVideoCodec(VideoCodec.LibX265); break;
case 2: options.WithVideoCodec(VideoCodec.LibaomAv1); break;
case 3: options.WithVideoCodec("hevc_nvenc"); break;
}
// Copy audio without changes
options.WithAudioCodec(AudioCodec.Copy);
// Set quality
options.WithConstantRateFactor((int)QualitySlider.Value);
// Set FPS if needed
double fps = GetFpsFromComboBox();
if (fps > 0)
{
options.WithFramerate(fps);
}
});
ffmpegArgumentProcessor.NotifyOnProgress(percent =>
{
DispatcherQueue.TryEnqueue(() =>
{
CurrentFileProgressBar.Value = percent;
});
}, file.Duration);
await ffmpegArgumentProcessor.ProcessAsynchronously();
var newSize = new FileInfo(outputPath).Length;
file.NewSize = FormatBytes((ulong)newSize);
file.SavedPercentage = $"{100 - (100.0 * newSize / originalSize):0.##}%";
file.Status = "Done";
}
catch (Exception ex)
{
file.Status = "Error";
System.Diagnostics.Debug.WriteLine($"Conversion failed for {file.Name}: {ex.Message}");
}
finally
{
file.IsConverting = false;
CurrentFileProgressBar.Value = 0;
TotalProgressBar.Value = ((double)(i + 1) / VideoFiles.Count) * 100;
}
}
StartButton.IsEnabled = true;
}
private nint GetWindowHandle()
{
return WindowNative.GetWindowHandle(this);
}
}