forked from tmarplatt/VegasScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VegasShakeScript.cs
297 lines (248 loc) · 8.17 KB
/
VegasShakeScript.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
/**
Simple camera shake effect for Sony Vegas.
Generates horizontal and vertical sinusoid motion.
Uses Pan+Crop keyframes to achieve the effect
all along the duration of the clip.
By Tommy Marplatt - December 2015
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ScriptPortal.Vegas;
public class EntryPoint
{
//Rate of shake multiplier
Double SkewIn = 12;
//Synchronicity factor between horizontal and vertical movements
Double SkewXY = 1.5;
//Number of pixels to displace
Single SkewOut = 4;
//Affect displacement horizontally as a proportion of vertical displacement
Single XToYRatio = 2.5f;
//Reset Pan on first frame
bool ShouldResetPan = false;
//Start all over?
bool ShouldClearFrames = true;
void Shout(string shout)
{
MessageBox.Show(shout);
}
public void FromVegas(Vegas vegas)
{
if (DialogResult.OK != DoDialog(vegas))
{
return;
}
ShakeClip(vegas);
}
DialogResult DoDialog(Vegas vegas)
{
Form form = new Form();
form.SuspendLayout();
form.StartPosition = FormStartPosition.CenterParent;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.HelpButton = false;
form.ShowInTaskbar = false;
form.AutoSize = true;
form.AutoSizeMode = AutoSizeMode.GrowAndShrink;
form.Text = "Camera Shake Settings";
TableLayoutPanel l = new TableLayoutPanel();
l.AutoSize = true;
l.AutoSizeMode = AutoSizeMode.GrowAndShrink;
l.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
l.ColumnCount = 2;
l.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 180));
l.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 40));
form.Controls.Add(l);
ToolTip tt = new ToolTip();
Label label = new Label();
label.Margin = new Padding(4, 4, 4, 4);
label.Text = "Speed";
l.Controls.Add(label);
TextBox SkewInBox = new TextBox();
SkewInBox.Text = String.Format("{0}", SkewIn);
l.Controls.Add(SkewInBox);
tt.SetToolTip(label, "Changes the frequency of the shake.");
tt.SetToolTip(SkewInBox, "Changes the frequency of the shake.");
label = new Label();
label.Margin = new Padding(4, 4, 4, 4);
label.Text = "H/V synchronicity";
l.Controls.Add(label);
TextBox SkewXYBox = new TextBox();
SkewXYBox.Text = String.Format("{0}", SkewXY);
l.Controls.Add(SkewXYBox);
tt.SetToolTip(label, "Changes the relative vertical speed. Set to 1 to move in a circle.");
tt.SetToolTip(SkewXYBox, "Changes the relative vertical speed. Set to 1 to move in a circle.");
label = new Label();
label.Margin = new Padding(4, 4, 4, 4);
label.Text = "Amount";
l.Controls.Add(label);
TextBox SkewOutBox = new TextBox();
SkewOutBox.Text = String.Format("{0}", SkewOut);
l.Controls.Add(SkewOutBox);
tt.SetToolTip(label, "Number of pixels the camera will shift away from the center. It is also the margin of zoom-in.");
tt.SetToolTip(SkewOutBox, "Number of pixels the camera will shift away from the center. It is also the margin of zoom-in.");
label = new Label();
label.Margin = new Padding(4, 4, 4, 4);
label.AutoSize = false;
label.TextAlign = ContentAlignment.MiddleLeft;
label.Text = "H/V ratio of displacement";
label.Anchor = AnchorStyles.Left|AnchorStyles.Right;
l.Controls.Add(label);
TextBox XToYBox = new TextBox();
XToYBox.Text = String.Format("{0}", XToYRatio);
l.Controls.Add(XToYBox);
tt.SetToolTip(label, "Multiply horizontal distance. Values above 1 will produce a greater zoom-in.");
tt.SetToolTip(XToYBox, "Multiply horizontal distance. Values above 1 will produce a greater zoom-in.");
CheckBox SRP = new CheckBox();
SRP.Text = "Reset Pan/Crop on first frame";
SRP.Checked = ShouldResetPan;
SRP.AutoSize = false;
SRP.Margin = new Padding(4,4,4,4);
SRP.Anchor = AnchorStyles.Left|AnchorStyles.Right;
l.Controls.Add(SRP);
l.SetColumnSpan(SRP, 2);
tt.SetToolTip(SRP, "Leave unchecked to shake within the current video zoom.");
CheckBox SCF = new CheckBox();
SCF.Text = "Reset all frames before shaking.";
SCF.Checked = ShouldClearFrames;
SCF.AutoSize = false;
SCF.Margin = new Padding(4,4,4,4);
SCF.Anchor = AnchorStyles.Left|AnchorStyles.Right;
l.Controls.Add(SCF);
l.SetColumnSpan(SCF, 2);
tt.SetToolTip(SCF, "Leave unchecked to multiply the new shake effect with a previous shake effect.");
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.FlowDirection = FlowDirection.RightToLeft;
panel.Size = Size.Empty;
panel.AutoSize = true;
panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
panel.Anchor = AnchorStyles.Top|AnchorStyles.Right;
l.Controls.Add(panel);
l.SetColumnSpan(panel, 2);
Button cancel = new Button();
cancel.Text = "Cancel";
cancel.DialogResult = DialogResult.Cancel;
panel.Controls.Add(cancel);
form.CancelButton = cancel;
Button ok = new Button();
ok.Text = "Ok";
ok.DialogResult = DialogResult.OK;
panel.Controls.Add(ok);
form.AcceptButton = ok;
form.ResumeLayout();
DialogResult result = form.ShowDialog(vegas.MainWindow);
if (DialogResult.OK == result)
{
try {
SkewIn = Double.Parse(SkewInBox.Text);
SkewXY = Double.Parse(SkewXYBox.Text);
SkewOut = Single.Parse(SkewOutBox.Text);
XToYRatio = Single.Parse(XToYBox.Text);
ShouldResetPan = SRP.Checked;
ShouldClearFrames = SCF.Checked;
}
catch (FormatException e)
{
Shout("Invalid parameter! " + e.Message);
return DialogResult.Cancel;
}
}
return result;
}
void ShakeClip(Vegas vegas)
{
VideoEvent ve = FindSelected(vegas.Project);
if (! ve.IsValid())
{
Shout("No video event selected!");
return;
}
if (ShouldClearFrames)
{
// Remove all pan/crop keyframes
ve.VideoMotion.Keyframes.Clear();
// Restore zoom to default
if (ShouldResetPan)
{
ResetPan(ve);
}
// Populate with keyframes: one per frame
Populate(ve);
}
//Shake that camera
PanShake(ve);
}
void PanShake(VideoEvent ve)
{
int i = 0;
foreach (VideoMotionKeyframe key in ve.VideoMotion.Keyframes)
{
SafelyScaleVideo(key);
VideoMotionVertex movertex = MoveDelta(i);
key.MoveBy(movertex);
i++;
}
}
void ResetPan(VideoEvent ve)
{
VideoStream v = ve.ActiveTake.Media.GetVideoStreamByIndex(0);
ve.VideoMotion.Keyframes[0].Bounds = new VideoMotionBounds( new VideoMotionVertex(0,0), new VideoMotionVertex(v.Width, 0), new VideoMotionVertex(v.Width, v.Height), new VideoMotionVertex(0,v.Height));
}
void SafelyScaleVideo(VideoMotionKeyframe key)
{
VideoMotionVertex delta = new VideoMotionVertex(0,0);
//Obtain the scaling ratio for each axis
delta.X = ( - (2 * SkewOut * XToYRatio) + (key.TopRight.X - key.TopLeft.X) ) / (key.TopRight.X - key.TopLeft.X);
delta.Y = ( - (2 * SkewOut) + (key.BottomLeft.Y - key.TopLeft.Y) ) / (key.BottomLeft.Y - key.TopLeft.Y);
key.ScaleBy(delta);
}
VideoMotionVertex MoveDelta(int n)
{
VideoMotionVertex delta = new VideoMotionVertex(0,0);
delta.X = (Single) Math.Sin(ToRads(n * SkewIn)) * SkewOut * XToYRatio;
delta.Y = (Single) Math.Cos(ToRads(n * SkewIn * SkewXY)) * SkewOut;
return delta;
}
Double ToRads(Double deg)
{
return deg * (Math.PI / 180);
}
void Populate(VideoEvent ve)
{
for (int i=0; i < ve.Length.FrameCount; i++)
{
VideoMotionKeyframe key = new VideoMotionKeyframe(Timecode.FromFrames(i));
ve.VideoMotion.Keyframes.Add(key);
}
}
VideoEvent FindSelected(Project project)
{
VideoEvent casket = new VideoEvent();
foreach (Track track in project.Tracks) {
if (track.IsVideo()) {
foreach (VideoEvent videoEvent in track.Events)
{
if (videoEvent.Selected)
{
return videoEvent;
}
}
}
}
return casket;
}
}