node.js - 使用nodejs stdout管道输出ffmpeg

标签 node.js video stream ffmpeg

我无法通过标准输出传输 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 命令工作正常。我已经通过命令行对此进行了测试并生成了正确的输出。

最佳答案

你必须传递 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

关于node.js - 使用nodejs stdout管道输出ffmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167893/

相关文章:

javascript - 我怎样才能把这一系列的javascript变量变成一个函数呢?

node.js - 如何使用 Knex 获取与我正在查询的表相关的行

video - FFMPEG - 链接 - 视频、音频、ASS 字幕,然后覆盖 Logo ?

html - HTML5标签不起作用

Java 7 Try-With-Resources (AutoCloseable) 实现

javascript - Node JS 如何将字符串转换为对象

node.js - Typescript 建立绝对导入 Node

ios - 如何使用 AVPlayerLooper 循环播放视频

video - 在两个单独的接收器上同步视频流

C++ cin char 逐个符号读取