-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPackForm.cs
277 lines (235 loc) · 8.73 KB
/
PackForm.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using Ionic.Zlib;
using ImageMagick;
namespace Aida64_Esp8266_DisplayControler
{
public partial class PackForm : Form
{
private SynchronizationContext Sync;
private Main main;
private Hashtable hashtable = new Hashtable();
private Dictionary<int, int> threadPercent = new Dictionary<int, int>();
int threadCount;
public PackForm(Main main)
{
InitializeComponent();
Sync = SynchronizationContext.Current;
this.main = main;
}
private void PackForm_Load(object sender, EventArgs e)
{
var tcount = Environment.ProcessorCount;
tbxThread.Value = tcount - 1;
tbxThread.Maximum = tcount;
}
private void SetPbar(object o)
{
var dic = (int[])o;
threadPercent[dic[0]] = dic[1];
var total = 0;
foreach (var d in threadPercent)
{
total += d.Value;
}
pbar.Value = total / threadCount;
}
private void BtnBrowser_Click(object sender, EventArgs e)
{
}
private void btnBrowser_Click(object sender, EventArgs e)
{
if (isVideo.Checked)
{
OpenFileDialog ofd = new OpenFileDialog
{
Multiselect = false
};
if (ofd.ShowDialog(this) == DialogResult.OK)
{
tbxPath.Text = ofd.FileName;
}
}
else
{
FolderBrowserDialog od = new FolderBrowserDialog();
if (od.ShowDialog(this) == DialogResult.OK)
{
tbxPath.Text = od.SelectedPath;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
if (!isVideo.Checked)
{
if (!Directory.Exists(tbxPath.Text))
return;
hashtable.Clear();
threadPercent.Clear();
threadCount = Convert.ToInt32(tbxThread.Value);
var files = Directory.GetFiles(tbxPath.Text);
MutilPack(files, threadCount);
}
else
{
if (!File.Exists(tbxPath.Text))
return;
if (!(main.CheckFileFromPath("ffmpeg.exe")) && !File.Exists(main.cfgjson.ffpath))
{
MessageBox.Show(this, "找不到ffmpeg,请手动指定路径", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
OpenFileDialog fd = new OpenFileDialog
{
Filter = "ffmpeg(ffmpeg.exe)|ffmpeg.exe"
};
if (fd.ShowDialog() == DialogResult.OK)
{
main.cfgjson.ffpath = fd.FileName;
}
else
{
MessageBox.Show(this, "找不到ffmpeg,处理终止!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
hashtable.Clear();
threadPercent.Clear();
GetPicFromVideo(tbxPath.Text, nbxWidth.Value.ToString() + "x" + nbxHeight.Value.ToString(), fpsnum.Value.ToString());
threadCount = Convert.ToInt32(tbxThread.Value);
var files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\VideoTempOutput");
MutilPack(files, threadCount);
}
}
private void PackImage(object files)
{
decimal i = 1;
int lastpercent = 0;
var fileArr = (string[])files;
//Sync.Send(SetLog, $"线程{Thread.CurrentThread.ManagedThreadId}开始执行");
foreach (var file in fileArr)
{
byte[] buf = Main.GetSingleBitmap(file);
MagickImage img = new MagickImage(buf) { Format = MagickFormat.Xbm };
var width = Convert.ToInt32(nbxWidth.Value);
var height = Convert.ToInt32(nbxHeight.Value);
img.Resize(new MagickGeometry($"{width}x{height}!"));
buf = img.ToByteArray();
lock (hashtable)
{
hashtable.Add(Path.GetFileName(file), buf);
}
var td = (i / fileArr.Length) * 100;
var percent = decimal.ToInt32(td);
if (lastpercent != percent)
{
lastpercent = percent;
Sync.Send(SetPbar, new int[] { Thread.CurrentThread.ManagedThreadId, percent });
}
i++;
}
}
private async void MutilPack(string[] arr, int splitCount)
{
string fname;
SaveFileDialog sd = new SaveFileDialog
{
Filter = "PackFile(*.dat)|*.dat",
Title = "请输入保存文件名"
};
if (sd.ShowDialog(this) == DialogResult.OK)
{
fname = sd.FileName;
}
else
{
return;
}
int size = arr.Length / splitCount;
Task[] taskArr = new Task[splitCount];
pnMain.Enabled = false;
for (int i = 0; i < splitCount; i++)
{
int index = i * size;
if (i == splitCount - 1)
size = arr.Length;
string[] subarr = arr.Skip(index).Take(size).ToArray();
var t = Task.Run(() =>
{
PackImage(subarr);
});
taskArr[i] = t;
}
await Task.WhenAll(taskArr);
pbar.Value = 99;
Main.PackData pack = new Main.PackData();
List<MemoryStream> ls = new List<MemoryStream>();
ArrayList akeys = new ArrayList(hashtable.Keys);
akeys.Sort();
foreach (string skey in akeys)
{
using (MemoryStream ms = new MemoryStream())
{
var ba = (byte[])hashtable[skey];
ms.Write(ba, 0, ba.Length);
ls.Add(ms);
}
}
pack.img = ls;
MemoryStream mm = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(mm, pack);
var data = GZipStream.CompressBuffer(mm.ToArray());
if (File.Exists(fname))
File.Delete(fname);
FileStream fs = new FileStream(fname, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Dispose();
pbar.Value = 100;
if(Directory.GetFiles(Directory.GetCurrentDirectory() + "\\VideoTempOutput").Length >= 0)
{
foreach(var file in Directory.GetFiles(Directory.GetCurrentDirectory() + "\\VideoTempOutput"))
{
File.Delete(file);
}
Directory.Delete(Directory.GetCurrentDirectory() + "\\VideoTempOutput");
}
Process.Start("Explorer.exe", "/select," + fname);
pnMain.Enabled = true;
mm.Dispose();
Close();
}
public void GetPicFromVideo(string VideoName, string WidthAndHeight,string FrameRate)
{
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\VideoTempOutput");
var ffpath = main.cfgjson.ffpath == null ? "ffmpeg.exe" : main.cfgjson.ffpath;
ProcessStartInfo startInfo = new ProcessStartInfo(ffpath)
{
WindowStyle = ProcessWindowStyle.Normal,
Arguments = @"-i """ + VideoName + @"""" + " -r " + FrameRate + " -f image2 -s " + WidthAndHeight + " " + @"""" + Directory.GetCurrentDirectory() + "\\VideoTempOutput\\%d.jpg" + @""""
};
Process ffmpegP = new Process
{
StartInfo = startInfo
};
try
{
ffmpegP.Start();
ffmpegP.WaitForExit();
ffmpegP.Close();
}
catch
{
MessageBox.Show("导出视频帧失败,请确保ffmpeg.exe存在并且能够使用");
return;
}
}
}
}