【问题标题】:Pipe output of ffmpeg using nodejs stdout使用nodejs标准输出的ffmpeg的管道输出
【发布时间】:2014-04-05 17:57:42
【问题描述】:

我无法通过标准输出管道传输 ffmpeg 的输出。

以下是我目前编写的代码块。

    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 东西时的代码部分。

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

还有像这样的管道:

file.pipe(res)

我在运行什么错误?

编辑: ffmpeg 命令工作正常。我已经通过命令行对此进行了测试并生成了正确的输出。

【问题讨论】:

  • 如果有人试图按原样使用这个脚本,它会丢失一个:res.on("close", () => { ffmpeg.kill(); }); 否则当关闭选项卡/浏览器窗口时,ffmpeg 进程会在后台运行。

标签: node.js video stream ffmpeg


【解决方案1】:

你必须通过 pipe:1 告诉 FFmpeg 将输出写入标准输出。这是我的一个项目中的一个示例:

 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

【讨论】:

  • 非常感谢。添加“管道:1”后,它现在以 webm 文件格式工作。你有什么想法像在 youtube 中一样控制带宽吗?
  • 我使用了一个循环缓冲区来控制带宽。你可以在我的 github repo 中看到代码。
  • 我收到Unable to find a suitable output format for 'pipe:1'
  • @jpillora 尝试添加类似-f mp3 的内容来强制输出格式
  • 应该是-progress pipe:1 否则你会得到@jpillora 提到的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多