-
Notifications
You must be signed in to change notification settings - Fork 6
/
Datamix.cs
340 lines (302 loc) · 14.6 KB
/
Datamix.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Datamoshes a part of a video quickly and automatically (mosh a clip onto another).
//
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using Sony.Vegas;
namespace VegasDatamix {
public class EntryPoint {
private static readonly byte[] Array1 = {
0x42, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x28, 0x9d, 0xea, 0x5a, 0xc8, 0xd3, 0x11, 0xbb, 0x3a, 0x00, 0x50, 0xda, 0x1a,
0x5b, 0x06
};
private static readonly byte[] Array2 = {
0xc8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00,
0x00, 0x11, 0x00, 0x00, 0x00, 0x80, 0xbb, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x28,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
private static readonly byte[] Array3 = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x44,
0x49, 0x42, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
0x00, 0x6e, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x70, 0x00, 0x72, 0x00, 0x65, 0x00, 0x73, 0x00, 0x73, 0x00,
0x65, 0x00, 0x64, 0x00, 0x20, 0x00
};
private static readonly byte[] Array4 = {0x00, 0x00, 0x00, 0x00};
private static void GetStandardTemplates(Vegas vegas) {
GetTemplate(vegas, 12000);
GetTemplate(vegas, 12500);
GetTemplate(vegas, 14000);
GetTemplate(vegas, 14985);
GetTemplate(vegas, 15000);
GetTemplate(vegas, 16000);
GetTemplate(vegas, 23976);
GetTemplate(vegas, 24000);
GetTemplate(vegas, 25000);
GetTemplate(vegas, 29970);
GetTemplate(vegas, 30000);
GetTemplate(vegas, 50000);
GetTemplate(vegas, 59940);
GetTemplate(vegas, 60000);
}
private static RenderTemplate GetTemplate(Vegas vegas, int frameRate) {
if (frameRate >= 100 * 1000) {
throw new ArgumentException("Frame rate must be < 100!");
}
var frameString = (frameRate / 1000).ToString("00") + "." + (frameRate % 1000).ToString("000");
var name = "Uncompressed " + frameString;
var template = vegas.Renderers.FindByRendererID(0).Templates
.FindByName(name);
if (template != null) {
return template;
}
var appData = Environment.GetEnvironmentVariable("APPDATA");
if (appData == null) {
throw new IOException("APPDATA not set!");
}
var folder = Path.Combine(appData, "Sony", "Render Templates", "avi");
Directory.CreateDirectory(folder);
var file = Path.Combine(folder, name + ".sft2");
if (File.Exists(file)) {
return null;
}
using (var writer = new BinaryWriter(new FileStream(file, FileMode.Create, FileAccess.Write))) {
writer.Write(Array1, 0, Array1.Length);
var guid = Guid.NewGuid().ToByteArray();
writer.Write(guid, 0, guid.Length);
writer.Write(Array2, 0, Array2.Length);
writer.Write((double) frameRate / 1000);
writer.Write(Array3, 0, Array3.Length);
var chars = frameString.ToCharArray();
foreach (var ch in chars) {
writer.Write((byte) ch);
writer.Write((byte) 0x00);
}
writer.Write(Array4, 0, Array4.Length);
return null;
}
}
private void Encode(Vegas vegas, string scriptDirectory, RenderArgs renderArgs, string pathEncoded) {
var status = vegas.Render(renderArgs);
if (status != RenderStatus.Complete) {
MessageBox.Show("Unexpected render status: " + status);
return;
}
var encode = new Process {
StartInfo = {
UseShellExecute = false,
FileName = Path.Combine(scriptDirectory, "_internal", "ffmpeg", "ffmpeg.exe"),
WorkingDirectory = Path.Combine(scriptDirectory, "_internal"),
Arguments = "-y -hide_banner -nostdin -i \"" + renderArgs.OutputFile +
"\" -c:v libxvid -q:v 1 -g 1M -flags +mv4+qpel -mpeg_quant 1 -c:a copy \"" + pathEncoded +
"\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
encode.Start();
var error = encode.StandardError.ReadToEnd();
var output = encode.StandardOutput.ReadToEnd();
Debug.WriteLine(output);
Debug.WriteLine("---------------------");
Debug.WriteLine(error);
encode.WaitForExit();
File.Delete(renderArgs.OutputFile);
File.Delete(renderArgs.OutputFile + ".sfl");
}
public void FromVegas(Vegas vegas) {
var start = vegas.Transport.LoopRegionStart;
var length = vegas.Transport.LoopRegionLength;
if (start.FrameCount == 0) {
MessageBox.Show("Selection must start at frame >= 1!");
return;
}
if (length.FrameCount <= 1) {
MessageBox.Show("Selection length must be > 1 frame!");
return;
}
try {
var frameRate = vegas.Project.Video.FrameRate;
var frameRateInt = (int) Math.Round(frameRate * 1000);
var scriptDirectory = Path.GetDirectoryName(Script.File);
if (scriptDirectory == null) {
MessageBox.Show("Couldn't get script directory path!");
return;
}
var xvidPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(xvidPath)) {
xvidPath = Environment.GetEnvironmentVariable("ProgramFiles");
}
if (string.IsNullOrEmpty(xvidPath)) {
MessageBox.Show("Couldn't get Xvid install path!");
return;
}
xvidPath += @"\Xvid\uninstall.exe";
if (!File.Exists(xvidPath)) {
MessageBox.Show(
"Xvid codec not installed. The script will install it now and may ask for admin access to install it.");
var xvid = new Process {
StartInfo = {
UseShellExecute = true,
FileName = Path.Combine(scriptDirectory, "_internal", "xvid", "xvid.exe"),
WorkingDirectory = Path.Combine(scriptDirectory, "_internal"),
Arguments =
"--unattendedmodeui none --mode unattended --AutoUpdater no --decode_divx DIVX --decode_3ivx 3IVX --decode_divx DIVX --decode_other MPEG-4",
CreateNoWindow = true,
Verb = "runas"
}
};
try {
xvid.Start();
}
catch (Win32Exception e) {
if (e.NativeErrorCode == 1223) {
MessageBox.Show("Admin privilege for Xvid installation refused.");
return;
}
throw;
}
xvid.WaitForExit();
GetStandardTemplates(vegas);
GetTemplate(vegas, frameRateInt);
MessageBox.Show(
"Xvid installed and render template generated for the current frame rate. Please restart Sony Vegas and run the script again.");
return;
}
var template = GetTemplate(vegas, frameRateInt);
if (template == null) {
GetStandardTemplates(vegas);
GetTemplate(vegas, frameRateInt);
MessageBox.Show(
"Render template generated for the current frame rate. Please restart Sony Vegas and run the script again.");
return;
}
VideoTrack videoTrack = null;
for (var i = vegas.Project.Tracks.Count - 1; i >= 0; i--) {
videoTrack = vegas.Project.Tracks[i] as VideoTrack;
if (videoTrack != null) {
break;
}
}
if (videoTrack == null) {
MessageBox.Show("No track found!");
return;
}
var changed = false;
var finalFolder = (string) Registry.GetValue(
"HKEY_CURRENT_USER\\SOFTWARE\\Sony Creative Software\\Custom Presets",
"ClipFolder", "");
while (string.IsNullOrEmpty(finalFolder) || !Directory.Exists(finalFolder)) {
MessageBox.Show("Select the folder to put generated datamoshed clips into.");
changed = true;
var dialog = new CommonOpenFileDialog {
IsFolderPicker = true,
EnsurePathExists = true,
EnsureFileExists = false,
AllowNonFileSystemItems = false,
DefaultFileName = "Select Folder",
Title = "Select the folder to put generated datamoshed clips into"
};
if (dialog.ShowDialog() != CommonFileDialogResult.Ok) {
MessageBox.Show("No folder selected");
return;
}
finalFolder = dialog.FileName;
}
if (changed) {
Registry.SetValue(
"HKEY_CURRENT_USER\\SOFTWARE\\Sony Creative Software\\Custom Presets",
"ClipFolder", finalFolder, RegistryValueKind.String);
}
var pathSrc = Path.Combine(vegas.TemporaryFilesPath, Path.GetFileNameWithoutExtension(vegas.Project.FilePath) + "-" +
Guid.NewGuid().ToString("B").ToUpper().Substring(1, 8) + ".avi");
var pathEncodedSrc = Path.Combine(vegas.TemporaryFilesPath,
Path.GetFileNameWithoutExtension(vegas.Project.FilePath) + "-" +
Guid.NewGuid().ToString("B").ToUpper().Substring(1, 8) + ".avi");
Encode(vegas, scriptDirectory, new RenderArgs {
OutputFile = pathSrc,
Start = Timecode.FromFrames(start.FrameCount - 1),
Length = Timecode.FromFrames(1),
RenderTemplate = template
}, pathEncodedSrc);
var pathClip = Path.Combine(vegas.TemporaryFilesPath, Path.GetFileNameWithoutExtension(vegas.Project.FilePath) + "-" +
Guid.NewGuid().ToString("B").ToUpper().Substring(1, 8) + ".avi");
var pathEncodedClip = Path.Combine(vegas.TemporaryFilesPath,
Path.GetFileNameWithoutExtension(vegas.Project.FilePath) + "-" +
Guid.NewGuid().ToString("B").ToUpper().Substring(1, 8) + ".avi");
Encode(vegas, scriptDirectory, new RenderArgs {
OutputFile = pathClip,
Start = start,
Length = length,
RenderTemplate = template
}, pathEncodedClip);
var pathDatamixed = Path.Combine(finalFolder,
Path.GetFileNameWithoutExtension(vegas.Project.FilePath) + "-" +
Guid.NewGuid().ToString("B").ToUpper().Substring(1, 8)) + ".avi";
string[] datamoshConfig = {
"var input0=\"" + pathEncodedSrc.Replace("\\", "/") + "\";",
"var input1=\"" + pathEncodedClip.Replace("\\", "/") + "\";",
"var output=\"" + pathDatamixed.Replace("\\", "/") + "\";"
};
File.WriteAllLines(Path.Combine(scriptDirectory, "_internal", "config_datamix.js"), datamoshConfig);
var datamosh = new Process {
StartInfo = {
UseShellExecute = false,
FileName = Path.Combine(scriptDirectory, "_internal", "avidemux", "avidemux2_cli.exe"),
WorkingDirectory = Path.Combine(scriptDirectory, "_internal"),
Arguments = "--nogui --run avidemux_datamix.js",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
datamosh.Start();
datamosh.StandardInput.WriteLine("n");
var output = datamosh.StandardOutput.ReadToEnd();
var error = datamosh.StandardError.ReadToEnd();
Debug.WriteLine(output);
Debug.WriteLine("---------------------");
Debug.WriteLine(error);
datamosh.WaitForExit();
File.Delete(pathEncodedSrc);
File.Delete(pathEncodedSrc + ".sfl");
File.Delete(pathEncodedClip);
File.Delete(pathEncodedClip + ".sfl");
var media = vegas.Project.MediaPool.AddMedia(pathDatamixed);
media.TimecodeIn = Timecode.FromFrames(1);
var videoEvent = videoTrack.AddVideoEvent(start, Timecode.FromFrames(length.FrameCount - 1));
videoEvent.AddTake(media.GetVideoStreamByIndex(0));
}
catch (Exception e) {
MessageBox.Show("Unexpected exception: " + e.Message);
Debug.WriteLine(e);
}
}
}
}