-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreator.js
174 lines (148 loc) · 4.61 KB
/
creator.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
const path = require('path')
const fs = require('fs')
const colors = require('colors')
const { FFScene, FFText, FFCreator } = require('ffcreator')
const config = require('./config')
const { fontSize, lineHeight, paddingTop } = config.text
const { width, height } = config.creator
const { getLyrics } = require('./getLRC.js')
const create = ({ folderPath, cutDuration, autoCut }) => {
const fileName = path.basename(folderPath)
const audioPath = path.join(folderPath, 'accompaniment.wav')// 音频路径
const lrcPath = path.join(folderPath, `${fileName}.lrc`) // 歌词路径
const lyrics = getLyrics(lrcPath)
// 自动分段
if (autoCut) {
cutDuration = 0
for (let i = 1; i < lyrics.length; i++) {
const preSec = lyrics[i-1].seconds
const curSec = lyrics[i].seconds
// if (curSec < 60) break
if (cutDuration < curSec - preSec) {
cutDuration = preSec + (curSec - preSec) / 2
}
}
}
const duration = cutDuration || lyrics[lyrics.length - 1].seconds + 5
const creator = new FFCreator({
cacheDir: config.cacheDir,
output: path.join(config.creator.outputDir, `${fileName}${cutDuration || autoCut ? '-合拍版' : ''}.mp4`),
width,
height,
audio: path.resolve(audioPath),
fps: 25
})
const scene = new FFScene()
scene.setBgColor('#000000')
// 添加歌手
const singer = new FFText({
text: fileName.split('-')[0].trim(),
x: width / 2,
y: height / 2 - 60,
fontSize: 50,
color: '#000000'
})
singer.setBackgroundColor('#ffffff')
singer.alignCenter()
singer.addAnimate({
from: { alpha: 1 },
to: { alpha: 0 },
delay: 0.5,
time: 0.2
})
scene.addChild(singer)
// 添加歌曲名
const title = new FFText({
text: `《${fileName.split('-')[1].trim()}》`,
x: width / 2,
y: height / 2,
fontSize: 50,
color: '#000000'
})
title.setBackgroundColor('#ffffff')
title.alignCenter()
title.addAnimate({
from: { alpha: 1 },
to: { alpha: 0 },
delay: 0.5,
time: 0.2
})
scene.addChild(title)
if (cutDuration || autoCut) {
const title = new FFText({
text: `合拍版`,
x: width / 2,
y: height / 2 + 60,
fontSize: 50,
color: '#000000'
})
title.setBackgroundColor('#ffffff')
title.alignCenter()
title.addAnimate({
from: { alpha: 1 },
to: { alpha: 0 },
delay: 0.5,
time: 0.2
})
scene.addChild(title)
}
// 添加歌词
const rollupIndex = Math.floor(height / lineHeight / 2)
lyrics.forEach((item, index) => {
if (cutDuration && item.seconds > cutDuration) return // 合拍版精简歌词
let curY = index * lineHeight + paddingTop
const text = new FFText({
text: item.text,
x: width / 2,
y: curY,
fontSize: fontSize,
color: '#ffffff'
})
text.alignCenter()
for (let i = rollupIndex; i < lyrics.length; i++) {
const seconds = lyrics[i].seconds;
text.addAnimate({
from: { y: curY, alpha: 0.3 },
to: { y: curY -= lineHeight, alpha: 0.3 },
delay: seconds,
time: 0.5
})
}
text.addAnimate({
from: { scale: 1, alpha: 0.3 },
to: { scale: 1.3, alpha: 1 },
delay: item.seconds,
time: 0.5
})
if (index !== lyrics.length - 1) {
text.addAnimate({
from: { scale: 1.3, alpha: 1 },
to: { scale: 1, alpha: 0.3 },
delay: lyrics[index + 1].seconds,
time: 0.5
})
}
scene.addChild(text)
})
scene.setDuration(duration)
creator.addChild(scene)
creator.start()
creator.on('start', () => {
console.log(`FFCreator start`);
});
creator.on('error', e => {
console.log(`FFCreator error: ${e.error}}`);
});
creator.on('progress', e => {
console.log(colors.yellow(`FFCreator progress: ${(e.percent * 100) >> 0}%`));
});
return new Promise((resolve, reject) => {
creator.on('complete', e => {
console.log(
colors.magenta(`FFCreator completed: \n USEAGE: ${e.useage} \n PATH: ${e.output} `),
);
resolve(e.output)
});
})
}
module.exports = create