clipping improvements

This commit is contained in:
fishtank-dashboard
2026-03-19 18:48:31 -07:00
committed by GitHub
parent e398ad69c3
commit 4f3624dd33
2 changed files with 518 additions and 217 deletions

View File

@@ -4,6 +4,8 @@ const fs = require('fs');
const path = require('path');
const url = require('url');
const zlib = require('zlib');
const os = require('os');
const { execFile } = require('child_process');
const WebSocket = require('ws');
const PORT = 3000;
@@ -424,6 +426,61 @@ const server = http.createServer((req, res) => {
}); return;
}
if (parsed.pathname === '/strip-audio') {
if (req.method === 'POST') {
// Accept webm upload, strip audio with ffmpeg, return result
const chunks = [];
req.on('data', c => chunks.push(c));
req.on('end', () => {
const inputBuf = Buffer.concat(chunks);
const tmpIn = path.join(os.tmpdir(), 'ft_strip_in_' + Date.now() + '.webm');
const tmpOut = path.join(os.tmpdir(), 'ft_strip_out_' + Date.now() + '.webm');
fs.writeFile(tmpIn, inputBuf, err => {
if (err) { res.writeHead(500); res.end('Write error'); return; }
// -an = no audio, -c:v copy = copy video stream without re-encoding
// Find ffmpeg — try PATH first, then common Windows locations
const ffmpegCandidates = [
'ffmpeg',
'ffmpeg.exe',
'C:\\ffmpeg\\bin\\ffmpeg.exe',
'C:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe',
process.env.APPDATA + '\\ffmpeg\\bin\\ffmpeg.exe',
];
const ffmpegBin = ffmpegCandidates[0]; // will try with shell:true to use PATH
console.log('[STRIP] running ffmpeg:', tmpIn, '->', tmpOut);
execFile(ffmpegBin, ['-y', '-i', tmpIn, '-an', '-c:v', 'copy', tmpOut],
{ shell: true }, (err, stdout, stderr) => {
console.log('[STRIP] ffmpeg done. err:', err && err.code, 'stderr:', stderr && stderr.slice(0,300));
fs.unlink(tmpIn, () => {});
if (err) {
fs.unlink(tmpOut, () => {});
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('FFmpeg error: ' + (stderr || err.message || 'unknown'));
return;
}
fs.readFile(tmpOut, (err, data) => {
fs.unlink(tmpOut, () => {});
if (err) { res.writeHead(500); res.end('Read error'); return; }
res.writeHead(200, {
'Content-Type': 'video/webm',
'Content-Disposition': 'attachment; filename="noaudio.webm"',
'Content-Length': data.length,
'access-control-allow-origin': '*',
});
res.end(data);
});
});
});
}); return;
}
const file = path.join(__dirname, 'strip-audio.html');
fs.readFile(file, (err, data) => {
if (err) { res.writeHead(404); res.end('Tool not found'); return; }
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}); return;
}
// Token registration from dashboard
if (parsed.pathname === '/ws-token' && req.method === 'POST') {
let body = '';
@@ -480,7 +537,6 @@ const server = http.createServer((req, res) => {
const rawText = body.toString('utf8');
// Log first segment line to debug path format
const firstSeg = rawText.split('\n').find(l => l.trim() && !l.startsWith('#'));
console.log('[CAM] sub-playlist first line:', JSON.stringify(firstSeg));
const basePath = slug + '/' + parts.slice(1, -1).join('/') + '/';
const rewritten = rewriteM3u8(rawText, basePath);
res.writeHead(200, { 'content-type': 'application/vnd.apple.mpegurl', 'access-control-allow-origin': '*', 'cache-control': 'no-cache' });