-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFmpegInstaller.cs
More file actions
161 lines (140 loc) · 5.36 KB
/
FFmpegInstaller.cs
File metadata and controls
161 lines (140 loc) · 5.36 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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace LiteCut
{
public static class FFmpegInstaller
{
public static bool InstallFFmpeg()
{
try
{
ProcessStartInfo startInfo = new()
{
FileName = "winget",
Arguments = "install Gyan.FFmpeg -h",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using Process process = new();
process.StartInfo = startInfo;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
// Refresh this process' PATH from the registry so newly-installed executables are found without restarting
RefreshProcessPathFromRegistry();
// Quick verification: try running `ffmpeg -version`
if (CanRunFFmpeg())
{
return true;
}
var ffmpegInstallationDirectory = FindFFmpeg();
if (!string.IsNullOrEmpty(ffmpegInstallationDirectory))
{
string dir = Path.GetDirectoryName(ffmpegInstallationDirectory);
string current = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) ?? string.Empty;
Environment.SetEnvironmentVariable("PATH", dir + ";" + current, EnvironmentVariableTarget.Process);
if (!CanRunFFmpeg())
{
MessageBox.Show("FFmpeg was not directly accessbile. You may need to restart LiteCut or your computer.");
return true; // installation likely succeeded, but process PATH wasn't picked up by all mechanisms
}
}
else
{
MessageBox.Show("FFmpeg was installed but could not be located. Try restarting Litecut or your computer.");
return true;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
return false;
}
return false;
}
private static void RefreshProcessPathFromRegistry()
{
try
{
const string systemEnvKey = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
string systemPath = Registry.LocalMachine.OpenSubKey(systemEnvKey)?.GetValue("Path") as string ?? string.Empty;
string userPath = Registry.CurrentUser.OpenSubKey("Environment")?.GetValue("Path") as string ?? string.Empty;
string merged;
if (string.IsNullOrEmpty(systemPath)) merged = userPath;
else if (string.IsNullOrEmpty(userPath)) merged = systemPath;
else merged = systemPath + ";" + userPath;
if (!string.IsNullOrEmpty(merged))
{
Environment.SetEnvironmentVariable("PATH", merged, EnvironmentVariableTarget.Process);
}
}
catch
{
// Non-fatal — leave process PATH unchanged if registry read fails.
}
}
private static bool CanRunFFmpeg()
{
try
{
ProcessStartInfo psi = new()
{
FileName = "ffmpeg",
Arguments = "-version",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using Process p = Process.Start(psi);
if (p is null)
{
return false;
}
p.WaitForExit(3000);
return p.ExitCode == 0;
}
catch
{
return false;
}
}
private static string FindFFmpeg()
{
try
{
ProcessStartInfo psi = new()
{
FileName = "where",
Arguments = "ffmpeg",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using Process p = Process.Start(psi);
if (p == null) return string.Empty;
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit(2000);
if (string.IsNullOrWhiteSpace(output)) return string.Empty;
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var path = line.Trim();
if (File.Exists(path)) return path;
}
}
catch
{
}
return string.Empty;
}
}
}