Spaces:
Configuration error
Configuration error
AIGoose commited on
Commit ·
ef62cee
1
Parent(s): 2d3b83d
fix: pre-download remote video URLs before render; use chrome-headless-shell if available
Browse files- lib/renderer.js +72 -10
lib/renderer.js
CHANGED
|
@@ -1,6 +1,61 @@
|
|
| 1 |
import { execFile } from 'child_process';
|
| 2 |
import path from 'path';
|
| 3 |
import fs from 'fs';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
export async function renderVideo(compDir, outputFile, config, onProgress) {
|
| 6 |
const { format = '9:16', duration = 75 } = config;
|
|
@@ -12,8 +67,20 @@ export async function renderVideo(compDir, outputFile, config, onProgress) {
|
|
| 12 |
{ duration, width: w, height: h, fps: 30 }, null, 2
|
| 13 |
));
|
| 14 |
|
| 15 |
-
//
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
process.env.PUPPETEER_EXECUTABLE_PATH,
|
| 18 |
process.env.CHROME_PATH,
|
| 19 |
'/usr/bin/chromium',
|
|
@@ -23,21 +90,17 @@ export async function renderVideo(compDir, outputFile, config, onProgress) {
|
|
| 23 |
|
| 24 |
console.log(`[renderer] ${w}x${h} ${duration}s | chrome=${chromePath} | dir=${compDir}`);
|
| 25 |
|
| 26 |
-
// Correct CLI: npx hyperframes render <dir> -o <output.mp4>
|
| 27 |
-
// dir is the project directory containing index.html
|
| 28 |
-
// -w 1 = 1 worker (cpu-basic has limited RAM, avoid spawning multiple Chromes)
|
| 29 |
const args = [
|
| 30 |
'hyperframes', 'render',
|
| 31 |
-
compDir,
|
| 32 |
-
'-o', outputFile,
|
| 33 |
-
'-w', '1',
|
| 34 |
'--fps', '30',
|
| 35 |
'--quality', 'standard',
|
| 36 |
];
|
| 37 |
|
| 38 |
const env = {
|
| 39 |
...process.env,
|
| 40 |
-
// HyperFrames uses PRODUCER_HEADLESS_SHELL_PATH for the Chrome path
|
| 41 |
PRODUCER_HEADLESS_SHELL_PATH: chromePath,
|
| 42 |
PUPPETEER_EXECUTABLE_PATH: chromePath,
|
| 43 |
CHROME_PATH: chromePath,
|
|
@@ -66,7 +129,6 @@ export async function renderVideo(compDir, outputFile, config, onProgress) {
|
|
| 66 |
onProgress?.(1);
|
| 67 |
resolve(outputFile);
|
| 68 |
} else if (code === 0) {
|
| 69 |
-
// HyperFrames may output to renders/ subdir by default
|
| 70 |
const defaultOut = path.join(compDir, 'renders', 'index.mp4');
|
| 71 |
if (fs.existsSync(defaultOut)) {
|
| 72 |
fs.renameSync(defaultOut, outputFile);
|
|
|
|
| 1 |
import { execFile } from 'child_process';
|
| 2 |
import path from 'path';
|
| 3 |
import fs from 'fs';
|
| 4 |
+
import { createWriteStream } from 'fs';
|
| 5 |
+
import { pipeline } from 'stream/promises';
|
| 6 |
+
|
| 7 |
+
// Download a remote video URL to a local path (handles redirects)
|
| 8 |
+
async function downloadRemoteVideo(url, destPath) {
|
| 9 |
+
const https = await import('https');
|
| 10 |
+
const http = await import('http');
|
| 11 |
+
return new Promise((resolve, reject) => {
|
| 12 |
+
const get = url.startsWith('https') ? https.default.get : http.default.get;
|
| 13 |
+
get(url, { headers: { 'User-Agent': 'Mozilla/5.0' } }, (res) => {
|
| 14 |
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
| 15 |
+
return downloadRemoteVideo(res.headers.location, destPath).then(resolve).catch(reject);
|
| 16 |
+
}
|
| 17 |
+
if (res.statusCode !== 200) return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
|
| 18 |
+
const out = createWriteStream(destPath);
|
| 19 |
+
res.pipe(out);
|
| 20 |
+
out.on('finish', () => { out.close(); resolve(destPath); });
|
| 21 |
+
out.on('error', reject);
|
| 22 |
+
}).on('error', reject);
|
| 23 |
+
});
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// Scan index.html for remote video src="" and download them to assets/
|
| 27 |
+
async function prefetchRemoteVideos(compDir) {
|
| 28 |
+
const htmlPath = path.join(compDir, 'index.html');
|
| 29 |
+
if (!fs.existsSync(htmlPath)) return;
|
| 30 |
+
let html = fs.readFileSync(htmlPath, 'utf8');
|
| 31 |
+
|
| 32 |
+
const srcPattern = /src=["'](https?:\/\/[^"']+\.(?:mov|mp4|webm|MOV|MP4)[^"']*)["']/g;
|
| 33 |
+
const matches = [...html.matchAll(srcPattern)];
|
| 34 |
+
if (!matches.length) return;
|
| 35 |
+
|
| 36 |
+
const assetsDir = path.join(compDir, 'assets');
|
| 37 |
+
fs.mkdirSync(assetsDir, { recursive: true });
|
| 38 |
+
|
| 39 |
+
for (const match of matches) {
|
| 40 |
+
const remoteUrl = match[1];
|
| 41 |
+
const filename = 'video-' + Buffer.from(remoteUrl).toString('base64').slice(0,12).replace(/[^a-z0-9]/gi,'') + '.mov';
|
| 42 |
+
const localPath = path.join(assetsDir, filename);
|
| 43 |
+
const relPath = `./assets/${filename}`;
|
| 44 |
+
|
| 45 |
+
if (!fs.existsSync(localPath)) {
|
| 46 |
+
console.log(`[renderer] Downloading remote video → ${filename}`);
|
| 47 |
+
await downloadRemoteVideo(remoteUrl, localPath);
|
| 48 |
+
console.log(`[renderer] Downloaded ${(fs.statSync(localPath).size / 1024 / 1024).toFixed(1)} MB`);
|
| 49 |
+
} else {
|
| 50 |
+
console.log(`[renderer] Using cached ${filename}`);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
html = html.split(match[0]).join(match[0].replace(remoteUrl, relPath));
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
fs.writeFileSync(htmlPath, html);
|
| 57 |
+
console.log(`[renderer] Patched ${matches.length} remote video src(s) to local paths`);
|
| 58 |
+
}
|
| 59 |
|
| 60 |
export async function renderVideo(compDir, outputFile, config, onProgress) {
|
| 61 |
const { format = '9:16', duration = 75 } = config;
|
|
|
|
| 67 |
{ duration, width: w, height: h, fps: 30 }, null, 2
|
| 68 |
));
|
| 69 |
|
| 70 |
+
// Pre-fetch any remote video URLs in index.html → local assets/
|
| 71 |
+
await prefetchRemoteVideos(compDir);
|
| 72 |
+
onProgress?.(0.1);
|
| 73 |
+
|
| 74 |
+
// Find chrome-headless-shell first, fall back to chromium
|
| 75 |
+
const shellGlob = '/app/.chrome/chrome-headless-shell/linux-*/chrome-headless-shell/chrome-headless-shell';
|
| 76 |
+
let headlessShell = '';
|
| 77 |
+
try {
|
| 78 |
+
const { execSync } = await import('child_process');
|
| 79 |
+
headlessShell = execSync(`ls ${shellGlob} 2>/dev/null | head -1`).toString().trim();
|
| 80 |
+
} catch {}
|
| 81 |
+
|
| 82 |
+
const chromePath = headlessShell || [
|
| 83 |
+
process.env.PRODUCER_HEADLESS_SHELL_PATH,
|
| 84 |
process.env.PUPPETEER_EXECUTABLE_PATH,
|
| 85 |
process.env.CHROME_PATH,
|
| 86 |
'/usr/bin/chromium',
|
|
|
|
| 90 |
|
| 91 |
console.log(`[renderer] ${w}x${h} ${duration}s | chrome=${chromePath} | dir=${compDir}`);
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
const args = [
|
| 94 |
'hyperframes', 'render',
|
| 95 |
+
compDir,
|
| 96 |
+
'-o', outputFile,
|
| 97 |
+
'-w', '1',
|
| 98 |
'--fps', '30',
|
| 99 |
'--quality', 'standard',
|
| 100 |
];
|
| 101 |
|
| 102 |
const env = {
|
| 103 |
...process.env,
|
|
|
|
| 104 |
PRODUCER_HEADLESS_SHELL_PATH: chromePath,
|
| 105 |
PUPPETEER_EXECUTABLE_PATH: chromePath,
|
| 106 |
CHROME_PATH: chromePath,
|
|
|
|
| 129 |
onProgress?.(1);
|
| 130 |
resolve(outputFile);
|
| 131 |
} else if (code === 0) {
|
|
|
|
| 132 |
const defaultOut = path.join(compDir, 'renders', 'index.mp4');
|
| 133 |
if (fs.existsSync(defaultOut)) {
|
| 134 |
fs.renameSync(defaultOut, outputFile);
|