audioconv
is an abbreviation for Audio Converter, this module
provides a function to convert audios to any supported format utilizing the
fluent-ffmpeg
module and ffmpeg
library on the system.
To convert the audio file, this module needs ffmpeg
to be installed.
You can download it from FFmpeg official site.
Or if you want to use CLI:
Using apt
(for Linux)
$ sudo apt install ffmpeg
Using Chocolatey (for Windows)
*Make sure to Run as Administrator
PS > choco install ffmpeg-full -y
- Since:
- 0.2.0
- License:
- MIT
- Source:
Requires
Members
(inner, constant) defaultOptions :Readonly.<module:audioconv~ResolvedConvertAudioOptions>
Default options of audio converter options.
This default options will convert the audio to the MP3 format with bitrate of 128 kbps, frequency of 44100 Hz (Hertz), stereo channel and use the default MP3 codec.
If you want to delete the old audio file after conversion, set the
deleteOld
option to true
.
Type:
- Readonly.<module:audioconv~ResolvedConvertAudioOptions>
- Since:
- 0.2.0
- Source:
Methods
(async, inner) checkFfmpeg(verbose) → {Promise.<boolean>}
Checks whether the ffmpeg
binary is installed on system or not.
First, it checks if the FFMPEG_PATH
environment variable is set. If it is set, it returns true
.
Otherwise, if not set, it checks if the ffmpeg
binary is installed on system by directly executing it.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
verbose |
boolean | false | Whether to log verbose messages or not. |
- Since:
- 1.0.0
- Source:
Returns:
true
if the ffmpeg
binary installed on system; otherwise, false
.
- Type
- Promise.<boolean>
(async, inner) convertAudio(inFile, optionsopt)
Converts an audio file to a specified format using the given options.
Before performing audio conversion, it first checks the ffmpeg
binary by
searching on the FFMPEG_PATH
environment variable, if set. Otherwise, it
force check by calling the ffmpeg
command itself on child process.
If the ffmpeg
is not installed on the system, this function will aborts
immediately and rejects with an error.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
inFile |
string | The input file path of the audio file to be converted. |
||
options |
ConvertAudioOptions |
<optional> |
defaultOptions | Options object for configuring the conversion process. |
- Since:
- 0.2.0
- Source:
- See:
Throws:
-
If the input audio file is not exist or if there is an error occurred during audio conversion.
- Type
- Error
Example
convertAudio('path/to/audio.wav', { format: 'mp3', bitrate: '192k' })
.then(() => console.log('Conversion complete'))
.catch(err => console.error('Conversion failed:', err));
(package, inner) createConversionProgress(info, extnames) → {string}
Creates a string representing the progress bar for audio conversion progress.
Parameters:
Name | Type | Description |
---|---|---|
info |
FFmpegInfo | The progress data from FFmpeg. |
extnames |
Array.<string> | A list of extension names of both input and output files. |
- Since:
- 1.0.0
- Source:
Returns:
A formatted string representing the progress bar with percentage.
- Type
- string
(package, inner) resolveOptions(options) → {module:audioconv~ResolvedConvertAudioOptions}
Resolves the given ConvertAudioOptions
options.
Parameters:
Name | Type | Description |
---|---|---|
options |
ConvertAudioOptions | The unresolved audio converter options. |
- Since:
- 1.0.0
- Source:
Returns:
The resolved options.
(package, inner) splitOptions(options) → {Array.<string>}
Splits and resolves FFmpeg options from a string or array format into an array of individual options.
This function handles both single string input, where options are space-separated, and array input. It correctly pairs options with their respective values and avoids accidental concatenation with subsequent options.
Parameters:
Name | Type | Description |
---|---|---|
options |
string | Array.<string> | The options to split, either as a string or an array. |
- Since:
- 1.0.0
- Source:
Returns:
The resolved options as an array of individual options.
- Type
- Array.<string>
Example
const optionsStr = '-f -vcodec libx264 -preset slow';
const result1 = splitOptions(optionsStr);
// Output: ['-f', '-vcodec libx264', '-preset slow']
(async, package, inner) writeErrorLog(logFile, data, erroropt) → {Promise.<void>}
Writes error details and associated video information to a log file.
The error message is written to the log file in the following format:
[ERROR]<ACONV> <error message>
Input Audio: <input audio name>
Output Audio: <output audio name>
File Size: <input audio size> MiB
---------------------------------------------
Generated log file will be saved in
directory
with file name typically prefixed with LOGDIR
'audioConvError'
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
logFile |
string | The name of the log file where the error details should be written. |
|
data |
Object | An object containing information about the audio associated with the error. |
|
error |
Error |
<optional> |
The error object, optional. If not provided,
an error message will be |
- Since:
- 1.0.0
- Source:
Returns:
- Type
- Promise.<void>
Type Definitions
FFmpegInfo
An object representing the information data when FFmpeg emits the 'progress'
event.
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
frames |
number | Total processed frame count. |
currentFps |
number | Framerate at which FFmpeg is currently processing. |
currentKbps |
number | Throughput at which FFmpeg is currently processing. |
targetSize |
number | Current size of the target file in kilobytes. |
timemark |
number | The timestamp of the current frame in seconds. |
percent |
number | An estimation of the progress percentage, may be (very) inaccurate. |
- Since:
- 1.1.0
- Source:
- See:
ResolvedConvertAudioOptions
The resolved ConvertAudioOptions
options.
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
inputOptions |
Array.<string> | The input options for the conversion. |
outputOptions |
Array.<string> | The output options for the conversion. |
format |
string | The desired output format (e.g., |
bitrate |
string | number | The audio bitrate (e.g., |
frequency |
number | The audio sampling frequency in Hz. |
codec |
string | The audio codec to use (e.g., |
channels |
number | The number of audio channels ( |
deleteOld |
boolean | Whether to delete the original file after conversion. |
quiet |
boolean | Whether to suppress the conversion progress and error message or not. |
- Since:
- 1.0.0
- Source:
- See: