Skip to content

Commit

Permalink
refactor(): finalize target api
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Jan 13, 2020
1 parent 45f4bd1 commit 35924cd
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 969 deletions.
54 changes: 54 additions & 0 deletions configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { ffmpeg } = require('./ffmpeg')
const settings = require('./settings')

/**
* Configures module level settings like binary paths
* for various commands (ffmpeg, ffprobe, mkvmerge, x264)
* and shared settings between functions.
* @public
* @param {?(Object)} opts
* @param {?(Object)} opts.bin
* @param {?(String)} opts.bin.x264
* @param {?(String)} opts.bin.ffmpeg
* @param {?(String)} opts.bin.ffprobe
*/
function configure(opts) {
if (!opts || 'object' !== typeof opts) {
opts = {}
}

if (opts.bin) {
if ('string' ==== typeof opts.bin.x264) {
settings.bin.x264 = opts.bin.x264
}

if ('string' ==== typeof opts.bin.mkvmerge) {
settings.bin.mkvmerge = opts.bin.mkvmerge
}

if ('string' === typeof opts.bin.ffmpeg) {
settings.bin.ffmpeg = opts.bin.ffmpeg
}

if ('string' === typeof opts.bin.ffprobe) {
settings.bin.ffprobe = opts.bin.ffprobe
}
}

if (settings.bin.ffmpeg) {
ffmpeg.setFfmpegPath(settings.bin.ffmpeg)
}

if (settings.bin.ffprobe) {
ffmpeg.setFfprobePath(opts.bin.ffprobe)
}

return settings
}

/**
* Module exports.
*/
module.exports = {
configure
}
3 changes: 2 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated on Tue Dec 17 2019 21:57:56 GMT-0500 (Eastern Standard Time)
// Generated on Wed Dec 18 2019 10:40:02 GMT-0500 (Eastern Standard Time)

const MUX_3G2_FORMAT = '3g2' // 3GP2 (3GPP2 file format)
const MUX_3GP_FORMAT = '3gp' // 3GP (3GPP file format)
Expand Down Expand Up @@ -478,6 +478,7 @@ const DEMUX_XWD_PIPE_FORMAT = 'xwd_pipe' // piped xwd sequence
const DEMUX_XWMA_FORMAT = 'xwma' // Microsoft xWMA
const DEMUX_YOP_FORMAT = 'yop' // Psygnosis YOP
const DEMUX_YUV4MPEGPIPE_FORMAT = 'yuv4mpegpipe' // YUV4MPEG pipe

/**
* Module exports.
*/
Expand Down
10 changes: 10 additions & 0 deletions example/target/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { Target } = require('../../target')

const target = new Target('ps4')
target.ready((err) => {
if (err) { throw err }
console.log(target.name);
console.log(target.config);
console.log(target.limits);
console.log(target.options);
})
3 changes: 2 additions & 1 deletion ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const FFPROBE_BIN_PATH = require('ffprobe-static').path
*/
const FFMPEG_BIN_PATH = require('ffmpeg-static').path

// configure ffmpeg/ffprobe
// initial configuration for `ffmpeg` and `ffprobe` binary paths
// consumers of `fluent-ffmpeg` can overload the path on their own
ffmpeg.setFfmpegPath(FFMPEG_BIN_PATH)
ffmpeg.setFfprobePath(FFPROBE_BIN_PATH)

Expand Down
948 changes: 0 additions & 948 deletions formats.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"text-extensions": "^2.0.0",
"through2": "^3.0.1",
"uuid": "^3.3.3",
"video-extensions": "^1.1.0"
"video-extensions": "^1.1.0",
"x264-static": "^0.1.0"
},
"devDependencies": {
"nyc": "^14.1.1",
Expand Down
9 changes: 3 additions & 6 deletions scripts/generate-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ function onread(err, buffer) {
exported.push(define(stream, demuxer[0], 'demux', demuxer[0], demuxer[1]))
}

stream.write(
`
stream.write(`
/**
* Module exports.
*/
module.exports = {
${exported.join(',\n ')}
}`.trim()
)
}`)

stream.end()

console.log('Generated %s', output)
})
}

Expand Down
28 changes: 28 additions & 0 deletions settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { FFMPEG_BIN_PATH, FFPROBE_BIN_PATH } = require('./ffmpeg')
const { MKVMERGE_BIN_PATH } = require('./mkvmerge')
const { X264_BIN_PATH } = require('./x264')

/**
* A static object to encapsulate shared settings between
* the module such as binary paths, default values, and more.
* @public
* @default
*/
const settings = {

/**
* Binary paths for various commands used internally within
* the module.
*/
bin: {
x264: X264_BIN_PATH,
ffmpeg: FFMPEG_BIN_PATH,
ffprobe: FFPROBE_BIN_PATH,
mkvmerge: MKVMERGE_BIN_PATH,
}
}

