-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove Letterboxing.cs
134 lines (113 loc) · 4.73 KB
/
Remove Letterboxing.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
/**
* Resize video events to match the aspect ratio of the project. This
* removes any letterboxing or pillarboxing. The script will operate
* on the selected video event or, if none are selected, it will
* operate on all video events.
*
* Revision Date: March 26, 2010.
**/
using System;
using System.Collections.Generic;
using ScriptPortal.Vegas;
public class EntryPoint
{
public void FromVegas(Vegas vegas)
{
double dWidthProject = vegas.Project.Video.Width;
double dHeightProject = vegas.Project.Video.Height;
double dPixelAspect = vegas.Project.Video.PixelAspectRatio;
double dAspect = dPixelAspect * dWidthProject / dHeightProject;
List<VideoEvent> events = new List<VideoEvent>();
AddSelectedVideoEvents(vegas, events);
if (0 == events.Count)
{
AddAllVideoEvents(vegas, events);
}
foreach (VideoEvent videoEvent in events)
{
Take take = videoEvent.ActiveTake;
if (null == take) continue;
VideoStream videoStream = take.MediaStream as VideoStream;
if (null == videoStream) continue;
double dMediaPixelAspect = videoStream.PixelAspectRatio;
foreach (VideoMotionKeyframe keyframe in videoEvent.VideoMotion.Keyframes)
{
MatchOutputAspect(keyframe, dMediaPixelAspect, dAspect);
}
}
}
void AddSelectedVideoEvents(Vegas vegas, List<VideoEvent> events)
{
foreach (Track track in vegas.Project.Tracks)
{
if (track.IsVideo())
{
foreach (VideoEvent videoEvent in track.Events)
{
if (videoEvent.Selected)
{
events.Add(videoEvent);
}
}
}
}
}
void AddAllVideoEvents(Vegas vegas, List<VideoEvent> events)
{
foreach (Track track in vegas.Project.Tracks)
{
if (track.IsVideo())
{
foreach (VideoEvent videoEvent in track.Events)
{
events.Add(videoEvent);
}
}
}
}
void MatchOutputAspect(VideoMotionKeyframe keyframe, double dMediaPixelAspect, double dAspectOut)
{
VideoMotionKeyframe keyframeSave = keyframe;
try
{
double rotation = keyframe.Rotation;
// undo rotation so that we can get at correct aspect ratio.
//
keyframe.RotateBy(-rotation);
double dWidth = Math.Abs(keyframe.TopRight.X - keyframe.TopLeft.X);
double dHeight = Math.Abs(keyframe.BottomLeft.Y - keyframe.TopLeft.Y);
double dCurrentAspect = dMediaPixelAspect * dWidth / dHeight;
double centerY = keyframe.Center.Y;
double centerX = keyframe.Center.X;
double dFactor;
VideoMotionBounds bounds = new VideoMotionBounds(keyframe.TopLeft, keyframe.TopRight, keyframe.BottomRight, keyframe.BottomLeft);
if (dCurrentAspect < dAspectOut)
{
// alter y coords
dFactor = dCurrentAspect / dAspectOut;
bounds.TopLeft.Y = (float) ((bounds.TopLeft.Y - centerY) * dFactor + centerY);
bounds.TopRight.Y = (float) ((bounds.TopRight.Y - centerY) * dFactor + centerY);
bounds.BottomLeft.Y = (float) ((bounds.BottomLeft.Y - centerY) * dFactor + centerY);
bounds.BottomRight.Y = (float) ((bounds.BottomRight.Y - centerY) * dFactor + centerY);
}
else
{
// alter x coords
dFactor = dAspectOut / dCurrentAspect;
bounds.TopLeft.X = (float) ((bounds.TopLeft.X - centerX) * dFactor + centerX);
bounds.TopRight.X = (float) ((bounds.TopRight.X - centerX) * dFactor + centerX);
bounds.BottomLeft.X = (float) ((bounds.BottomLeft.X - centerX) * dFactor + centerX);
bounds.BottomRight.X = (float) ((bounds.BottomRight.X - centerX) * dFactor + centerX);
}
// set it to new bounds
keyframe.Bounds = bounds;
// restore rotation.
keyframe.RotateBy (rotation);
}
catch (Exception e)
{
// restore original settings on error
keyframe = keyframeSave;
}
}
}