Skip to content

Commit

Permalink
refactor(): module clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Jan 13, 2020
1 parent 5f1b9a8 commit 92ac5dd
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 128 deletions.
4 changes: 2 additions & 2 deletions configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function configure(opts) {
}

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

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

Expand Down
36 changes: 0 additions & 36 deletions example/basic/index.js

This file was deleted.

Binary file removed example/basic/test.mp4
Binary file not shown.
142 changes: 59 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,60 @@
const Asset = require('./asset')
const Source = require('./source')
const Target = require('./target')
const { AudioTrack, SubtitleTrack, VideoTrack, Track } = require('./track')

const nanoprocess = require('nanoprocess')
const path = require('path')
const uuidv4 = require('uuid/v4')

class Delivery {
constructor(sources, opts = {}) {
if (sources instanceof Source) {
this.sources = [sources]
} else if (Array.isArray(sources) && sources.every(s => s instanceof Source)) {
this.sources = sources
} else {
throw new Error('Invalid sources provided')
}
this.opts = opts
}
}

class Package {
constructor(tracks, opts = {}) {
const opt = opts //copy
this.targets = []
this.uuid = uuidv4()
if (tracks instanceof Track) {
this.tracks = [tracks]
} else if (Array.isArray(tracks) && tracks.every(t => t instanceof Track)) {
this.tracks = tracks.sort((track1, track2) => {
return track1.properties.index - track2.properties.index
})
} else {
throw new Error('Invalid tracks provided')
}

if (opt.targets) {
if (opt.targets instanceof Target) {
this.targets = [opt.targets]
} else if (Array.isArray(opt.targets) &&
opt.targets.every(t => t instanceof Target)
) {
this.targets.push(opt.targets)
}
}

this.opts = opt

this.muxes = []
this.demuxes = []
}
mux(options = {}) {
return new Promise((resolve, reject) => {
const opts = options //copy
const outputUrl = opts.outputUrl || 'mux_output.mkv'

const tracks = this.tracks //copy
const cmdOpts = ['-o', outputUrl, tracks.shift().source.uri]
cmdOpts.push(
...tracks.filter(t => t.source.uri !== cmdOpts[2]).map(m => `+${m.source.uri}`)
)
const muxCmd = nanoprocess('mkvmerge', cmdOpts)

muxCmd.open((err) => {
if (err) { reject(err) }
muxCmd.process.on('close', (exitCode) => {
console.log('closed mux command with code', exitCode)
if (exitCode == 0) {
this.muxes.push(outputUrl)
resolve(outputUrl)
} else {
reject('mkvmerge failed with exit code', exitCode)
}
})
})
})
}
assignTargets(targets) {
this.targets.push(targets)
}
const { ffmpeg, FFPROBE_BIN_PATH, FFMPEG_BIN_PATH } = require('./ffmpeg')
const { createDemuxStream, demux } = require('./demux')
const { MKVMERGE_BIN_PATH } = require('./mkvmerge')
const { X264_BIN_PATH } = require('./x264')
const { configure } = require('./configure')
const { Delivery } = require('./delivery')
const extensions = require('./extensions')
const { Source } = require('./source')
const { Target } = require('./target')
const { Asset } = require('./asset')
const constants = require('./constants')
const settings = require('./settings')
const { mux } = require('./mux')
const targets = require('./targets')
const iso639 = require('./iso-639')

const {
AudioTrack,
SubtitleTrack,
Track,
TrackError,
TrackPropertiesError,
TrackPropertiesMissingFormatError,
TrackPropertiesMissingStreamError,
TrackValidationError,
VideoTrack
} = require('./track')

/**
* Module exports.
*/
module.exports = {
Asset,
AudioTrack,
configure,
constants,
createDemuxStream,
Delivery,
demux,
extensions,
ffmpeg,
FFPROBE_BIN_PATH,
FFMPEG_BIN_PATH,
iso639,
MKVMERGE_BIN_PATH,
mux,
settings,
Source,
SubtitleTrack,
Target,
targets,
Track,
TrackError,
TrackPropertiesError,
TrackPropertiesMissingFormatError,
TrackPropertiesMissingStreamError,
TrackValidationError,
VideoTrack,
X264_BIN_PATH,
}

module.exports = { Asset, AudioTrack, Delivery, Package, Source, SubtitleTrack, Target, Track, VideoTrack }
7 changes: 0 additions & 7 deletions mux.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ function mux(sources, opts, callback) {
})
}

/**
* @TODO
* @public
*/
function createMuxStream() {
}

/**
* Module exports.
*/
Expand Down
14 changes: 14 additions & 0 deletions targets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mobile = require('./mobile.json')
const ps4 = require('./ps4.json')
const web = require('./web.json')
const vr = require('./vr.json')

/**
* Module exports.
*/
module.exports = {
mobile,
ps4,
vr,
web
}
32 changes: 32 additions & 0 deletions targets/mobile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"limits": {
"codec": {
"audio": {
},
"video": {
}
}
},
"name": "mobile",
"enabled": [ ],
"options": {
"defaults": {
"audio": {
"channels": 2,
"codec": [
"aac"
],
"sampleRate": 48000
},
"container": {
"trackCount": {
"video": 1,
"audio": 1
}
},
"video": {
"codec": [ "h264_nvenc", "libx264" ]
}
}
}
}
32 changes: 32 additions & 0 deletions targets/vr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"limits": {
"codec": {
"audio": {
},
"video": {
}
}
},
"name": "vr",
"enabled": [ ],
"options": {
"defaults": {
"audio": {
"channels": 2,
"codec": [
"aac"
],
"sampleRate": 48000
},
"container": {
"trackCount": {
"video": 1,
"audio": 1
}
},
"video": {
"codec": [ "h264_nvenc", "libx264" ]
}
}
}
}
32 changes: 32 additions & 0 deletions targets/web.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"limits": {
"codec": {
"audio": {
},
"video": {
}
}
},
"name": "web",
"enabled": [ ],
"options": {
"defaults": {
"audio": {
"channels": 2,
"codec": [
"aac"
],
"sampleRate": 48000
},
"container": {
"trackCount": {
"video": 1,
"audio": 1
}
},
"video": {
"codec": [ "h264_nvenc", "libx264" ]
}
}
}
}

0 comments on commit 92ac5dd

Please sign in to comment.