-
Notifications
You must be signed in to change notification settings - Fork 0
/
openshot_service.go
203 lines (176 loc) · 5.57 KB
/
openshot_service.go
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
package main
import (
"fmt"
"math"
"github.com/Bimde/fancam-generator/config"
"github.com/Bimde/fancam-generator/openshot"
log "github.com/sirupsen/logrus"
)
const (
projectName = "Test Project #1"
defaultFileName = "DALLA_DALLA.mp4"
deafultS3Folder = "files/"
deafultS3Bucket = "fancamgenerator"
openshotURL = "http://cloud.openshot.org/"
scale = 0
fps = 30
height = 1080
width = 1920
frameWidth = width / 5
)
var (
openShot *openshot.OpenShot
clients map[int64]*OpenShot
)
func init() {
openShot = openshot.New(openshotURL, config.GetString(config.Username), config.GetString(config.Password))
clients = map[int64]*OpenShot{}
}
type OpenShot struct {
project *openshot.Project
file *openshot.File
clip *openshot.Clip
}
// GetClient returns the OpenShot client associated with the particular id.
// Creates a new client and the mapping to the id if it doesn't exist. This
// function is purpose-built for dealing a large number of projects at once,
// i.e when dealing with aws rekognition people pathing results
func GetClient(ID int64) *OpenShot {
if clients[ID] == nil {
clients[ID] = newOpenShot()
}
return clients[ID]
}
// TriggerAllExports starts exporting all projects associated with any OpenShot clients
// created through GetClient.
func TriggerAllExports() *[]*openshot.Export {
exports := make([]*openshot.Export, len(clients))
for index, client := range clients {
exports[index] = triggerExport(index, deafultExport(client.project), client)
}
return &exports
}
// TriggerAllExportsTrimmed provides the same functionality as TriggerAllExports,
// except with each export being trimmed to the range of entries for all properties.
func TriggerAllExportsTrimmed() *[]*openshot.Export {
exports := make([]*openshot.Export, len(clients))
for index, client := range clients {
export := deafultExport(client.project)
trim(export, client.clip)
exports[index] = triggerExport(index, export, client)
}
return &exports
}
func triggerExport(index int64, export *openshot.Export, client *OpenShot) *openshot.Export {
client.saveClip()
export, err := client.createExport(export)
if err != nil {
log.WithField("index", index).Error("error exporting project ", err)
export = deafultExport(client.project)
export.URL = fmt.Sprintf("Export failed for projectID: %d, index: %d", client.project.ID, index)
}
return export
}
func newOpenShot() *OpenShot {
project := createProject(defaultProject())
file := createFile(project, defaultFile())
clip := createClip(project, defaultClip(file, project))
return &OpenShot{project: project, file: file, clip: clip}
}
func (o *OpenShot) AddTrackingFrame(timestamp int64, width float64, left float64) {
openShot.AddPropertyPoint(o.clip, openshot.LocationX, o.getFrame(timestamp), o.getLocationX(left))
}
func (o *OpenShot) saveClip() error {
var err error
o.clip, err = openShot.UpdateClip(o.clip)
return err
}
func (o *OpenShot) createExport(export *openshot.Export) (*openshot.Export, error) {
export, err := openShot.CreateExport(o.project, export)
if err != nil {
return nil, err
}
return export, nil
}
func (o *OpenShot) getLocationX(left float64) float64 {
// TODO change hardcoded formula values to be proportional to frame / video dimensions
return (1-left)*5 - 2.75
}
func (o *OpenShot) getFrame(timestamp int64) int {
return int((float64(timestamp) / 1000.0) * float64(fps))
}
func createProject(project *openshot.Project) *openshot.Project {
project, err := openShot.CreateProject(project)
if err != nil {
log.Panic("error creating project ", err)
}
return project
}
func createFile(project *openshot.Project, input *openshot.FileUploadS3) *openshot.File {
file, err := openShot.CreateFile(project, input)
if err != nil {
log.Panic("error creating file ", err)
}
return file
}
// createClip creates a clip uses openshot, sets scale and clears LocationX
func createClip(project *openshot.Project, input *openshot.Clip) *openshot.Clip {
clip, err := openShot.CreateClip(project, input)
if err != nil {
log.Panic("error creating clip ", err)
}
openShot.SetScale(clip, scale)
openShot.ClearPropertyPoints(clip, openshot.LocationX)
return clip
}
func defaultProject() *openshot.Project {
return &openshot.Project{
Name: projectName,
FPSNumerator: fps,
FPSDenominator: 1,
Height: height,
Width: width,
}
}
func defaultFile() *openshot.FileUploadS3 {
return openshot.CreateFileStruct(openshot.CreateFileS3InfoStruct(defaultFileName, deafultS3Folder, deafultS3Bucket))
}
func defaultClip(file *openshot.File, project *openshot.Project) *openshot.Clip {
return openshot.CreateClipStruct(file, project)
}
func deafultExport(project *openshot.Project) *openshot.Export {
o := openshot.CreateDefaultExportStruct(project)
o.JSON["width"] = frameWidth
return o
}
func trim(export *openshot.Export, clip *openshot.Clip) {
property := openShot.GetProperty(clip, openshot.LocationX)
export.StartFrame = getFirstFrame(property)
export.EndFrame = getLastFrame(property)
log.WithFields(log.Fields{
"project": clip.ProjectURL,
"startFrame": export.StartFrame,
"endFrame": export.EndFrame,
}).Infof(
"seconds: %f",
((float64(export.EndFrame) - float64(export.StartFrame)) / fps),
)
}
func getFirstFrame(property *openshot.Property) int {
min := math.MaxInt32
for _, point := range property.Points {
if point.Co.X < min {
min = point.Co.X
}
}
return min
}
func getLastFrame(property *openshot.Property) int {
max := 0
for _, point := range property.Points {
if point.Co.X > max {
max = point.Co.X
}
}
return max
}