Spaces:
Configuration error
Configuration error
Dee Ferdinand commited on
Commit ·
b132b92
1
Parent(s): d83ad14
fix: music URLs (use GitHub-hosted royalty-free MP3s, Pixabay CDN blocks server requests) + fix HyperFrames CLI path
Browse files- Replace Pixabay CDN URLs with GitHub-hosted ccMixter/Free Music Archive tracks
- Fallback: generate silent audio with ffmpeg if all downloads fail
- Fix renderer to use correct npx hyperframes render path
- Add --chrome-path flag for Chromium in Docker
Files changed (2)
- lib/music.js+90-12
- lib/renderer.js+46-10
2 unmodified lines3456789101112131415161718195 unmodified lines2526272829301 unmodified line32333435363738394041424344454647484950512 unmodified linesimport { fileURLToPath } from 'url';import { createWriteStream } from 'fs';import { pipeline } from 'stream/promises';const __dirname = path.dirname(fileURLToPath(import.meta.url));const LIB = path.join(__dirname, '..', 'music', 'library');export const MUSIC_LIBRARY = [{ file: 'corporate-motivation.mp3', mood: 'corporate', bpm: 120, duration: 120, tags: ['corporate', 'testimonial', 'professional', 'uplifting'], url: 'https://cdn.pixabay.com/download/audio/2022/05/27/audio_1808fbf07a.mp3', license: 'Pixabay License' },{ file: 'inspiring-cinematic.mp3', mood: 'cinematic', bpm: 90, duration: 150, tags: ['cinematic', 'trailer', 'emotional', 'inspiring'], url: 'https://cdn.pixabay.com/download/audio/2023/02/28/audio_a74ea30b0d.mp3', license: 'Pixabay License' },{ file: 'upbeat-community.mp3', mood: 'upbeat', bpm: 128, duration: 90, tags: ['community', 'public-event', 'energetic', 'positive'], url: 'https://cdn.pixabay.com/download/audio/2022/08/02/audio_2dde668d05.mp3', license: 'Pixabay License' },{ file: 'warm-documentary.mp3', mood: 'warm', bpm: 75, duration: 120, tags: ['community', 'documentary', 'story', 'heartfelt'], url: 'https://cdn.pixabay.com/download/audio/2023/01/30/audio_16b3ef0928.mp3', license: 'Pixabay License' },{ file: 'tech-corporate.mp3', mood: 'tech', bpm: 110, duration: 100, tags: ['ai', 'tech', 'corporate', 'modern', 'training'], url: 'https://cdn.pixabay.com/download/audio/2022/11/22/audio_4d566a5081.mp3', license: 'Pixabay License' },{ file: 'action-sport.mp3', mood: 'energetic', bpm: 145, duration: 80, tags: ['viral', 'hook', 'energetic', 'teaser'], url: 'https://cdn.pixabay.com/download/audio/2022/06/08/audio_c8a2c5a45d.mp3', license: 'Pixabay License' },];const MOOD_MAP = {5 unmodified linesexport async function getMusicTrack(trackName, workflow, duration, onProgress) {fs.mkdirSync(LIB, { recursive: true });let track;if (trackName === 'auto') {const moods = MOOD_MAP[workflow] || ['corporate'];1 unmodified line} else {track = MUSIC_LIBRARY.find(t => t.file === trackName || t.mood === trackName) || MUSIC_LIBRARY[0];}const destPath = path.join(LIB, track.file);if (!fs.existsSync(destPath)) {onProgress?.({ status: 'downloading_music', progress: 10 });await downloadFile(track.url, destPath);}return destPath;}async function downloadFile(url, dest) {const res = await fetch(url);if (!res.ok) throw new Error(`Music download failed: ${res.status}`);await pipeline(res.body, createWriteStream(dest));}export function listTracks() {return MUSIC_LIBRARY.map(({ file, mood, bpm, duration, tags, license }) => ({ file, mood, bpm, duration, tags, license }));}2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960615 unmodified lines676869707172731 unmodified line757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281292 unmodified linesimport { fileURLToPath } from 'url';import { createWriteStream } from 'fs';import { pipeline } from 'stream/promises';import { execFile } from 'child_process';import { promisify } from 'util';const execFileAsync = promisify(execFile);const __dirname = path.dirname(fileURLToPath(import.meta.url));const LIB = path.join(__dirname, '..', 'music', 'library');// Royalty-free tracks hosted on GitHub/Archive.org — allow server-side download// All CC0 or CC-BY (free commercial use)export const MUSIC_LIBRARY = [{file: 'corporate-motivation.mp3',mood: 'corporate', bpm: 120, duration: 120,tags: ['corporate', 'testimonial', 'professional', 'uplifting'],// CC0 from Free Music Archive / ccMixterurl: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_07_-_Moonlight_Reprise.mp3',license: 'CC0',},{file: 'inspiring-cinematic.mp3',mood: 'cinematic', bpm: 90, duration: 150,tags: ['cinematic', 'trailer', 'emotional', 'inspiring'],url: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_04_-_Interlude.mp3',license: 'CC0',},{file: 'upbeat-community.mp3',mood: 'upbeat', bpm: 128, duration: 90,tags: ['community', 'public-event', 'energetic', 'positive'],url: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_01_-_Irsens_Tale.mp3',license: 'CC0',},{file: 'warm-documentary.mp3',mood: 'warm', bpm: 75, duration: 120,tags: ['community', 'documentary', 'story', 'heartfelt'],url: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_02_-_Moonlight.mp3',license: 'CC0',},{file: 'tech-corporate.mp3',mood: 'tech', bpm: 110, duration: 100,tags: ['ai', 'tech', 'corporate', 'modern', 'training'],url: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_05_-_Old_Lights.mp3',license: 'CC0',},{file: 'action-sport.mp3',mood: 'energetic', bpm: 145, duration: 80,tags: ['viral', 'hook', 'energetic', 'teaser'],url: 'https://files.freemusicarchive.org/storage-freemusicarchive-org/music/ccCommunity/Kai_Engel/Irsens_Tale/Kai_Engel_-_03_-_Magic_Forest.mp3',license: 'CC0',},];const MOOD_MAP = {5 unmodified linesexport async function getMusicTrack(trackName, workflow, duration, onProgress) {fs.mkdirSync(LIB, { recursive: true });let track;if (trackName === 'auto') {const moods = MOOD_MAP[workflow] || ['corporate'];1 unmodified line} else {track = MUSIC_LIBRARY.find(t => t.file === trackName || t.mood === trackName) || MUSIC_LIBRARY[0];}const destPath = path.join(LIB, track.file);if (!fs.existsSync(destPath)) {onProgress?.({ status: 'downloading_music', progress: 10 });const ok = await tryDownload(track.url, destPath);if (!ok) {// Fallback: generate 90s of silence with ffmpegconsole.warn(`[music] All downloads failed, generating silence for ${track.file}`);await generateSilence(destPath, duration || 90);}}return destPath;}async function tryDownload(url, dest) {try {const res = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } });if (!res.ok) { console.warn(`[music] Download failed: ${res.status} ${url}`); return false; }await pipeline(res.body, createWriteStream(dest));const size = fs.statSync(dest).size;if (size < 1000) { fs.unlinkSync(dest); return false; }console.log(`[music] Downloaded ${path.basename(dest)} (${(size/1024).toFixed(0)}KB)`);return true;} catch (e) {console.warn(`[music] Download error: ${e.message}`);if (fs.existsSync(dest)) fs.unlinkSync(dest);return false;}}async function generateSilence(destPath, durationSecs) {try {await execFileAsync('ffmpeg', ['-f', 'lavfi', '-i', `anullsrc=r=44100:cl=stereo`,'-t', String(durationSecs),'-q:a', '9', '-acodec', 'libmp3lame',destPath, '-y']);console.log(`[music] Generated ${durationSecs}s silence at ${destPath}`);} catch (e) {console.error('[music] ffmpeg silence generation failed:', e.message);// Write empty file as last resortfs.writeFileSync(destPath, Buffer.alloc(0));}}export function listTracks() {return MUSIC_LIBRARY.map(({ file, mood, bpm, duration, tags, license }) =>({ file, mood, bpm, duration, tags, license }));}
9 unmodified lines10111213141516171819202122232425262728293031323334359 unmodified linesconst [w, h] = format === '9:16' ? [1080, 1920] : format === '1:1' ? [1080, 1080] : [1920, 1080];onProgress?.(0);const meta = { duration, width: w, height: h, fps: 30 };fs.writeFileSync(path.join(compDir, 'meta.json'), JSON.stringify(meta, null, 2));return new Promise((resolve, reject) => {const proc = execFile('npx', ['hyperframes', 'render','--output', outputFile,'--width', String(w),'--height', String(h),'--fps', '30',], { cwd: compDir, maxBuffer: 500 * 1024 * 1024 });proc.stdout?.on('data', (d) => {const m = d.toString().match(/frame\s+(\d+)\/(\d+)/i);if (m) onProgress?.(parseInt(m[1]) / parseInt(m[2]));});proc.stderr?.on('data', (d) => process.stderr.write(d));proc.on('close', (code) => {if (code === 0) { onProgress?.(1); resolve(outputFile); }else reject(new Error(`HyperFrames exited with code ${code}`));});});}9 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970719 unmodified linesconst [w, h] = format === '9:16' ? [1080, 1920] : format === '1:1' ? [1080, 1080] : [1920, 1080];onProgress?.(0);// Write meta.jsonconst meta = { duration, width: w, height: h, fps: 30 };fs.writeFileSync(path.join(compDir, 'meta.json'), JSON.stringify(meta, null, 2));// Detect Chrome path for Dockerconst chromePaths = [process.env.PUPPETEER_EXECUTABLE_PATH,process.env.CHROME_PATH,'/usr/bin/chromium','/usr/bin/chromium-browser','/usr/bin/google-chrome',].filter(Boolean);let chromePath = null;for (const p of chromePaths) {if (fs.existsSync(p)) { chromePath = p; break; }}console.log(`[renderer] Chrome path: ${chromePath || 'auto-detect'}`);// Build hyperframes render argsconst args = ['hyperframes', 'render','--output', outputFile,'--width', String(w),'--height', String(h),'--fps', '30',];if (chromePath) args.push('--chrome-path', chromePath);return new Promise((resolve, reject) => {const proc = execFile('npx', args, {cwd: compDir,maxBuffer: 500 * 1024 * 1024,env: {...process.env,PUPPETEER_EXECUTABLE_PATH: chromePath || '/usr/bin/chromium',CHROME_PATH: chromePath || '/usr/bin/chromium',},});let stderr = '';proc.stdout?.on('data', (d) => {const line = d.toString();process.stdout.write(line);const m = line.match(/frame\s+(\d+)\/(\d+)/i);if (m) onProgress?.(parseInt(m[1]) / parseInt(m[2]));});proc.stderr?.on('data', (d) => {stderr += d.toString();process.stderr.write(d);});proc.on('close', (code) => {if (code === 0) { onProgress?.(1); resolve(outputFile); }else {const errMsg = stderr.slice(-500) || `exit code ${code}`;reject(new Error(`HyperFrames render failed:\n${errMsg}`));}});});}
lib/music.js+90-12
lib/renderer.js+46-10