使用node.js stdout管道输出ffmpeg [英] Pipe output of ffmpeg using nodejs stdout

查看:88
本文介绍了使用node.js stdout管道输出ffmpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过stdout传递ffmpeg的输出.

I am not being able to pipe the output of the ffmpeg over a stdout.

以下是我到目前为止编写的代码块.

Following are the block of code what I coded so far.

    var http = require('http')
    , fs = require('fs') 
    var child_process = require("child_process")

    http.createServer(function (req, res) {
    console.log("Request:", dump_req(req) , "\n")

    // path of the 
    var path = 'test-mp4.mp4'  //test-mp4-long.mp4
    , stat = fs.statSync(path)
    , total = stat.size


    var range = req.headers.range
    , parts = range.replace(/bytes=/, "").split("-")
    , partialstart = parts[0]
    , partialend = parts[1]
    , start = parseInt(partialstart, 10)
    , end = partialend ? parseInt(partialend, 10) : total-1
    , chunksize = (end-start)+1


    console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize +  "\n")


    var ffmpeg = child_process.spawn("ffmpeg",[
            "-i", path,             // path
            "-b:v" , "64k",         // bitrate to 64k
            "-bufsize", "64k", 
            "-"                     // Output to STDOUT
        ]);


    //set header
    res.writeHead(206
    , { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total
    , 'Accept-Ranges': 'bytes', 'Content-Length': chunksize
    , 'Content-Type': 'video/mp4'
    })

    stdout[ params[1] ] = ffmpeg.stdout

    // Pipe the video output to the client response
    ffmpeg.stdout.pipe(res);

    console.log("Response", dump_res(res), "\n")
    }).listen(1337)

当我从上面的代码替换了ffmpeg素材时,一切正常.以下是我替换ffmpeg内容时的部分代码.

When i replaced the ffmpeg stuffs from above code, all works fine. Following is the part of the code when i replace the ffmpeg stuffs.

 var file = fs.createReadStream(path, {start: start, end: end})

管道如下:

file.pipe(res)

我在做什么错?

修改:ffmpeg命令可以正常工作.我已经通过命令行进行了测试,并生成了正确的输出.

The ffmpeg command works fine. I have tested this through the command line and generating proper output.

推荐答案

您必须传递 pipe:1 来告诉FFmpeg将输出写入stdout.这是一个来自我的项目的示例:

You have to pass pipe:1 to tell FFmpeg to write output to stdout. Here's an example taken from one of my projects:

 var ffmpeg = spawn(argv.ffmpeg, [
    '-i', argv.file,
    '-f', 's16le', // PCM 16bits, little-endian
    '-ar', '44100', // Sampling rate
    '-ac', 2, // Stereo
    'pipe:1' // Output on stdout
  ]);

https://github.com/lperrin/node_airtunes/blob/master/examples/play_ffmpeg.js

这篇关于使用node.js stdout管道输出ffmpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