修改为下载完成之后再转码
This commit is contained in:
18
server.js
18
server.js
@@ -2,6 +2,7 @@ const express = require('express');
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const dotenv = require('dotenv');
|
const dotenv = require('dotenv');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const os = require('os');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
@@ -174,10 +175,23 @@ 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 tmpInputPath = path.join(os.tmpdir(), `s3-input-${Date.now()}-${Math.random().toString(16).slice(2)}.tmp`);
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
const writeStream = fs.createWriteStream(tmpInputPath);
|
||||||
|
s3Stream.pipe(writeStream);
|
||||||
|
s3Stream.on('error', reject);
|
||||||
|
writeStream.on('error', reject);
|
||||||
|
writeStream.on('finish', resolve);
|
||||||
|
});
|
||||||
|
|
||||||
// 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}`);
|
||||||
|
|
||||||
ffmpeg(s3Stream)
|
const cleanupTmpInput = () => {
|
||||||
|
fs.unlink(tmpInputPath, () => {});
|
||||||
|
};
|
||||||
|
|
||||||
|
ffmpeg(tmpInputPath)
|
||||||
.videoCodec(videoCodec)
|
.videoCodec(videoCodec)
|
||||||
.audioCodec('aac')
|
.audioCodec('aac')
|
||||||
.outputOptions([
|
.outputOptions([
|
||||||
@@ -204,6 +218,7 @@ app.post('/api/transcode', async (req, res) => {
|
|||||||
console.log(`ffmpeg stderr: ${stderrLine}`);
|
console.log(`ffmpeg stderr: ${stderrLine}`);
|
||||||
})
|
})
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
|
cleanupTmpInput();
|
||||||
console.log(`Finished transcoding ${key} to MP4`);
|
console.log(`Finished transcoding ${key} to MP4`);
|
||||||
let progressState;
|
let progressState;
|
||||||
try {
|
try {
|
||||||
@@ -223,6 +238,7 @@ app.post('/api/transcode', async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('error', (err) => {
|
.on('error', (err) => {
|
||||||
|
cleanupTmpInput();
|
||||||
console.error(`Error transcoding ${key}:`, err);
|
console.error(`Error transcoding ${key}:`, err);
|
||||||
const failedState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: err.message || 'Transcoding failed', mp4Url };
|
const failedState = { status: 'failed', percent: progressMap[progressKey]?.percent || 0, details: err.message || 'Transcoding failed', mp4Url };
|
||||||
progressMap[progressKey] = failedState;
|
progressMap[progressKey] = failedState;
|
||||||
|
|||||||
Reference in New Issue
Block a user