Node.js CLI tools for audio and subtitle processing:
- ποΈ SRT Formatter (
srt-toolkit): Validate and process subtitle files with template variables - π Silence Detector (
silence-detector): Detect silence periods in audio files using FFmpeg - βοΈ Audio Splitter (
audio-splitter): Split audio files based on silence detection
This is a Node.js command-line tool to validate, format, and process template variables in SubRip Subtitle (.srt) files.
- SRT Validation and Auto-Formatting:
- Automatically re-numbers segments sequentially.
- Validates timecode format (
HH:MM:SS,mmm --> HH:MM:SS,mmm). - Checks for logical time errors (start time >= end time).
- Detects and warns about overlapping segments.
- Auto-fixes segments with a missing end time by using the start time of the next segment.
- Variable Processing:
- Finds and replaces template variables (e.g.,
{{variable_name}}) using a provided JSON file. - Supports Unicode characters in variable names.
- Automatically discovers new variables in the SRT file and adds them to the JSON file for future use.
- Finds and replaces template variables (e.g.,
node srt-toolkit.js <input-file.srt> [--variables <variable-file.json>] [--output <output-file.srt>]input-file.srt: (Required) The path to the source SRT file.--variables,-v: (Optional) Path to a JSON file containing variable definitions. If provided, variable processing is enabled.--output,-o: (Optional) Path for the formatted output file. If this is not provided, the tool will overwrite the input file.
Given the following files:
input.srt:
12
00:01:10,500 --> 00:01:13,000
Hello, {{user}}! Welcome to {{location}}.
6
00:01:15,000
This is a test subtitle.
2
00:01:16,500 --> 00:01:10,000
This timecode is invalid.
vars.json:
{
"user": "Alice"
}node srt-toolkit.js input.srt -v vars.json -o formatted.srtFormatted SRT file saved to formatted.srt
Updated variables file saved to /path/to/your/project/vars.json
Substituted 1 variables. Added 1 new variables to JSON.
Processed 3 segments.
Errors:
- Segment 3 has an invalid timecode (start >= end).
Warnings:
- Segment 2 was missing a start or end time. Used previous segment's end time and next segment's start time.
formatted.srt:
1
00:01:10,500 --> 00:01:13,000
Hello, Alice! Welcome to {{location}}.
2
00:01:13,000 --> 00:01:16,500
This is a test subtitle.
3
00:01:16,500 --> 00:01:10,000
This timecode is invalid.
vars.json (Updated):
{
"user": "Alice",
"location": "location"
}This project uses Jest for unit testing. To run the tests, follow these steps:
-
Install dependencies: If you haven't already, install the necessary development dependencies.
npm install
-
Run tests: Execute the test suite with the following command:
npm test
The tests will run, and you will see the output in your console, indicating whether all tests passed or if any of them failed.
Detect silence periods in audio files using FFmpeg and save the silence end times to a text file.
- FFmpeg: Must be installed and available in the system PATH
- Download from https://ffmpeg.org/download.html
- Verify installation:
ffmpeg -version
- Detects silence periods in audio files using FFmpeg's silencedetect filter
- Configurable noise threshold and minimum silence duration
- Saves silence end times to a text file for use with other tools
- Real-time progress feedback during processing
node silence-detector.js <input-audio-file> [options]input-audio-file: (Required) Path to the input audio file (e.g.,input.mp3)
--output,-o <file>: Output file for silence end times (default:silence_ends.txt)--threshold,-t <dB>: Noise threshold in dB (default:-45)--duration,-d <sec>: Minimum silence duration in seconds (default:0.5)
# Basic usage with default settings
node silence-detector.js input.mp3
# Custom output file and settings
node silence-detector.js input.mp3 -o my_silence.txt -t -40 -d 1.0
# More sensitive detection (lower threshold, shorter duration)
node silence-detector.js input.mp3 --output silence.txt --threshold -50 --duration 0.3The script will:
- Analyze the audio file for silence periods
- Save silence end times (in seconds) to the specified output file
- Display a summary of detected silence periods
- Show the first 10 silence end times with formatted timestamps
Example output:
π΅ Silence Detection - Analyzing audio file
π Input file: input.mp3
π Output file: silence_ends.txt
π Noise threshold: -45 dB
β±οΈ Minimum silence duration: 0.5 seconds
βΆοΈ Executing FFmpeg...
β
Silence detection completed!
π Found 24 silence end times
π Results saved to: silence_ends.txt
π Silence end times (first 10):
1. 101.913146s (1:41.91)
2. 106.800729s (1:46.80)
3. 154.706042s (2:34.71)
...
You can also use the npm script:
npm run detect input.mp3Automatically generate FFmpeg commands to split audio files based on silence detection with minimum interval requirements.
- FFmpeg: Must be installed and available in the system PATH
- Download from https://ffmpeg.org/download.html
- or install it using Homebrew on macOS
- Verify installation:
ffmpeg -version
- Reads silence end times from
silence_ends.txt - Ensures minimum 5-minute intervals between splits
- Generates properly formatted FFmpeg commands
- Supports any audio format that FFmpeg can handle
node audio-splitter.js <input-audio-file> [output-prefix] [options]input-audio-file: (Required) Path to the input audio file (e.g.,input.mp3)output-prefix: (Optional) Prefix for output files (default:output_part)
--dry-run,-n: (Optional) Print the FFmpeg command without executing it--min-interval,-m <minutes>: (Optional) Minimum interval in minutes (default: 5)--silence-file,-s <file>: (Optional) Silence file path (default:silence_ends.txt)
# Basic usage
node audio-splitter.js input.mp3 my_audio_part
# With custom silence file and minimum interval
node audio-splitter.js input.mp3 my_audio_part -s custom_silence.txt -m 10
# Dry run with custom settings
node audio-splitter.js input.mp3 my_audio_part --silence-file silence.txt --min-interval 7 --dry-runThis will generate an FFmpeg command that splits input.mp3 into multiple files with the prefix my_audio_part1.mp3, my_audio_part2.mp3, etc.
The script will output a ready-to-use FFmpeg command like:
ffmpeg -i input.mp3 -ss 00:00:00 -to 00:09:35 -c copy output_part1.mp3 \
-ss 00:09:35 -to 00:16:06 -c copy output_part2.mp3 \
-ss 00:16:06 -to 00:21:09 -c copy output_part3.mp3 \
-ss 00:21:09 -to 00:39:20 -c copy output_part4.mp3You can also use the npm script:
npm run split input.mp3Useful FFmpeg commands for audio and video processing:
ffmpeg -i input.mp4 -vf subtitles=subtitle.srt output_srt.mp4[Parsed_subtitles_0 @ 0x10e604f10] Error opening font: '/System/Library/PrivateFrameworks/FontServices.framework/Resources/Reserved/PingFangUI.ttc', 0
How to resolve?
- In MacOS, open the Application
Font Book, try to download and install a proper Chinese font family over there, and try again. - you can also enforce the Chinese typefont like this:
ffmpeg -i input-video.mp4 -vf subtitles=subtitle-file.srt:force_style='FontName=PingFang HK' output-video.mp4(good for quick testing)
ffmpeg -i input.mp4 -i subtitle.srt -c copy -c:s mov_text -metadata:s:s:0 language=chi ouptut_chi.mp4ffmpeg -i input.mp4 output_audio.mp3ffmpeg -i output_audio.mp3 -af silencedetect=n=-45dB:d=0.5 -f null - 2>&1 | grep "silence_end" | awk '{print $5}' > silence_ends.txtn=-45dBnoise tolerance level (audio below this level is considered silent) (in dB)
d=0.5 minimum duration of silence to be detected (in seconds)
ffmpeg -i output.mp3 -ss 00:00:00 -to 00:05:00 -c copy output_part1.mp3 \
-ss 00:05:00 -to 00:10:00 -c copy output_part2.mp3We can add more -ss, -to, and output file combinations to split the audio into as many custom segments as required.
ffmpeg -i input-video.mp4 -i input-audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output-video-with-audio.mp4