gpt4 book ai didi

javascript - 使用 Node 的 ffmpeg 应用程序偶尔会崩溃,因为文件似乎没有被正确读取

转载 作者:行者123 更新时间:2023-12-04 22:56:40 25 4
gpt4 key购买 nike

我有一个简单的 Node 应用程序,它允许我将 AWS S3 URL 链接传递给文件(在本例中为视频文件)。它使用 FFMPEG 库读取视频文件并返回编解码器、持续时间、比特率等数据。
该脚本是从 PHP 脚本调用的,该脚本又将数据发送到 Node 端点并将 Amazon S3 URL 传递给 Node 。有时,由于没有明显的原因,视频文件无法返回有关容器、编解码器、持续时间等的预期值......并且只返回“0”。但是当我再次尝试完全相同的文件/请求时,它会正确返回此数据,例如 container:mp4我不确定,但我认为脚本在某种程度上需要 createWriteStream关闭,但我不能确定,问题是我发现的问题并不是一直发生,而是偶尔发生,所以当它难以复制时,很难解决这个问题。
有任何想法吗?

router.post('/', async function(req, res) {
const fileURL = new URL(req.body.file);
var path = fileURL.pathname;
path = 'tmp/'+path.substring(1); // removes the initial / from the path

let file = fs.createWriteStream(path); // create the file locally
const request = https.get(fileURL, function(response) {
response.pipe(file);
});

// after file has saved
file.on('finish', function () {
var process = new ffmpeg(path);
process.then(function (video) {
let metadata = formatMetadata(video.metadata);

res.send ({
status: '200',
data: metadata,
errors: errors,
response: 'success'
});

}, function (err) {
console.warn('Error: ' + err);

res.send ({
status: '400',
data: 'Something went wrong processing this video',
response: 'fail',
});
});
});

file.on('error', function (err) {
console.warn(err);
});

});

function formatMetadata(metadata) {
const data = {
'video' : metadata.video,
'audio' : metadata.audio,
'duration' : metadata.duration
};
return data;
}
//预期输出
{"data":{"video":{"container":"mov","bitrate":400,"stream":0,"codec":"h264","resolution":{"w":1280,"h":720},"resolutionSquare":{"w":1280,"h":720},"aspect":{"x":16,"y":9,"string":"16:9","value":1.7777777777777777},"rotate":0,"fps":25,"pixelString":"1:1","pixel":1},"audio":{"codec":"aac","bitrate":"127","sample_rate":44100,"stream":0,"channels":{"raw":"stereo","value":2}},"duration":{"raw":"00:00:25.68","seconds":25}}
//实际输出
{"data":{"video":{"container":"","bitrate":0,"stream":0,"codec":"","resolution":{"w":0,"h":0},"resolutionSquare":{"w":0,"h":null},"aspect":{},"rotate":0,"fps":0,"pixelString":"","pixel":0},"audio":{"codec":"","bitrate":"","sample_rate":0,"stream":0,"channels":{"raw":"","value":""}},"duration":{"raw":"","seconds":0}}
注意 - 这偶尔会发生

最佳答案

您没有考虑从 AWS 获取失败的原因。在继续使用管道之前,您应该检查响应的状态代码。

  const request = https.get(fileURL, function(response) {
if(response.statusCode == 200)
response.pipe(file);
else
// Handle error case
});

关于javascript - 使用 Node 的 ffmpeg 应用程序偶尔会崩溃,因为文件似乎没有被正确读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72406201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com