【问题标题】:Node.JS: FFmpeg piped video encoding no thumbnail createdNode.JS:FFmpeg 管道视频编码未创建缩略图
【发布时间】:2013-04-04 04:35:36
【问题描述】:

我想在上传到 ffmpeg 期间通过管道传输视频以实时创建缩略图。 一切正常,但没有创建 thumbnail.jpg 并且 ffmpeg 标准错误在库版本显示后挂起。

更新:我更新了我的代码,但它也没有创建有效的缩略图。

var formidable = require('formidable'),
        http = require('http'),
        sys = require('sys'),
        spawn = require('child_process').spawn;

function spawnFfmpeg(exitCallback) {
    var args = ['-i', 'pipe:0', '-c:v', 'mjpeg', '-ss', '00:00:13', '-vframes', '1', '-s', '100x80', 'thumbnail.jpg']
    var ffmpeg = spawn('ffmpeg', args);
    console.log('Spawning ffmpeg ' + args.join(' '));

    ffmpeg.on('exit', exitCallback);
    ffmpeg.stderr.on('data', function(data) {
        console.log('grep stderr: ' + data);
    });
    return ffmpeg;
}

http.createServer(function(req, res) {
    if (req.url == '/' && req.method.toLowerCase() == 'get') {
        // show a file upload form
        res.writeHead(200, {'content-type': 'text/html'});
        res.end
                ('<form action="/upload" enctype="multipart/form-data" method="post">'
                        + '<input type="text" name="title"><br>'
                        + '<input type="file" name="upload" multiple="multiple"><br>'
                        + '<input type="submit" value="Upload">'
                        + '</form>'
                        );
    } else if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
        // parse a file upload
        var form = new formidable.IncomingForm();
        form.maxFieldsSize = 29 * 1024 * 1024;
        // Handle each part of the multi-part post
        var ffmpeg = spawnFfmpeg(function(code) {
            console.log('child process exited with code ' + code);
            res.end();
        });

        var form = new formidable.IncomingForm();
        // Handle each part of the multi-part post
        form.onPart = function(part) {
            // Handle each data chunk as data streams in
            part.addListener('data', function(data) {
                ffmpeg.stdout.pipe(res);
                res.pipe(ffmpeg.stdin);
                // Write each chunk to disk
                //savedFile.write(data);
            });
        };

        // Do it
        form.parse(req);
        return;
    }
}).listen(80, "127.0.0.1");

process.on('uncaughtException', function(err) {
});

【问题讨论】:

    标签: javascript node.js ffmpeg


    【解决方案1】:

    经过一些研究和数小时的测试,我找到了正确的解决方案^_^

    var formidable = require('formidable'),
            http = require('http'),
            sys = require('sys'),
            spawn = require('child_process').spawn;
    
    function spawnFfmpeg(exitCallback) {
        var args = ['-i', 'pipe:0', '-c:v', 'mjpeg', '-ss', '00:00:13', '-vframes', '1', '-s', '100x80', 'thumbnail.jpg']
        var ffmpeg = spawn('ffmpeg', args);
        console.log('Spawning ffmpeg ' + args.join(' '));
    
        ffmpeg.on('exit', exitCallback);
        ffmpeg.stderr.on('data', function(data) {
            console.log('grep stderr: ' + data);
        });
        return ffmpeg;
    }
    
    http.createServer(function(req, res) {
        if (req.url == '/' && req.method.toLowerCase() == 'get') {
            // show a file upload form
            res.writeHead(200, {'content-type': 'text/html'});
            res.end
                    ('<form action="/upload" enctype="multipart/form-data" method="post">'
                            + '<input type="text" name="title"><br>'
                            + '<input type="file" name="upload" multiple="multiple"><br>'
                            + '<input type="submit" value="Upload">'
                            + '</form>'
                            );
        } else if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
            // parse a file upload
            var form = new formidable.IncomingForm();
            form.maxFieldsSize = 29 * 1024 * 1024;
            // Handle each part of the multi-part post
            var ffmpeg = spawnFfmpeg(function(code) {
                console.log('child process exited with code ' + code);
                res.end();
            });
    
            var form = new formidable.IncomingForm();
            // Handle each part of the multi-part post
            form.onPart = function(part) {
                // Handle each data chunk as data streams in
                part.addListener('data', function(data) {
                    /*
                     * This only one line was the solution of my problem now all works really fast !! 500mbit like transloadit it does
                     */
                    ffmpeg.stdin.write(data);
                });
            };
    
            // Do it
            form.parse(req);
            return;
        }
    }).listen(80, "127.0.0.1");
    
    process.on('uncaughtException', function(err) {
    });
    

    【讨论】:

      【解决方案2】:

      您的代码看起来像是在设置管道之前启动 ffmpeg。

      以下内容未经验证,但听起来像您需要的:

      http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

      child_process.spawn() 的“stdio”选项是一个数组,其中每个索引对应于子进程中的一个 fd。该值为以下之一:

      'pipe' - 在子进程和父进程之间创建一个管道。管道的父端作为 ChildProcess.stdio[fd] 的 child_process 对象上的属性向父端公开。为 fds 0 - 2 创建的管道也可以分别作为 ChildProcess.stdin、ChildProcess.stdout 和 ChildProcess.stderr 使用。

      【讨论】:

      • 解决方案现在由我创立,感谢您对 sascha 的大力帮助
      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2018-05-15
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2013-09-24
      • 2011-08-11
      相关资源
      最近更新 更多