├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── test └── basic.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test/sample.webm -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - '5' 5 | script: 6 | - npm run test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Thomas Mullen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fluent-ffmpeg-multistream 2 | 3 | [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 4 | 5 | Uses Unix domain sockets to add support for multiple stream inputs/outputs to [fluent-ffmpeg](https://github.com/fluent-ffmpeg/node-fluent-ffmpeg). 6 | 7 | ```javascript 8 | const ffmpeg = require('fluent-ffmpeg') 9 | const { StreamInput, StreamOutput } = require('fluent-ffmpeg-multistream') 10 | 11 | ffmpeg() 12 | .input(StreamInput(readableStream1).url) 13 | .input(StreamInput(readableStream2).url) 14 | .output(StreamOutput(writableStream1).url) 15 | .output(StreamOutput(writableStream2).url) 16 | ``` 17 | 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fluent-ffmpeg-multistream", 3 | "version": "1.0.0", 4 | "description": "Adds support for multiple stream inputs/outputs to fluent-ffmpeg.", 5 | "main": "src/index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/t-mullen/fluent-ffmpeg-multistream.git" 12 | }, 13 | "scripts": { 14 | "test": "standard --fix src/** && tape test/*.js" 15 | }, 16 | "author": "Thomas Mullen", 17 | "license": "MIT", 18 | "devDependencies": { 19 | "fluent-ffmpeg": "^2.1.2", 20 | "standard": "^8.6.0", 21 | "tape": "^4.6.3" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const net = require('net') 2 | const fs = require('fs') 3 | const path = require('path'); 4 | 5 | var counter = 0 6 | class UnixStream { 7 | constructor (stream, onSocket) { 8 | const socketPath = path.resolve('/tmp/', (++counter), '.sock'); 9 | this.url = 'unix:' + socketPath 10 | 11 | try { 12 | fs.statSync(socketPath) 13 | fs.unlinkSync(socketPath) 14 | } catch (err) {} 15 | const server = net.createServer(onSocket) 16 | stream.on('finish', () => { 17 | server.close() 18 | }) 19 | server.listen(socketPath) 20 | } 21 | } 22 | 23 | function StreamInput (stream) { 24 | return new UnixStream(stream, socket => stream.pipe(socket)) 25 | } 26 | module.exports.StreamInput = StreamInput 27 | 28 | function StreamOutput (stream) { 29 | return new UnixStream(stream, socket => socket.pipe(stream)) 30 | } 31 | module.exports.StreamOutput = StreamOutput 32 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const ffmpeg = require('fluent-ffmpeg') 3 | const { StreamInput, StreamOutput } = require('./../') 4 | const { Writable, Readable } = require('stream') 5 | const fs = require('fs') 6 | 7 | test('two outputs', function (t) { 8 | t.plan(2) 9 | 10 | const ws1 = new Writable() 11 | ws1._write = () => { 12 | ws1._write = () => {} 13 | t.pass('got data on ws1') 14 | } 15 | 16 | const ws2 = new Writable() 17 | ws2._write = () => { 18 | ws2._write = () => {} 19 | t.pass('got data on ws2') 20 | } 21 | 22 | const command = ffmpeg() 23 | .addInput('./test/sample.webm') 24 | .addOutput(StreamOutput(ws1).url) 25 | .addOutputOption('-f mpegts') 26 | .addOutput(StreamOutput(ws2).url) 27 | .addOutputOption('-f mpegts') 28 | .on('error', (err) => {}) 29 | 30 | command.run() 31 | }) 32 | 33 | test('two inputs, two outputs', function (t) { 34 | t.plan(2) 35 | 36 | const rs1 = fs.createReadStream('./test/sample.webm') 37 | const rs2 = fs.createReadStream('./test/sample.webm') 38 | 39 | const ws1 = new Writable() 40 | ws1._write = () => { 41 | ws1._write = () => {} 42 | t.pass('got data on ws1') 43 | } 44 | 45 | const ws2 = new Writable() 46 | ws2._write = () => { 47 | ws2._write = () => {} 48 | t.pass('got data on ws2') 49 | } 50 | 51 | const command = ffmpeg() 52 | .addInput(StreamInput(rs1).url) 53 | .addInput(StreamInput(rs2).url) 54 | .output(StreamOutput(ws1).url) 55 | .addOutputOption('-f mpegts') 56 | .output(StreamOutput(ws2).url) 57 | .addOutputOption('-f mpegts') 58 | .on('error', (err) => { 59 | console.log(err); 60 | }) 61 | 62 | command.run() 63 | }) 64 | 65 | test('cleanup', function (t) { 66 | t.end() 67 | process.exit(0) 68 | }) --------------------------------------------------------------------------------