Joã*_*ago 1 javascript ffmpeg node.js
我尝试使用 Node JS 和 FFMpeg 在 na 视频流中包含字幕(srt)......我正在尝试这样做:
var command = ffmpeg(file.createReadStream())
.input("C:\\code.srt").videoCodec('copy')
.videoCodec('libvpx').audioCodec('libvorbis').format('webm')
.audioBitrate(128)
.videoBitrate(1024)
.inputFPS(75)
.outputOptions([
'-deadline realtime',
'-error-resilient 1'
])
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
[Error: ffmpeg exited with code 1: Encoder (codec none) not found for output stream #0:2
Run Code Online (Sandbox Code Playgroud)
也试试这个,使用 FFMpeg 文档的 --vf subititles= ,我有这个错误:
var command = ffmpeg(file.createReadStream())
.videoCodec('libvpx').audioCodec('libvorbis').format('webm')
.audioBitrate(128)
.videoBitrate(1024)
.inputFPS(75)
.outputOptions([
'-deadline realtime',
'-vf subtitles=C:\\code.srt',
'-error-resilient 1'
])
Error: ffmpeg exited with code 1: Error opening filters!
Run Code Online (Sandbox Code Playgroud)
有人知道在 Node.JS 中使用 FFMpeg Fluent Api 在视频中嵌入字幕
对不起我的英语,我是巴西人!非常感谢
小智 6
我通过将任务分成两部分来解决这个问题
添加字幕并将视频保存到本地代码:
var addSubtitles = function(key, callback) {
console.log("inside addSubtitles");
ffmpeg('./' + key + '.mp4')
.videoCodec('libx264')
.audioCodec('libmp3lame')
.outputOptions(
'-vf subtitles=./jellies.srt'
)
.on('error', function(err) {
callback(true, err)
})
.save('./moviewithsubtitle.mp4')
.on('end', function() {
callback(false, "done");
})
}
Run Code Online (Sandbox Code Playgroud)发送带字幕的视频
var streaming = function(req, res, newpath) {
var path = './' + newpath + '.mp4';
var stat = fs.statSync(path);
var total = stat.size;
if (req.headers['range']) {
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total - 1;
var chunksize = (end - start) + 1;
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);
var file = fs.createReadStream(path, {
start: start,
end: end
});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
});
file.pipe(res);
} else {
console.log('ALL: ' + total);
res.writeHead(200, {
'Content-Length': total,
'Content-Type': 'video/mp4'
});
fs.createReadStream(path).pipe(res);
}
}
Run Code Online (Sandbox Code Playgroud)所以现在我的整个程序变成了
var ffmpeg = require('fluent-ffmpeg');
var fs = require('fs');
var express = require('express');
var app = express();
var uuid = require('node-uuid');
var port = 8000;
var addSubtitles = function(key, callback) {
console.log("inside addSubtitles");
ffmpeg('./' + key + '.mp4')
.videoCodec('libx264')
.audioCodec('libmp3lame')
.outputOptions(
'-vf subtitles=./jellies.srt'
)
.on('error', function(err) {
callback(true, err)
})
.save('./moviewithsubtitle.mp4')
.on('end', function() {
callback(false, "done");
})
}
var streaming = function(req, res, newpath) {
var path = './' + newpath + '.mp4';
var stat = fs.statSync(path);
var total = stat.size;
if (req.headers['range']) {
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total - 1;
var chunksize = (end - start) + 1;
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);
var file = fs.createReadStream(path, {
start: start,
end: end
});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
});
file.pipe(res);
} else {
console.log('ALL: ' + total);
res.writeHead(200, {
'Content-Length': total,
'Content-Type': 'video/mp4'
});
fs.createReadStream(path).pipe(res);
}
}
app.get('/subs', function(req, res) {
addSubtitles("movie", function(error, newpath) {
if (error) {
res.send("error : " + error)
} else {
console.log("done");
res.end();
}
})
})
app.get('/', function(req, res) {
streaming(req, res, "moviewithsubtitle");
})
app.listen(port);
console.log("the server is running at port :", port);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2722 次 |
| 最近记录: |