-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn8n.html
More file actions
290 lines (259 loc) · 8.27 KB
/
n8n.html
File metadata and controls
290 lines (259 loc) · 8.27 KB
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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>n8n與p5.js視頻生成工作流</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 900px;
margin: 0 auto;
background-color: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.workflow {
display: flex;
flex-direction: column;
align-items: center;
}
.step {
width: 100%;
margin-bottom: 30px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
}
.step-header {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.step-number {
background-color: #4CAF50;
color: white;
width: 30px;
height: 30px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin-right: 10px;
font-weight: bold;
}
.step-title {
font-weight: bold;
font-size: 18px;
}
.code {
background-color: #f1f1f1;
padding: 15px;
border-radius: 5px;
font-family: monospace;
overflow-x: auto;
white-space: pre-wrap;
}
.arrow {
font-size: 24px;
margin: 10px 0;
}
.output-preview {
border: 1px dashed #ccc;
padding: 10px;
text-align: center;
}
.output-preview img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>n8n與p5.js視頻生成工作流</h1>
<div class="workflow">
<div class="step">
<div class="step-header">
<div class="step-number">1</div>
<div class="step-title">輸入p5.js代碼</div>
</div>
<p>通過Webhook節點或手動輸入p5.js代碼</p>
<div class="code">
// p5.js代碼範例
function setup() {
createCanvas(400, 400);
frameRate(30);
}
function draw() {
background(220);
// 畫一個隨時間移動的圓形
let t = frameCount * 0.05;
let x = width/2 + 100 * cos(t);
let y = height/2 + 100 * sin(t);
fill(255, 100, 100);
ellipse(x, y, 50, 50);
// 添加一些動態線條
stroke(0, 100, 200);
strokeWeight(2);
for(let i = 0; i < 10; i++) {
let lineX = width/2 + 150 * cos(t + i * 0.5);
let lineY = height/2 + 150 * sin(t + i * 0.5);
line(width/2, height/2, lineX, lineY);
}
}
</div>
</div>
<div class="arrow">⬇️</div>
<div class="step">
<div class="step-header">
<div class="step-number">2</div>
<div class="step-title">執行代碼節點</div>
</div>
<p>使用Code節點或Execute Command節點處理p5.js代碼</p>
<div class="code">
// n8n中的JavaScript代碼節點範例
const p5Code = items[0].json.p5Code;
// 生成一個HTML文件,嵌入p5.js和用戶代碼
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="https://unpkg.com/p5.js-screen-capture"></script>
<style>body { margin: 0; overflow: hidden; }</style>
</head>
<body>
<script>
${p5Code}
// 添加錄製功能
let capture;
let isRecording = false;
function keyPressed() {
if (key === 'r') {
if (!isRecording) {
capture = new p5ScreenCapture(this);
capture.start();
isRecording = true;
} else {
capture.stop();
isRecording = false;
}
}
}
</script>
</body>
</html>
`;
// 保存HTML文件到臨時位置
const fs = require('fs');
const path = '/tmp/p5_sketch.html';
fs.writeFileSync(path, htmlContent);
// 返回文件路徑供下一個節點使用
return [
{
json: {
htmlPath: path
}
}
];
</div>
</div>
<div class="arrow">⬇️</div>
<div class="step">
<div class="step-header">
<div class="step-number">3</div>
<div class="step-title">渲染視頻</div>
</div>
<p>使用Puppeteer或其他工具渲染p5.js代碼並生成視頻</p>
<div class="code">
// Execute Command節點中的命令
const htmlPath = $input.item.json.htmlPath;
const outputPath = '/tmp/output_video.mp4';
// 使用Puppeteer運行HTML並錄製視頻
const puppeteerScript = `
const puppeteer = require('puppeteer');
const path = require('path');
(async () => {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
// 設置視窗大小
await page.setViewport({ width: 400, height: 400 });
// 載入HTML文件
await page.goto('file://${htmlPath}', { waitUntil: 'networkidle0' });
// 等待頁面載入完成
await page.waitForTimeout(1000);
// 開始錄製
await page.keyboard.press('r');
// 錄製10秒
await page.waitForTimeout(10000);
// 停止錄製
await page.keyboard.press('r');
// 等待視頻生成
await page.waitForTimeout(2000);
// 關閉瀏覽器
await browser.close();
})();
`;
// 執行Puppeteer腳本
const puppeteerScriptPath = '/tmp/puppeteer_script.js';
fs.writeFileSync(puppeteerScriptPath, puppeteerScript);
execSync('node ' + puppeteerScriptPath);
// 使用FFmpeg處理視頻
execSync('ffmpeg -i /tmp/p5_capture.webm -c:v libx264 -preset fast -crf 22 -c:a aac -b:a 128k ' + outputPath);
return [
{
json: {
videoPath: outputPath
}
}
];
</div>
</div>
<div class="arrow">⬇️</div>
<div class="step">
<div class="step-header">
<div class="step-number">4</div>
<div class="step-title">輸出結果</div>
</div>
<p>將生成的視頻保存到本地或上傳到雲端存儲</p>
<div class="code">
// 使用HTTP Request節點上傳視頻
// 或使用Write Binary File節點保存到本地
// 返回視頻URL或文件路徑
return [
{
json: {
status: "success",
message: "Processing視頻生成完成",
videoUrl: "https://example.com/videos/generated_p5_video.mp4"
}
}
];
</div>
<div class="output-preview">
<h3>生成的視頻預覽</h3>
<img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAwIiBoZWlnaHQ9IjQwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxyZWN0IHdpZHRoPSI0MDAiIGhlaWdodD0iNDAwIiBmaWxsPSIjZGNkY2RjIiAvPgogICAgPGNpcmNsZSBjeD0iMjUwIiBjeT0iMjAwIiByPSI1MCIgZmlsbD0iI2ZmNjQ2NCIgLz4KICAgIDxsaW5lIHgxPSIyMDAiIHkxPSIyMDAiIHgyPSIzMDAiIHkyPSIxMDAiIHN0cm9rZT0iIzAwNjRjOCIgc3Ryb2tlLXdpZHRoPSIyIiAvPgogICAgPGxpbmUgeDE9IjIwMCIgeTE9IjIwMCIgeDI9IjMzMCIgeTI9IjE4MCIgc3Ryb2tlPSIjMDA2NGM4IiBzdHJva2Utd2lkdGg9IjIiIC8+CiAgICA8bGluZSB4MT0iMjAwIiB5MT0iMjAwIiB4Mj0iMzIwIiB5Mj0iMjUwIiBzdHJva2U9IiMwMDY0YzgiIHN0cm9rZS13aWR0aD0iMiIgLz4KICAgIDx0ZXh0IHg9IjIwMCIgeT0iMzgwIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBmb250LWZhbWlseT0iQXJpYWwiIGZvbnQtc2l6ZT0iMTYiPnAuanMg5Yqg6LyJ5YWJ55qE5YuV55Wl5ris6KaL77yI6KaB5piv5Y+v5Yqg6LyJ55qE5YWJ55qE77yJPC90ZXh0Pgo8L3N2Zz4=" alt="p5.js視頻預覽">
<p><i>注意:這是視頻的單幀示意圖,實際輸出是動態視頻</i></p>
</div>
</div>
</div>
</div>
</body>
</html>