Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwooley committed Oct 2, 2016
0 parents commit fe6ab19
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.nyc_output
coverage
*.log
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- '6'
- '4'
after_script:
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outDir": null
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Place your settings in this file to overwrite default and user settings.
{
"eslint.enable": false,
"standard.enable": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Eric Wooley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'
var shell = require('shelljs')
var endOfLine = require('os').EOL

var deviceModeEnum = {
unkown: 0,
video: 1,
audio: 2
}
/**
* Get a list of usuable recording devices
* @param ffmpeg Command to run ffmpeg
* @param opts Options
* @param opts.ffmpegOutput: string Use this output instead of output from ffmpeg
* @return collection Collection of available devices
* @return collection.audio ffmpeg listed Audio devices
* @return collection.video ffmpeg listed Video devices
* @return collection.unkown ffmpeg listed unkown devices
*/
function getFFMPEGDevices (ffmpeg, opts) {
ffmpeg = ffmpeg || 'ffmpeg'
opts = opts || {}
var output = opts.ffmpegOutput || shell.exec(ffmpeg + ' -f avfoundation -list_devices true -i ""', {silent: true})
output = opts.ffmpegOutput || output.stdout || output.stderr
output = output.trim()
output = output.split(endOfLine)
// example line: [AVFoundation input device @ 0x7ff1799001c0] [1] Built-in Microphone
.filter(function (line) { return line.match(/AVFoundation/) })
var deviceTypeMode = deviceModeEnum.unkown // 0 for unknown, 1 for video, 2 for audio
var deviceLists = output.reduce(function (collection, line) {
if (line.match(/AVFoundation video devices/)) {
deviceTypeMode = deviceModeEnum.video
} else if (line.match(/AVFoundation audio devices/)) {
deviceTypeMode = deviceModeEnum.audio
} else {
var extractedText = extractTextFromLine(line)
switch (deviceTypeMode) {
case deviceModeEnum.video:
collection.video.push(extractedText)
break
case deviceModeEnum.audio:
collection.audio.push(extractedText)
break
default:
collection.unknown.push(extractedText)
}
}
return collection
}, { audio: [], video: [], unknown: [] })
// .map(function (line) {return line.match(/(\[\d*\].*)/)[1]})
return deviceLists
};
module.exports = getFFMPEGDevices
function extractTextFromLine (line) {
var extractedText
try {
extractedText = line.match(/(\[\d*\].*)/)[1]
} catch (e) {
extractedText = line
}
return extractedText
}
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "ffmpeg-devices",
"version": "0.0.0",
"description": "Git a list of avialable audio and video devices from ffmpeg",
"license": "MIT",
"repository": "ericwooley/ffmpeg-devices",
"author": {
"name": "Eric Wooley",
"email": "ericwooley@gmail.com",
"url": "github.com/ericwooley"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "standard && nyc ava",
"test-watch": "standard && ava test.js --watch"
},
"files": [
"index.js"
],
"keywords": [
""
],
"dependencies": {
"shelljs": "^0.7.4"
},
"devDependencies": {
"ava": "^0.15.2",
"coveralls": "^2.11.12",
"nyc": "^7.1.0",
"standard": "^8.3.0"
},
"xo": {
"esnext": true
},
"nyc": {
"reporter": [
"lcov",
"text"
]
}
}
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ffmpeg-devices [![Build Status](https://travis-ci.org/ericwooley/ffmpeg-devices.svg?branch=master)](https://travis-ci.org/ericwooley/ffmpeg-devices) [![Coverage Status](https://coveralls.io/repos/github/ericwooley/ffmpeg-devices/badge.svg?branch=master)](https://coveralls.io/github/ericwooley/ffmpeg-devices?branch=master)

> Git a list of avialable audio and video devices from ffmpeg
# WARNING

Currently this only works on mac. If you want this to work on another platform, that is coming eventually, and PR's are welcome!

## Install

```
$ npm install --save ffmpeg-devices
```


## Usage

```js
const ffmpegDevices = require('ffmpeg-devices');

ffmpegDevices();
// => {
// audio: [], // audio devices
// video: [], // video devices
// unknown: [] // unknown devices
// }

// If you want to use a specific bin
ffmpegDevices('<path to ffmpeg binary>')

// can specify an input to parse if you would prefer
ffmpeg(null, {
ffmpegOutput: `some output to parse` // ffmpeg should not be called in this case.
})
```

## License

MIT © [Eric Wooley](http://github.com/ericwooley)
68 changes: 68 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import test from 'ava'
import fn from './index'

const exampleInput1 = `
ffmpeg version N-81741-g1212e34-tessus Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.38)
configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --as=yasm --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb
libavutil 55. 30.100 / 55. 30.100
libavcodec 57. 58.100 / 57. 58.100
libavformat 57. 50.100 / 57. 50.100
libavdevice 57. 0.102 / 57. 0.102
libavfilter 6. 62.100 / 6. 62.100
libswscale 4. 1.100 / 4. 1.100
libswresample 2. 1.100 / 2. 1.100
libpostproc 54. 0.100 / 54. 0.100
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation video devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7ff1799001c0] [1] Capture screen 0
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation audio devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] USB audio CODEC
[AVFoundation input device @ 0x7ff1799001c0] [1] Built-in Microphone
`
test('it should parse an input', (t) => {
t.deepEqual(fn(null, { ffmpegOutput: exampleInput1 }), JSON.parse(`{"audio":["[0] USB audio CODEC","[1] Built-in Microphone"],"video":["[0] FaceTime HD Camera","[1] Capture screen 0"],"unknown":[]}`))
})

const exampleInput2 = `
ffmpeg version N-81741-g1212e34-tessus Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.38)
configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --as=yasm --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb
libavutil 55. 30.100 / 55. 30.100
libavcodec 57. 58.100 / 57. 58.100
libavformat 57. 50.100 / 57. 50.100
libavdevice 57. 0.102 / 57. 0.102
libavfilter 6. 62.100 / 6. 62.100
libswscale 4. 1.100 / 4. 1.100
libswresample 2. 1.100 / 2. 1.100
libpostproc 54. 0.100 / 54. 0.100
`
test('it should return empty arrays when nothing is available', (t) => {
t.deepEqual(fn(null, { ffmpegOutput: exampleInput2 }), JSON.parse(`{"audio":[],"video":[],"unknown":[]}`))
})

const exampleInput3 = `
[AVFoundation input device @ 0x7ff1799001c0] [0] Unknown device
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation video devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7ff1799001c0] [1] Capture screen 0
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation audio devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] USB audio CODEC
[AVFoundation input device @ 0x7ff1799001c0] [1] Built-in Microphone
`
test('it should handle devices which are listed before an av association is made ', (t) => {
t.deepEqual(fn(null, { ffmpegOutput: exampleInput3 }), JSON.parse(`{"audio":["[0] USB audio CODEC","[1] Built-in Microphone"],"video":["[0] FaceTime HD Camera","[1] Capture screen 0"],"unknown":["[0] Unknown device"]}`))
})

const exampleInput4 = `
[AVFoundation input device @ 0x7ff1799001c0] Unknown device
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation video devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7ff1799001c0] [1] Capture screen 0
[AVFoundation input device @ 0x7ff1799001c0] AVFoundation audio devices:
[AVFoundation input device @ 0x7ff1799001c0] [0] USB audio CODEC
[AVFoundation input device @ 0x7ff1799001c0] [1] Built-in Microphone
`
test('it handle unkown devices', (t) => {
t.deepEqual(fn(null, { ffmpegOutput: exampleInput4 }), JSON.parse(`{"audio":["[0] USB audio CODEC","[1] Built-in Microphone"],"video":["[0] FaceTime HD Camera","[1] Capture screen 0"],"unknown":["[AVFoundation input device @ 0x7ff1799001c0] Unknown device"]}`))
})

0 comments on commit fe6ab19

Please sign in to comment.