修复NVIDIA编码错误
This commit is contained in:
@@ -75,6 +75,7 @@
|
|||||||
<div id="now-playing" class="now-playing hidden">
|
<div id="now-playing" class="now-playing hidden">
|
||||||
<h3>Now Playing</h3>
|
<h3>Now Playing</h3>
|
||||||
<p id="current-video-title">video.mp4</p>
|
<p id="current-video-title">video.mp4</p>
|
||||||
|
<button id="transcode-btn" class="play-btn hidden">Start Transcode</button>
|
||||||
<button id="play-btn" class="play-btn hidden">Play</button>
|
<button id="play-btn" class="play-btn hidden">Play</button>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const videoPlayer = document.getElementById('video-player');
|
const videoPlayer = document.getElementById('video-player');
|
||||||
const nowPlaying = document.getElementById('now-playing');
|
const nowPlaying = document.getElementById('now-playing');
|
||||||
const currentVideoTitle = document.getElementById('current-video-title');
|
const currentVideoTitle = document.getElementById('current-video-title');
|
||||||
|
const transcodeBtn = document.getElementById('transcode-btn');
|
||||||
const playBtn = document.getElementById('play-btn');
|
const playBtn = document.getElementById('play-btn');
|
||||||
const progressInfo = document.getElementById('progress-info');
|
const progressInfo = document.getElementById('progress-info');
|
||||||
const progressText = document.getElementById('progress-text');
|
const progressText = document.getElementById('progress-text');
|
||||||
const progressFill = document.getElementById('progress-fill');
|
const progressFill = document.getElementById('progress-fill');
|
||||||
|
|
||||||
let currentPollInterval = null;
|
let currentPollInterval = null;
|
||||||
|
let selectedKey = null;
|
||||||
let ws = null;
|
let ws = null;
|
||||||
let wsConnected = false;
|
let wsConnected = false;
|
||||||
let subscribedKey = null;
|
let subscribedKey = null;
|
||||||
@@ -87,6 +89,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
progressFill.style.width = '0%';
|
progressFill.style.width = '0%';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (transcodeBtn) {
|
||||||
|
transcodeBtn.addEventListener('click', () => {
|
||||||
|
startTranscode();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (playBtn) {
|
if (playBtn) {
|
||||||
playBtn.addEventListener('click', () => {
|
playBtn.addEventListener('click', () => {
|
||||||
videoPlayer.play().catch(e => console.log('Play blocked:', e));
|
videoPlayer.play().catch(e => console.log('Play blocked:', e));
|
||||||
@@ -207,7 +215,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Handle video selection and trigger transcode
|
// Handle video selection and trigger transcode
|
||||||
const selectVideo = async (key, listItemNode) => {
|
const selectVideo = (key, listItemNode) => {
|
||||||
// Update UI
|
// Update UI
|
||||||
document.querySelectorAll('.video-item').forEach(n => n.classList.remove('active'));
|
document.querySelectorAll('.video-item').forEach(n => n.classList.remove('active'));
|
||||||
listItemNode.classList.add('active');
|
listItemNode.classList.add('active');
|
||||||
@@ -217,21 +225,33 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
playerOverlay.classList.add('hidden');
|
playerOverlay.classList.add('hidden');
|
||||||
videoPlayer.classList.add('hidden');
|
videoPlayer.classList.add('hidden');
|
||||||
videoPlayer.pause();
|
videoPlayer.pause();
|
||||||
|
if (transcodeBtn) {
|
||||||
|
transcodeBtn.disabled = false;
|
||||||
|
transcodeBtn.textContent = 'Start Transcode';
|
||||||
|
transcodeBtn.classList.remove('hidden');
|
||||||
|
}
|
||||||
if (playBtn) {
|
if (playBtn) {
|
||||||
playBtn.disabled = true;
|
playBtn.classList.add('hidden');
|
||||||
playBtn.textContent = '等待转码完成';
|
|
||||||
playBtn.classList.remove('hidden');
|
|
||||||
}
|
}
|
||||||
resetProgress();
|
resetProgress();
|
||||||
|
|
||||||
|
selectedKey = key;
|
||||||
currentVideoKey = key;
|
currentVideoKey = key;
|
||||||
subscribeToKey(key);
|
subscribeToKey(key);
|
||||||
|
|
||||||
nowPlaying.classList.remove('hidden');
|
nowPlaying.classList.remove('hidden');
|
||||||
currentVideoTitle.textContent = key.split('/').pop();
|
currentVideoTitle.textContent = key.split('/').pop();
|
||||||
|
};
|
||||||
|
|
||||||
|
const startTranscode = async () => {
|
||||||
|
if (!selectedKey) return;
|
||||||
|
if (transcodeBtn) {
|
||||||
|
transcodeBtn.disabled = true;
|
||||||
|
transcodeBtn.textContent = 'Starting...';
|
||||||
|
}
|
||||||
|
stopPolling();
|
||||||
transcodingOverlay.classList.remove('hidden');
|
transcodingOverlay.classList.remove('hidden');
|
||||||
setProgress({ status: 'Starting transcoding...', percent: 0, details: 'Starting transcoding...' });
|
setProgress({ status: 'Starting download...', percent: 0, details: 'Starting download...' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const codec = codecSelect?.value || 'h264';
|
const codec = codecSelect?.value || 'h264';
|
||||||
@@ -239,14 +259,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const res = await fetch('/api/transcode', {
|
const res = await fetch('/api/transcode', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ key, codec, encoder })
|
body: JSON.stringify({ key: selectedKey, codec, encoder })
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data.error) throw new Error(data.error);
|
if (data.error) throw new Error(data.error);
|
||||||
|
|
||||||
if (!wsConnected) {
|
if (!wsConnected) {
|
||||||
pollForMp4Ready(key, data.mp4Url);
|
pollForMp4Ready(selectedKey, data.mp4Url);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
206
server.js
206
server.js
@@ -35,12 +35,14 @@ const BUCKET_NAME = process.env.S3_BUCKET_NAME;
|
|||||||
const progressMap = {};
|
const progressMap = {};
|
||||||
const wsSubscriptions = new Map();
|
const wsSubscriptions = new Map();
|
||||||
|
|
||||||
const addWsClient = (key, ws) => {
|
const getProgressKey = (key) => key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_')).join('/');
|
||||||
if (!wsSubscriptions.has(key)) {
|
|
||||||
wsSubscriptions.set(key, new Set());
|
const addWsClient = (progressKey, ws) => {
|
||||||
|
if (!wsSubscriptions.has(progressKey)) {
|
||||||
|
wsSubscriptions.set(progressKey, new Set());
|
||||||
}
|
}
|
||||||
wsSubscriptions.get(key).add(ws);
|
wsSubscriptions.get(progressKey).add(ws);
|
||||||
ws.currentKey = key;
|
ws.currentKey = progressKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeWsClient = (ws) => {
|
const removeWsClient = (ws) => {
|
||||||
@@ -63,6 +65,23 @@ const broadcastWs = (key, payload) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const createFfmpegOptions = (encoderName) => {
|
||||||
|
const options = ['-preset fast'];
|
||||||
|
if (encoderName === 'libx264' || encoderName === 'libx265') {
|
||||||
|
options.push('-crf', '23');
|
||||||
|
} else if (/_nvenc$/.test(encoderName)) {
|
||||||
|
options.push('-rc:v', 'vbr_hq', '-cq', '19');
|
||||||
|
} else if (/_qsv$/.test(encoderName)) {
|
||||||
|
options.push('-global_quality', '23');
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
};
|
||||||
|
|
||||||
|
const shouldRetryWithSoftware = (message) => {
|
||||||
|
if (!message) return false;
|
||||||
|
return /Cannot load libcuda\.so\.1|Could not open encoder before EOF|Error while opening encoder|Operation not permitted|Invalid argument/i.test(message);
|
||||||
|
};
|
||||||
|
|
||||||
const wss = new WebSocket.Server({ server });
|
const wss = new WebSocket.Server({ server });
|
||||||
|
|
||||||
wss.on('connection', (ws) => {
|
wss.on('connection', (ws) => {
|
||||||
@@ -70,12 +89,13 @@ wss.on('connection', (ws) => {
|
|||||||
try {
|
try {
|
||||||
const message = JSON.parse(raw.toString());
|
const message = JSON.parse(raw.toString());
|
||||||
if (message.type === 'subscribe' && typeof message.key === 'string') {
|
if (message.type === 'subscribe' && typeof message.key === 'string') {
|
||||||
if (ws.currentKey && ws.currentKey !== message.key) {
|
const progressKey = getProgressKey(message.key);
|
||||||
|
if (ws.currentKey && ws.currentKey !== progressKey) {
|
||||||
removeWsClient(ws);
|
removeWsClient(ws);
|
||||||
}
|
}
|
||||||
addWsClient(message.key, ws);
|
addWsClient(progressKey, ws);
|
||||||
|
|
||||||
const currentProgress = progressMap[message.key];
|
const currentProgress = progressMap[progressKey];
|
||||||
if (currentProgress) {
|
if (currentProgress) {
|
||||||
ws.send(JSON.stringify({ type: 'progress', key: message.key, progress: currentProgress }));
|
ws.send(JSON.stringify({ type: 'progress', key: message.key, progress: currentProgress }));
|
||||||
if (currentProgress.status === 'finished' && currentProgress.mp4Url) {
|
if (currentProgress.status === 'finished' && currentProgress.mp4Url) {
|
||||||
@@ -174,16 +194,49 @@ app.post('/api/transcode', async (req, res) => {
|
|||||||
|
|
||||||
const response = await s3Client.send(command);
|
const response = await s3Client.send(command);
|
||||||
const s3Stream = response.Body;
|
const s3Stream = response.Body;
|
||||||
|
const totalBytes = response.ContentLength || 0;
|
||||||
|
let downloadedBytes = 0;
|
||||||
const tmpInputPath = path.join(os.tmpdir(), `s3-input-${Date.now()}-${Math.random().toString(16).slice(2)}.tmp`);
|
const tmpInputPath = path.join(os.tmpdir(), `s3-input-${Date.now()}-${Math.random().toString(16).slice(2)}.tmp`);
|
||||||
|
|
||||||
|
const broadcastDownloadProgress = () => {
|
||||||
|
const percent = totalBytes ? Math.min(100, Math.round(downloadedBytes / totalBytes * 100)) : 0;
|
||||||
|
const downloadState = {
|
||||||
|
status: 'downloading',
|
||||||
|
percent,
|
||||||
|
downloadedBytes,
|
||||||
|
totalBytes,
|
||||||
|
details: totalBytes ? `Downloading ${percent}%` : 'Downloading...',
|
||||||
|
mp4Url
|
||||||
|
};
|
||||||
|
progressMap[progressKey] = downloadState;
|
||||||
|
broadcastWs(progressKey, { type: 'progress', key, progress: downloadState });
|
||||||
|
};
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
const writeStream = fs.createWriteStream(tmpInputPath);
|
const writeStream = fs.createWriteStream(tmpInputPath);
|
||||||
s3Stream.pipe(writeStream);
|
s3Stream.on('data', (chunk) => {
|
||||||
s3Stream.on('error', reject);
|
downloadedBytes += chunk.length;
|
||||||
|
broadcastDownloadProgress();
|
||||||
|
});
|
||||||
|
s3Stream.on('error', (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
writeStream.on('error', reject);
|
writeStream.on('error', reject);
|
||||||
writeStream.on('finish', resolve);
|
writeStream.on('finish', resolve);
|
||||||
|
s3Stream.pipe(writeStream);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
broadcastDownloadProgress();
|
||||||
|
progressMap[progressKey] = {
|
||||||
|
status: 'downloaded',
|
||||||
|
percent: 100,
|
||||||
|
downloadedBytes,
|
||||||
|
totalBytes,
|
||||||
|
details: 'Download complete, starting transcode...',
|
||||||
|
mp4Url
|
||||||
|
};
|
||||||
|
broadcastWs(progressKey, { type: 'progress', key, progress: progressMap[progressKey] });
|
||||||
|
|
||||||
// Triggers fluent-ffmpeg to transcode to MP4
|
// Triggers fluent-ffmpeg to transcode to MP4
|
||||||
console.log(`Starting transcoding for ${key} with codec ${videoCodec}`);
|
console.log(`Starting transcoding for ${key} with codec ${videoCodec}`);
|
||||||
|
|
||||||
@@ -191,60 +244,85 @@ app.post('/api/transcode', async (req, res) => {
|
|||||||
fs.unlink(tmpInputPath, () => {});
|
fs.unlink(tmpInputPath, () => {});
|
||||||
};
|
};
|
||||||
|
|
||||||
ffmpeg(tmpInputPath)
|
let attemptedSoftwareFallback = false;
|
||||||
.videoCodec(videoCodec)
|
const startFfmpeg = (encoderName) => {
|
||||||
.audioCodec('aac')
|
console.log(`Starting ffmpeg with encoder ${encoderName} for ${key}`);
|
||||||
.outputOptions([
|
ffmpeg(tmpInputPath)
|
||||||
'-preset fast',
|
.videoCodec(encoderName)
|
||||||
'-crf 23'
|
.audioCodec('aac')
|
||||||
])
|
.outputOptions(createFfmpegOptions(encoderName))
|
||||||
.format('mp4')
|
.format('mp4')
|
||||||
.output(mp4Path)
|
.output(mp4Path)
|
||||||
.on('progress', (progress) => {
|
.on('progress', (progress) => {
|
||||||
const progressState = {
|
const progressState = {
|
||||||
status: 'transcoding',
|
status: 'transcoding',
|
||||||
percent: Math.min(Math.max(Math.round(progress.percent || 0), 0), 100),
|
percent: Math.min(Math.max(Math.round(progress.percent || 0), 0), 100),
|
||||||
frame: progress.frames || null,
|
frame: progress.frames || null,
|
||||||
fps: progress.currentFps || null,
|
fps: progress.currentFps || null,
|
||||||
bitrate: progress.currentKbps || null,
|
bitrate: progress.currentKbps || null,
|
||||||
timemark: progress.timemark || null,
|
timemark: progress.timemark || null,
|
||||||
details: `Transcoding... ${Math.min(Math.max(Math.round(progress.percent || 0), 0), 100)}%`,
|
details: `Transcoding... ${Math.min(Math.max(Math.round(progress.percent || 0), 0), 100)}%`,
|
||||||
mp4Url
|
mp4Url
|
||||||
};
|
};
|
||||||
progressMap[progressKey] = progressState;
|
progressMap[progressKey] = progressState;
|
||||||
broadcastWs(progressKey, { type: 'progress', key: progressKey, progress: progressState });
|
broadcastWs(progressKey, { type: 'progress', key, progress: progressState });
|
||||||
})
|
})
|
||||||
.on('stderr', (stderrLine) => {
|
.on('stderr', (stderrLine) => {
|
||||||
console.log(`ffmpeg stderr: ${stderrLine}`);
|
console.log(`ffmpeg stderr: ${stderrLine}`);
|
||||||
})
|
})
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
cleanupTmpInput();
|
cleanupTmpInput();
|
||||||
console.log(`Finished transcoding ${key} to MP4`);
|
console.log(`Finished transcoding ${key} to MP4`);
|
||||||
let progressState;
|
let progressState;
|
||||||
try {
|
try {
|
||||||
const stats = fs.statSync(mp4Path);
|
const stats = fs.statSync(mp4Path);
|
||||||
if (!stats.isFile() || stats.size === 0) {
|
if (!stats.isFile() || stats.size === 0) {
|
||||||
throw new Error('Output MP4 is empty or missing');
|
throw new Error('Output MP4 is empty or missing');
|
||||||
|
}
|
||||||
|
progressState = { status: 'finished', percent: 100, details: 'Transcoding complete', mp4Url };
|
||||||
|
} catch (verifyError) {
|
||||||
|
console.error(`Output verification failed for ${mp4Path}:`, verifyError);
|
||||||
|
progressState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: `Output verification failed: ${verifyError.message}`, mp4Url };
|
||||||
}
|
}
|
||||||
progressState = { status: 'finished', percent: 100, details: 'Transcoding complete', mp4Url };
|
progressMap[progressKey] = progressState;
|
||||||
} catch (verifyError) {
|
broadcastWs(progressKey, { type: 'progress', key, progress: progressState });
|
||||||
console.error(`Output verification failed for ${mp4Path}:`, verifyError);
|
if (progressState.status === 'finished') {
|
||||||
progressState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: `Output verification failed: ${verifyError.message}`, mp4Url };
|
broadcastWs(progressKey, { type: 'ready', key, mp4Url });
|
||||||
}
|
}
|
||||||
progressMap[progressKey] = progressState;
|
})
|
||||||
broadcastWs(progressKey, { type: 'progress', key: progressKey, progress: progressState });
|
.on('error', (err) => {
|
||||||
if (progressState.status === 'finished') {
|
const errMessage = err?.message || '';
|
||||||
broadcastWs(progressKey, { type: 'ready', key: progressKey, mp4Url });
|
const isHardwareFailure = !attemptedSoftwareFallback && encoderName !== codecMap.software[safeCodec] && shouldRetryWithSoftware(errMessage);
|
||||||
}
|
if (isHardwareFailure) {
|
||||||
})
|
attemptedSoftwareFallback = true;
|
||||||
.on('error', (err) => {
|
console.warn(`Hardware encoder failed for ${key}; retrying with software encoder`, errMessage);
|
||||||
cleanupTmpInput();
|
try {
|
||||||
console.error(`Error transcoding ${key}:`, err);
|
if (fs.existsSync(mp4Path)) {
|
||||||
const failedState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: err.message || 'Transcoding failed', mp4Url };
|
fs.unlinkSync(mp4Path);
|
||||||
progressMap[progressKey] = failedState;
|
}
|
||||||
broadcastWs(progressKey, { type: 'progress', key: progressKey, progress: failedState });
|
} catch (_) {}
|
||||||
})
|
const softwareEncoder = codecMap.software[safeCodec];
|
||||||
.run();
|
progressMap[progressKey] = {
|
||||||
|
status: 'fallback',
|
||||||
|
percent: 0,
|
||||||
|
details: 'Hardware encoder unavailable, retrying with software encoder...',
|
||||||
|
mp4Url
|
||||||
|
};
|
||||||
|
broadcastWs(progressKey, { type: 'progress', key, progress: progressMap[progressKey] });
|
||||||
|
startFfmpeg(softwareEncoder);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanupTmpInput();
|
||||||
|
console.error(`Error transcoding ${key}:`, err);
|
||||||
|
const failedState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: err.message || 'Transcoding failed', mp4Url };
|
||||||
|
progressMap[progressKey] = failedState;
|
||||||
|
broadcastWs(progressKey, { type: 'progress', key, progress: failedState });
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
};
|
||||||
|
|
||||||
|
startFfmpeg(videoCodec);
|
||||||
|
|
||||||
// Return immediately so the client can start polling or waiting
|
// Return immediately so the client can start polling or waiting
|
||||||
res.json({ message: 'Transcoding started', mp4Url });
|
res.json({ message: 'Transcoding started', mp4Url });
|
||||||
|
|||||||
Reference in New Issue
Block a user