-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform.js
195 lines (187 loc) · 7.01 KB
/
transform.js
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
var fs = require("fs")
var path = require("path")
var ShortID = require("shortid")
var Bluebird = require("bluebird")
var Fluently = require("fluent-ffmpeg")
function transform(protovideo) {
return Bluebird.map(protovideo.clips, function(clips) {
return new Bluebird(function(resolve, reject) {
if(protovideo.color == undefined) {
protovideo.color = "0x000000"
} else {
if(!/^0x[0-9a-f]{6}$/i.test(protovideo.color)) {
if(/^#([0-9a-f]{3}){1,2}$/i.test(protovideo.color)) {
protovideo.color = hash2hex(protovideo.color)
} else {
throw new Error("The background color must be hex value.")
}
}
}
if(protovideo.width == undefined && protovideo.height == undefined) {
throw new Error("The video must have a width and height.")
}
var clip_duration = 0
for(var index in clips) {
var clip = clips[index]
if(clip.asset.duration - clip.trim.left - clip.trim.right > clip_duration) {
clip_duration = clip.asset.duration - clip.trim.left - clip.trim.right
}
if(clip.asset.file == null) {
//todo: better format for empty frames.
break
}
if(clip.trim == undefined) {
clip.trim = new Object()
} if(clip.trim.left == undefined) {
clip.trim.left = 0
} if(clip.trim.right == undefined) {
clip.trim.right = 0
}
if(clip.size == undefined) {
clip.size = new Object()
} if(clip.size.width == undefined) {
clip.size.width = clip.asset.size.width
} if(clip.size.height == undefined) {
clip.size.height = clip.asset.size.height
}
//todo: "autopad" default offset?
if(clip.offset == undefined) {
clip.offset = new Object()
} if(clip.offset.x == undefined) {
clip.offset.x = 0
} if(clip.offset.y == undefined) {
clip.offset.y = 0
}
}
var clip_directory = path.join(__dirname, "clips")
if(!fs.existsSync(clip_directory)) {
fs.mkdir(clip_directory)
}
var clip_file = path.join(clip_directory, ShortID.generate() + ".mp4")
var process = new Fluently()
process.addOutput(clip_file)
var filters = new Array()
filters.push({
"filter": "color",
"options": {
"color": protovideo.color,
"size": protovideo.width + "x" + protovideo.height,
"duration": clip_duration
},
"outputs": "nv:0"
})
filters.push({
"filter": "aevalsrc",
"options": {
"exprs": 0,
"duration": 0.25
},
"outputs": "[na:0]"
})
var video_output = "nv:0"
var audio_output = "na:0"
for(var index in clips)
{
var clip = clips[index]
if(clip.asset.file == null) {
break
}
process.addInput(clip.asset.file)
video_output = "nv:" + (parseInt(index) + 1)
audio_output = "na:" + (parseInt(index) + 1)
filters.push({
"filter": "trim",
"options": {
"start": clip.trim.left,
"end": clip.asset.duration - clip.trim.right
},
"inputs": index + ":v",
"outputs": "v:" + index + ":a"
})
filters.push({
"filter": "setpts",
"options": "PTS-STARTPTS",
"inputs": "v:" + index + ":a",
"outputs": "v:" + index + ":b"
})
filters.push({
"filter": "scale",
"options": {
"width": clip.size.width,
"height": clip.size.height
},
"inputs": "v:" + index + ":b",
"outputs": "v:" + index + ":c"
})
filters.push({
"filter": "overlay",
"options": {
"shortest": 1, //?!
"x": clip.offset.x,
"y": clip.offset.y
},
"inputs": [
"nv:" + index,
"v:" + index + ":c"
],
"outputs": video_output
})
filters.push({
"filter": "atrim",
"options": {
"start": clip.trim.left,
"end": clip.asset.duration - clip.trim.right
},
"inputs": index + ":a",
"outputs": "a:" + index + ":a",
})
filters.push({
"filter": "asetpts",
"options": "PTS-STARTPTS",
"inputs": "a:" + index + ":a",
"outputs": "a:" + index + ":b"
})
filters.push({
"filter": "amix",
"inputs": [
"na:" + index,
"a:" + index + ":b"
],
"outputs": audio_output
})
}
process.complexFilter(filters, [video_output, audio_output])
process.on("error", function(error) {
reject(error)
})
//process.on("error", function(error, stdout, stderr) {
// console.log(stdout)
// console.log(stderr)
//})
process.on("end", function() {
//todo: store the data in a clip variable
// before it needs to be resolved.
resolve({
"file": clip_file,
"duration": clip_duration
})
})
process.run()
})
}).then(function(clips) {
delete protovideo.color
protovideo.clips = clips
return protovideo
})
}
module.exports = transform
function hash2hex(color) {
color = color.substring(1)
if(color.length == 3) {
color = color[0] + color[0]
+ color[1] + color[1]
+ color[2] + color[2]
}
color = "0x" + color
return color
}