-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteCut.cs
More file actions
150 lines (139 loc) · 5.09 KB
/
LiteCut.cs
File metadata and controls
150 lines (139 loc) · 5.09 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
using FFMpegCore;
namespace LiteCut
{
public partial class LiteCut : Form
{
string initialFilePath = "";
public LiteCut(string[] args)
{
//check if FFMpeg is installed and prompt to install it if not
try
{
FFMpeg.GetCodecs();
}
catch (Exception e)
{
DialogResult result = MessageBox.Show($"Error retrieving FFmpeg codecs: \n {e.Message} \n\n This usually happens when FFmpeg is not installed. Do you want to try to install it now?", "error", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
var installResult = FFmpegInstaller.InstallFFmpeg();
if (installResult == true)
{
MessageBox.Show("Successfully installed FFMpeg!");
}
else
{
MessageBox.Show("Failed to install FFMpeg.");
}
}
}
//if opened via file association, get initial file path from arguments
if (args.Length == 1)
{
if (File.Exists(args[0]))
{
initialFilePath = args[0];
}
}
InitializeComponent();
if (!string.IsNullOrEmpty(initialFilePath))
{
PickFile(initialFilePath);
}
//associate a file path
try
{
FileAssociation.AssociateFile();
}
catch { } //it's really not too bad if it doesn't work.
}
private void PickFileButton_Click(object sender, EventArgs e)
{
var result = FileDialog.ShowDialog();
if (result == DialogResult.OK)
{
PickFile(FileDialog.FileName);
}
}
private async void PickFile(string path)
{
try
{
var info = await FFProbe.AnalyseAsync(path); //fetch duration of video and set the values of the time boxes
StartTimeBox.Invoke(() =>
{
StartTimeBox.Value = 0;
EndTimeBox.Maximum = (decimal)info.Duration.TotalSeconds;
EndTimeBox.Value = (decimal)info.Duration.TotalSeconds;
FileNameTextBox.Text = path;
});
}
catch
{
MessageBox.Show("Cannot retrieve video info. Please check the provided video, and if you have installed FFmpeg.");
}
}
//start a compression task
private async void CompressButton_Click(object sender, EventArgs e)
{
var size = (int)MbBox.Value;
var fileName = FileNameTextBox.Text;
var startTime = TimeSpan.FromSeconds((double)StartTimeBox.Value);
var endTime = TimeSpan.FromSeconds((double)EndTimeBox.Value);
var mergeAudio = MergeAudioTrackCheckBox.Checked;
var compression = new CompressionTask(fileName, fileName + "_compressed.mp4", size, startTime, endTime, mergeAudio );
compression.CompressionProgress += (sender, progress) =>
{
if (ProgressBar.InvokeRequired)
{
ProgressBar.Invoke(new MethodInvoker(() =>
{
ProgressBar.Value = (int)progress;
}));
}
};
ToggleFormControls(false);
try
{
await compression.CompressVideo().ConfigureAwait(false);
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"File not found: {ex.FileName}", "Error");
}
catch (Exception ex)
{
MessageBox.Show("Error compressing file: " + ex.Message, "Error");
}
MessageBox.Show("Compression done, file can be found under " + fileName + "_compressed.mp4");
ToggleFormControls(true);
ProgressBar.Invoke(new MethodInvoker(() =>
{
ProgressBar.Value = 0;
}));
}
//disable form controls during compression
private void ToggleFormControls(bool enabled)
{
PickFileButton.Invoke(
new MethodInvoker(() =>
{
PickFileButton.Enabled = enabled;
MbBox.Enabled = enabled;
StartTimeBox.Enabled = enabled;
EndTimeBox.Enabled = enabled;
}
)
);
}
//these methods stop you from setting your end before your start
private void StartTimeBox_ValueChanged(object sender, EventArgs e)
{
EndTimeBox.Minimum = StartTimeBox.Value;
}
private void EndTimeBox_ValueChanged(object sender, EventArgs e)
{
StartTimeBox.Maximum = EndTimeBox.Value;
}
}
}