-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (120 loc) · 5.26 KB
/
index.js
File metadata and controls
145 lines (120 loc) · 5.26 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
const dotenv = require('dotenv');
dotenv.config();
const { basic_setup } = require("./lib/express");
const { verify_url, prepare_file, stream_response } = require("./lib/utils");
const { capture_screenshot, capture_pdf, capture_recording } = require("./lib/capture");
const { video_optimize, video_looping, video_mp3, video_webm} = require("./lib/ffmpeg");
const app = basic_setup();
// -------------------------------------------------------------------------------
// Puppeteer Capture Endpoints
// -------------------------------------------------------------------------------
// Webpage Screenshot
app.post('/screenshot', async (req, res) => {
const { url, proxy, browser, config, upload } = req.body;
try {
const file = prepare_file( upload, { prefix: 'screenshot-', ext: 'png' });
const blob = await capture_screenshot( verify_url(url, proxy), { browser, options: config } );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/screenshot:', err);
res.status(500).json({ error: err.message });
}
});
// Webpage to PDF
app.post('/pdf', async (req, res) => {
const { url, proxy, browser, config, upload } = req.body;
try {
const params = browser ?? { viewport: { width: 1080, height: 1920 } };
const file = prepare_file( upload, { prefix: 'webpage-', ext: 'pdf' });
const blob = await capture_pdf( verify_url(url, proxy), { browser: params, options: config } );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/pdf:', err);
res.status(500).json({ error: err.message });
}
});
// Webpage Screen Recording
app.post('/recording', async (req, res) => {
const { url, proxy, browser, config, upload } = req.body;
if (proxy) { return res.status(400).json({ error: 'Can not proxy recording requests' }); }
try {
const file = prepare_file( upload, { prefix: 'webclip-', ext: 'webm' });
const blob = await capture_recording( verify_url(url), { browser, options: config } );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/recording:', err);
res.status(500).json({ error: err.message });
}
});
// -------------------------------------------------------------------------------
// FFMPEG Video Processing Endpoints
// -------------------------------------------------------------------------------
// Optimize video for web playback
app.post("/optimize-video", async (req, res) => {
const { url, upload } = req.body;
try {
const file = prepare_file( upload, { ext: 'mp4' } );
const blob = await video_optimize( verify_url(url) );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/optimize-video:', err);
res.status(500).json({ error: err.message });
}
});
// Optimize video for silent web playback
app.post("/optimize-silent-video", async (req, res) => {
const { url, upload } = req.body;
try {
const file = prepare_file( upload, { ext: 'mp4' } );
const blob = await video_optimize( verify_url(url), { audio: false } );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/optimize-silent-video:', err);
res.status(500).json({ error: err.message });
}
});
// Optimize video for background (silent, looping) web playback
app.post("/optimize-looping-video", async (req, res) => {
const { url, upload } = req.body;
try {
const file = prepare_file( upload, { ext: 'mp4' } );
const blob = await video_looping( verify_url(url) );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/optimize-looping-video:', err);
res.status(500).json({ error: err.message });
}
});
// Convert video to webm format
app.post("/video-to-webm", async (req, res) => {
const { url, upload } = req.body;
try {
const file = prepare_file( upload, { ext: 'webm' } );
const blob = await video_webm( verify_url(url) );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/video-to-webm:', err);
res.status(500).json({ error: err.message });
}
});
// Extract audio from video (video to mp3)
app.post("/video-to-mp3", async (req, res) => {
const { url, upload } = req.body;
try {
const file = prepare_file( upload, { ext: 'mp3' } );
const blob = await video_mp3( verify_url(url) );
return stream_response( res, { file, blob } );
} catch (err) {
console.error('/video-to-mp3:', err);
res.status(500).json({ error: err.message });
}
});
// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------
// Start the Express.js server
// -------------------------------------------------------------------------------
const server = app.listen( process.env.SERVER_PORT ?? 3000, () => {
console.log(`Server is running on http://localhost:${server.address().port}.`);
});
server.timeout = process.env.SERVER_TIMEOUT ?? 10 * 60 * 1000; // 5 min
server.headersTimeout = ( process.env.SERVER_TIMEOUT ?? 10 * 60 * 1000 ) + 1000; // 5 min + buffer