使用NodeJS和FFMpeg Fluent Api的视频中的字幕

时间:2016-02-25 21:14:46

标签: javascript node.js ffmpeg

我尝试使用Node JS和FFMpeg在navídeo流中包含字幕(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'
  ])

我收到了这个错误:

[Error: ffmpeg exited with code 1: Encoder (codec none) not found for output stream #0:2

尝试使用--vf subititles = FFMpeg的文档,我也遇到了这个错误:

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!

有人知道在Vista中使用FFMpeg Fluent Api在vídeo中嵌入字幕

对不起我的英文,我是巴西人!非常感谢

1 个答案:

答案 0 :(得分:2)

我通过将任务分成两部分来解决这个问题

  1. 添加字幕并在本地保存视频 代码:

    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");
            })
    }
    
  2. 发送带字幕的视频

    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);
        }
    }
    
  3. 所以现在我的整个程序变成了

        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);