From 7581d33112a85ef618187a5aa279ae80e3f8bcbd Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Fri, 3 Apr 2026 00:49:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8B=E8=BD=BD=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.html | 2 +- public/js/main.js | 4 ++-- server.js | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/public/index.html b/public/index.html index 2809741..b752d17 100644 --- a/public/index.html +++ b/public/index.html @@ -67,7 +67,7 @@ - +
diff --git a/public/js/main.js b/public/js/main.js index 8edaaa2..ddf195d 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -116,7 +116,7 @@ document.addEventListener('DOMContentLoaded', () => { const buildStreamUrl = (targetSeconds = null) => { const codec = codecSelect?.value || 'h264'; - const encoder = encoderSelect?.value || 'software'; + const encoder = encoderSelect?.value || 'vaapi'; const streamSessionId = createStreamSessionId(); let streamUrl = `/api/stream?bucket=${encodeURIComponent(selectedBucket)}&key=${encodeURIComponent(selectedKey)}&codec=${encodeURIComponent(codec)}&encoder=${encodeURIComponent(encoder)}&streamSessionId=${encodeURIComponent(streamSessionId)}`; if (typeof targetSeconds === 'number' && Number.isFinite(targetSeconds) && targetSeconds > 0) { @@ -909,7 +909,7 @@ document.addEventListener('DOMContentLoaded', () => { try { const codec = codecSelect?.value || 'h264'; - const encoder = encoderSelect?.value || 'software'; + const encoder = encoderSelect?.value || 'vaapi'; if (!selectedBucket) throw new Error('No bucket selected'); const streamUrl = buildStreamUrl(); videoPlayer.src = streamUrl; diff --git a/server.js b/server.js index f5a5417..bdb6fc4 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,6 @@ const express = require('express'); const cors = require('cors'); const dotenv = require('dotenv'); const fs = require('fs'); -const os = require('os'); const path = require('path'); const http = require('http'); const WebSocket = require('ws'); @@ -15,6 +14,7 @@ const app = express(); const PORT = process.env.PORT || 3000; const HOST = process.env.HOST || process.env.LISTEN_ADDRESS || '0.0.0.0'; const server = http.createServer(app); +const DOWNLOAD_CACHE_DIR = path.join(__dirname, 'Downloads'); app.use(cors()); app.use(express.json()); @@ -66,6 +66,12 @@ const getProgressKey = (key) => key.split('/').map(segment => segment.replace(/[ const createStreamSessionId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`; +const ensureDownloadCacheDir = () => { + if (!fs.existsSync(DOWNLOAD_CACHE_DIR)) { + fs.mkdirSync(DOWNLOAD_CACHE_DIR, { recursive: true }); + } +}; + const addWsClient = (progressKey, ws) => { if (!wsSubscriptions.has(progressKey)) { wsSubscriptions.set(progressKey, new Set()); @@ -257,13 +263,12 @@ wss.on('connection', (ws) => { const clearDownloadCache = () => { - const tmpDir = os.tmpdir(); try { - if (!fs.existsSync(tmpDir)) return; - const files = fs.readdirSync(tmpDir); + ensureDownloadCacheDir(); + const files = fs.readdirSync(DOWNLOAD_CACHE_DIR); for (const file of files) { if (file.startsWith('s3-input-') && file.endsWith('.tmp')) { - const filePath = path.join(tmpDir, file); + const filePath = path.join(DOWNLOAD_CACHE_DIR, file); fs.rmSync(filePath, { force: true }); } } @@ -392,7 +397,7 @@ app.get('/api/stream', async (req, res) => { } const safeCodec = codec === 'h265' ? 'h265' : 'h264'; - const safeEncoder = ['nvidia', 'intel', 'vaapi', 'neon'].includes(encoder) ? encoder : 'software'; + const safeEncoder = ['nvidia', 'intel', 'vaapi', 'neon'].includes(encoder) ? encoder : 'vaapi'; const codecMap = { software: { h264: 'libx264', h265: 'libx265' }, neon: { h264: 'libx264', h265: 'libx265' }, @@ -405,7 +410,8 @@ app.get('/api/stream', async (req, res) => { const safeKeySegments = key.split('/').map(segment => segment.replace(/[^a-zA-Z0-9_\-]/g, '_')); const progressKey = safeKeySegments.join('/'); const safeBucket = bucket.replace(/[^a-zA-Z0-9_\-]/g, '_'); - const tmpInputPath = path.join(os.tmpdir(), `s3-input-${safeBucket}-${safeKeySegments.join('-')}.tmp`); + ensureDownloadCacheDir(); + const tmpInputPath = path.join(DOWNLOAD_CACHE_DIR, `s3-input-${safeBucket}-${safeKeySegments.join('-')}.tmp`); const cacheExists = fs.existsSync(tmpInputPath); const auth = extractS3Credentials(req);