Skip to content

Commit

Permalink
refactor(audioconv): Improve option handling and flexibility
Browse files Browse the repository at this point in the history
- Updated `resolveOptions` to return `defaultOptions` when the input is an empty object or not an object.
- Set the default value of `useDefault` parameter in `resolveOptions` to false.
- Refined `convertAudio` function to apply `ffmpeg` audio options only when specified, allowing for more flexible conversion with minimal configurations.
  • Loading branch information
mitsuki31 committed Aug 24, 2024
1 parent c2bc654 commit 8fb46a5
Showing 1 changed file with 17 additions and 8 deletions.
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 @@ const {
createDirIfNotExistSync,
createLogFile,
dropNullAndUndefined,
isNullOrUndefined
isNullOrUndefined,
isObject
} = require('./utils');

/**
Expand Down Expand Up @@ -193,7 +194,9 @@ function splitOptions(options) {
* @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 @@ async function convertAudio(inFile, options = defaultOptions) {
|| (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);

// 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);
} else {
if (Array.isArray(options.inputOptions)) {
ffmpegChain.inputOptions(options.inputOptions);
Expand Down

0 comments on commit 8fb46a5

Please sign in to comment.