Skip to content

Commit

Permalink
feat(9): Read environment variables (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipjscott authored Aug 26, 2018
1 parent 14a9cb7 commit 816882c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,35 @@ Below is a comparison between `ffprobe-client` and other popular `ffprobe` libra
## Usage

```js
// optional: specify a binary path, see config
process.env.FFPROBE_PATH = '/usr/bin/ffprobe'
const ffprobe = require('ffprobe-client')

const file = './path/to/file'
const url = 'http://www.example.com/foo.webm'

ffprobe(file).then((fileData) => {
console.log(fileData)
// promise
ffprobe('./myfile.webm')
.then(data => console.log(data.format.duration))
.catch(err => console.error(err))

return ffprobe(url)
}).then((urlData) => {
console.log(urlData)
}).catch(err => console.error(err))
// async/await
(async () => {
try {
const data = await ffprobe('http://www.example.com/foo.webm')

console.log(data)
} catch (err) {
console.error(err)
}
})()
```

## API

#### ffprobe(target, [config])

Returns a `Promise` which resolves to the `JSON` outputted by `ffprobe`.
Returns a `Promise` which resolves to a `JSON` outputted by `ffprobe`.

#### target

Expand All @@ -59,7 +69,7 @@ A configuration object, see details below.

Type: `String`

The path of the `ffprobe` binary. If omitted, the path will default to `ffprobe`.
The path of the `ffprobe` binary. If omitted, the path will be set to the `FFPROBE_PATH` environment variable. If the environment variable is not set, `ffprobe` will be invoked directly (ie. `ffprobe [...]`).

## Payload

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function ffprobeExecFile (path, params) {
}

function ffprobe (target, config = {}) {
const path = config.path || 'ffprobe'
const path = config.path || process.env.FFPROBE_PATH || 'ffprobe'
const params = [
'-show_streams',
'-show_format',
Expand Down
19 changes: 16 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ const { expect } = chai
const validPathCases = [
{
description: 'Behaviour with default ffprobe path',
config: undefined
config: undefined,
env: null
},
{
description: 'Behaviour with custom path',
config: { path: '/usr/bin/ffprobe' }
description: 'Behaviour with passed custom path',
config: { path: '/usr/bin/ffprobe' },
env: null
},
{
description: 'Behaviour with environment variable path',
config: undefined,
env: '/usr/bin/ffprobe'
}
]

Expand All @@ -35,6 +42,8 @@ describe('ffprobe-client', () => {
nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
.get(parsedUrl.path)
.replyWithFile(200, filePath, { 'Content-Type': 'video/webm' })

process.env.FFPROBE_PATH = ''
})

describe('Invalid binary path', () => {
Expand All @@ -50,6 +59,10 @@ describe('ffprobe-client', () => {
})

validPathCases.forEach((validPathCase) => {
if (validPathCase.env) {
process.env.FFPROBE_PATH = validPathCase.env
}

describe(validPathCase.description, () => {
it('resolves to correct JSON when target is a file', async () => {
try {
Expand Down

0 comments on commit 816882c

Please sign in to comment.