This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
299 lines (265 loc) · 9.41 KB
/
MainWindow.xaml.cs
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
295
296
297
298
299
using System;
using io = System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public string ffmpegExe;
public string ffplayExe;
public string srcFilePath = "";
public MainWindow()
{
InitializeComponent();
// 1. 先在命令行PATH中找ffmpeg
// 2. 其次在程序执行目录中ffmpeg
// 3. 都没找到退出
ffmpegExe = GetFullPath("ffmpeg.exe");
ffplayExe = GetFullPath("ffplay.exe");
if(ffmpegExe == null) ffmpegExe = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
if (ffplayExe == null) ffplayExe = AppDomain.CurrentDomain.BaseDirectory + "ffplay.exe";
if (System.IO.File.Exists(ffmpegExe) == false)
{
MessageBox.Show("未找到[ffmpeg.exe],工具将无法使用", "错误",MessageBoxButton.OK, MessageBoxImage.Error);
System.Windows.Application.Current.Shutdown();
}
if (System.IO.File.Exists(ffplayExe) == false)
{
MessageBox.Show("未找到[ffplay.exe],播放功能无法使用", "错误",MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private string GetFullPath(string fileName)
{
if (io.File.Exists(fileName))
return io.Path.GetFullPath(fileName);
var values = Environment.GetEnvironmentVariable("PATH");
foreach (var path in values.Split(io.Path.PathSeparator))
{
var fullPath = io.Path.Combine(path, fileName);
if (io.File.Exists(fullPath))
return fullPath;
}
return null;
}
private void Drop1(object sender, DragEventArgs e)
{
string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);
if (filenames.Length > 0)
{
srcFilePath = filenames[0];
selectRes.Text = srcFilePath;
}
}
private void Drop2(object sender, DragEventArgs e)
{
string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);
if (filenames.Length > 0)
{
inpytAudio.Text = filenames[0];
}
}
/// <summary>
/// 带控制台,方便看到更多信息,和错误
/// </summary>
/// <param name="command"></param>
private void execute(string command)
{
try
{
System.Diagnostics.Process.Start(ffmpegExe, command);
}
catch (Exception)
{
MessageBox.Show("执行错误: " + command);
}
}
/// <summary>
/// 将处理后的资源,写入相同目录下
/// </summary>
/// <returns></returns>
private string getOutputFilepath(string extension = "")
{
extension = extension.Length != 0 ? extension : io.Path.GetExtension(srcFilePath);
string time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
// xxx/xxx/[time]-oldname.extension
return io.Path.GetDirectoryName(srcFilePath) + "\\[" + time + "]-" + io.Path.GetFileNameWithoutExtension(srcFilePath) + extension;
}
/// <summary>
/// 选择文件视频
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "(*.*)|*.*"
};
if (openFileDialog.ShowDialog() == false) return;
srcFilePath = openFileDialog.FileName;
selectRes.Text = srcFilePath;
}
/// <summary>
/// 去掉视频音轨
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string dstFilePath = getOutputFilepath();
// ffmpeg -i "demo.mp4" -vcodec copy -an "out.mp4"
string command = "-i \"" + srcFilePath + "\" -vcodec copy -an \"" + dstFilePath + "\"";
execute(command);
}
/// <summary>
/// 提取视频音轨
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string dstFilePath = getOutputFilepath(".mp3");
// ffmpeg -i demo.mp4 out.mp3
string command = "-i \"" + srcFilePath + "\" \"" + dstFilePath + "\"";
execute(command);
}
/// <summary>
/// 调整音量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
if (inputVolume.Text.Length == 0) return;
string dstFilePath = getOutputFilepath();
// 视频不变,减少音量
// ffmpeg - i input.mp4 -vcodec copy -af "volume=-10dB" out.mp4
string command = "-i \"" + srcFilePath + "\" -vcodec copy -af \"volume=" + inputVolume.Text + "\" \"" + dstFilePath + "\"";
execute(command);
}
/// <summary>
/// 使用ffplay播放输入资源,视频,音频,图片,直播流...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_5(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string command = "\"" + srcFilePath + "\"";
System.Diagnostics.Process.Start(ffplayExe, command);
}
/// <summary>
/// 音频填充视频
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_6(object sender, RoutedEventArgs e)
{
// 将10s视频和5s音频合并,输出视频有10s,音频将一直循环
// ffmpeg -i input.mp4 -stream_loop -1 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 -shortest out.mp4
if (srcFilePath.Length == 0) return;
if (inpytAudio.Text.Length == 0) return;
string dstFilePath = getOutputFilepath();
// 从1小时到结尾
// ffmpeg -ss 01:00:00 -i m.mp4 -c copy out.mp4
string command = "-i \"" + srcFilePath + "\" -stream_loop -1 -i \"" + inpytAudio.Text + "\" -c copy -map 0:v:0 -map 1:a:0 -shortest \""+ dstFilePath +"\"";
execute(command);
}
/// <summary>
/// 视频填充音频
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_7(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
if (inpytAudio.Text.Length == 0) return;
string dstFilePath = getOutputFilepath();
// 将5s视频和10s音频合并,输出视频有10s,视频将一直循环
// ffmpeg -stream_loop -1 -i input.mp4 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 -shortest out.mp4
string command = "-stream_loop -1 -i \""+srcFilePath+"\" -i \""+ inpytAudio.Text +"\" -c copy -map 0:v:0 -map 1:a:0 -shortest \""+ dstFilePath +"\"";
execute(command);
}
/// <summary>
/// 选择音频文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_8(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "(*.*)|*.*"
};
if (openFileDialog.ShowDialog() == false) return;
inpytAudio.Text = openFileDialog.FileName;
}
/// <summary>
/// 从开始处裁剪指定时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_9(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string dstFilePath = getOutputFilepath();
// ffmpeg -i input.mp4 -ss 00:00:00 -t 10 1.mp3
string command = "-i \"" + srcFilePath + "\" -ss "+ c_1_0.Text +" -t "+ c_1_1.Text +" \"" + dstFilePath + "\"";
execute(command);
}
/// <summary>
/// 时间段裁剪
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_10(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string dstFilePath = getOutputFilepath();
DateTime start = DateTime.Parse(c_2_0.Text);
DateTime end = DateTime.Parse(c_2_1.Text);
int t = (end.Second - start.Second);
if(t < 0)
{
MessageBox.Show("切割时间不能为负!!!");
return;
}
// ffmpeg -i input.mp4 -ss 00:00:00 -t 10 1.mp3
string command = "-i \"" + srcFilePath + "\" -ss " + c_2_0.Text + " -t " + t.ToString() + " \"" + dstFilePath + "\"";
execute(command);
}
/// <summary>
/// 从指定时间道结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_11(object sender, RoutedEventArgs e)
{
if (srcFilePath.Length == 0) return;
string dstFilePath = getOutputFilepath();
// 从1小时到结尾
// ffmpeg -ss 01:00:00 -i m.mp4 -c copy out.mp4
string command = "-ss "+ c_3_0.Text + " -i \"" + srcFilePath + "\" -c copy \"" + dstFilePath + "\"";
execute(command);
}
}
}