Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance FFmpeg option handling and add new flexibility #41

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/audioconv.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
createDirIfNotExistSync,
createLogFile,
dropNullAndUndefined,
isNullOrUndefined
isNullOrUndefined,
isObject
} = require('./utils');

/**
Expand Down Expand Up @@ -193,7 +194,9 @@
* @return {module:audioconv~ResolvedConvertAudioOptions} The resolved options.
* @since 1.0.0
*/
function resolveOptions(options, useDefault=true) {
function resolveOptions(options, useDefault=false) {
if (!isObject(options)) return defaultOptions;

return {
inputOptions: (
Array.isArray(options?.inputOptions)
Expand Down Expand Up @@ -457,12 +460,18 @@
|| (Array.isArray(options.outputOptions) && options.outputOptions.length === 0)
)
) {
ffmpegChain
.audioBitrate(options.bitrate)
.audioCodec(options.codec)
.audioChannels(options.channels)
.audioFrequency(options.frequency)
.outputFormat(options.format);
// Add each option only if present and specified
// By using this logic, now user can convert an audio file with only specifying
// the output audio format
if (options.bitrate) ffmpegChain.audioBitrate(options.bitrate);
if (options.codec) ffmpegChain.audioCodec(options.codec);
if (options.channels) ffmpegChain.audioChannels(options.channels);
if (options.frequency) ffmpegChain.audioFrequency(options.frequency);

Check warning on line 469 in lib/audioconv.js

View check run for this annotation

Codecov / codecov/patch

lib/audioconv.js#L466-L469

Added lines #L466 - L469 were not covered by tests

// Mandatory for output format
// Note: Specifying the same output format as input may copy the input file
// with the same codec and format
ffmpegChain.outputFormat(options.format);

Check warning on line 474 in lib/audioconv.js

View check run for this annotation

Codecov / codecov/patch

lib/audioconv.js#L474

Added line #L474 was not covered by tests
} else {
if (Array.isArray(options.inputOptions)) {
ffmpegChain.inputOptions(options.inputOptions);
Expand Down
11 changes: 6 additions & 5 deletions test/unittest/audioconv.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('module:audioconv', function () {
],
resolveOptions: [
'should return default options if given argument is nullable value',
'should return nullable options if given argument is nullable value',
'should return default options if given argument is not an object',
'should resolve the given configuration options'
]
};
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('module:audioconv', function () {
});

it(testMessages.resolveOptions[0], function () {
const actualOptions = [null, undefined, {}].map((val) => {
const actualOptions = [null, undefined, false].map((val) => {
return audioconv.resolveOptions(val , true);
});

Expand All @@ -110,15 +110,16 @@ describe('module:audioconv', function () {
});

it(testMessages.resolveOptions[1], function () {
const actualOptions = [null, undefined, {}].map((val) => {
const actualOptions = ['&', /y/, 16n].map((val) => {
return audioconv.resolveOptions(val , false);
});

actualOptions.forEach((actual) => {
assert.notStrictEqual(actual, null); // Non-nullable
assert.notStrictEqual(typeof actual, 'undefined');
assert.notDeepStrictEqual(actual, expectedOptions[0]);
assert.deepStrictEqual(actual, expectedOptions[2]);
assert.deepStrictEqual(actual, expectedOptions[0]);
assert.notDeepStrictEqual(actual, expectedOptions[1]);
assert.notDeepStrictEqual(actual, expectedOptions[2]);
});
[...actualOptions].reverse().forEach((actual1) => {
actualOptions.forEach((actual2) => {
Expand Down