/**
* Module exports.
*/
module.exports = settings
150 changes: 139 additions & 11 deletions target.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const Resource = require('nanoresource')
const assert = require('assert')
const ready = require('nanoresource-ready')
const path = require('path')
const fs = require('fs')

/**
* Absolute path to the built-in target configuration.
* @public
*/
const TARGETS_DIR = path.resolve(__dirname, 'targets')

/**
* The `Target` class represents a nanoresource to
* @public
* @class
* @extends Resource
Expand All @@ -26,37 +34,139 @@ class Target extends Resource {
assert(name && 'string' === typeof name,
'Expecting name to be a string.')

// normalize
name = name.toLowerCase()
.replace(/_/g, '-')

this.cwd = opts.cwd || process.cwd()
this.config = new Map()
this.filename = path.resolve(this.cwd, this.name)
this.state = new Map()

if ('.' === name[0]) {
this.filename = path.resolve(this.cwd, name)
} else {
this.filename = path.resolve(TARGETS_DIR, name)
}

if ('.json' !== path.extname(this.filename)) {
this.filename += '.json'
}
}

/**
* The enabled configuration for this target. This value
* may be `null`.
* @accessor
* @type {?(Object)}
*/
get config() {
return this.state.get('config') || null
}

/**
* An array of enabled options for the target.
* @accessor
* @type {Array}
*/
get enabled() {
return this.state.get('enabled') || []
}

/**
* An object describing the limits of the target, such as
* audio and video codec limitations (bit rate, channels,
* pixel format, etc). This value can be `null`.
* @accessor
* @type {?(Object)}
*/
get limits() {
return this.config.get('limits') || null
return this.state.get('limits') || null
}

/**
* The name of this target. This can be the name defined in a
* targets configuration (if defined) falling back to the base name
* of the target filename, without the extension.
* @accessor
* @type {?(String)}
*/
get name() {
return this.config.get('name') || null
const basename = path.basename(this.filename)
const extname = path.extname(basename)
const name = this.state.get('name')
return name || basename.replace(extname, '') || null
}

/**
* Implements the abstract `_open()` method for `nanoresource`
* Reads configuration from file system and stores state on
* the instance calling `callback()` upon success or error.
* @protected
* An object containing the possible options for the target as defined
* in the target configuration. This value can be `null`.
* @accessor
* @type {?(Object)}
*/
get options() {
return this.state.get('options') || null
}

/**
* Updates the target configuration state.
* @public
* @param {Function} callback
*/
_open(callback) {
update(callback) {
fs.readFile(this.filename, (err, buffer) => {
if (err) { return callback(err) }
try {
const config = JSON.parse(buffer.toString('utf8'))
const target = JSON.parse(buffer.toString('utf8'))

if ('string' === typeof target.enabled) {
target.enabled = [target.enabled]
}

if (target.enabled && target.options) {
const config = {}
const defaults = target.options.defaults || {}

for (const enabled of target.enabled) {
const options = target.options[enabled]
config[enabled] = Object.assign({}, defaults, options)
}

this.state.set('config', config)
}

if (Array.isArray(target.enabled)) {
this.state.get('enabled', target.enabled)
}

if (target.limits && 'object' === typeof target.limits) {
this.state.set('limits', target.limits)
}

if ('string' === typeof target.name) {
this.state.set('name', target.name)
}

if (target.options && 'object' === typeof target.options) {
this.state.set('options', target.options)
}

callback(null)
} catch (err) {
return callback(err)
callback(err)
}
})
}

/**
* Implements the abstract `_open()` method for `nanoresource`
* Reads configuration from file system and stores state on
* the instance calling `callback()` upon success or error.
* @protected
* @param {Function} callback
*/
_open(callback) {
this.update(callback)
}

/**
* Implements the abstract `_close()` method for `nanoresource`
* Will clear internal state calling `callback()` upon success
Expand All @@ -68,4 +178,22 @@ class Target extends Resource {
this.config.clear()
process.nextTick(callback, null)
}

/**
* Waits for target to be "ready" calling `callback()` upon
* success or error.
* @param {Function} callback
*/
ready(callback) {
ready(this, callback)
}
}

/**
* Module exports.
*/
module.exports = {
TARGETS_DIR,

Target
}
2 changes: 1 addition & 1 deletion targets/ps4.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "ps4",
"enabled": [ "hls" ],
"options": {
"all": {
"defaults": {
"audio": {
"channels": 2,
"codec": [
Expand Down
11 changes: 11 additions & 0 deletions x264.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Static `x264(1) binary path.
*/
const X264_BIN_PATH = require('x264-static').path

/**
* Module exports.
*/
module.exports = {
X264_BIN_PATH
}

0 comments on commit 35924cd

Please sign in to comment.