Skip to content

Commit db80877

Browse files
authored
feat: Add --enforce-output-format flag with FLAC, MP3, and ALAC support (#14)
* feat: enforce output format * chore: address copilot feedback
1 parent 8c95ef6 commit db80877

File tree

4 files changed

+1167
-16
lines changed

4 files changed

+1167
-16
lines changed

Development.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,41 @@ Added comprehensive support for Apple Lossless Audio Codec (ALAC) files:
298298
- Added comprehensive unit tests for ALAC functionality
299299

300300
**Dependencies:**
301+
- FFmpeg is now required for ALAC support and metadata preservation
302+
- FFmpeg dependency is automatically detected when ALAC files are present
303+
- Maintains backward compatibility - projects with only FLAC files still work with SoX alone
304+
305+
### Format Enforcement Feature
306+
307+
Added `--enforce-output-format` flag for converting all audio files to a specific output format:
308+
309+
**Features:**
310+
- Supports three output formats: `flac`, `mp3`, and `alac`
311+
- Intelligent conversion logic that avoids unnecessary re-encoding when possible
312+
- Preserves audio quality appropriately for each target format
313+
- Maintains folder structure and file organization
314+
315+
**Technical Implementation:**
316+
- New `processAudioFileWithEnforcedFormat()` function handles format enforcement logic
317+
- Separate processing functions for each target format:
318+
- `processToFLAC()`: Converts all files to 16-bit FLAC
319+
- `processToMP3()`: Converts to 320kbps MP3 with intelligent sample rate handling
320+
- `processToALAC()`: Converts to 16-bit ALAC format
321+
- Added helper functions for format conversion:
322+
- `convertMP3ToFLAC()`: MP3 to FLAC conversion using SoX
323+
- `convertToMP3()`: Multi-format to MP3 conversion with 320kbps quality
324+
- `convertToALAC()`: Multi-format to ALAC conversion using FFmpeg
325+
- File extension management with new helper functions:
326+
- `changeExtensionToMP3()`: Updates file paths for MP3 output
327+
- `changeExtensionToM4A()`: Updates file paths for ALAC output
328+
329+
**Conversion Logic:**
330+
- **FLAC mode**: Optimizes existing 16-bit FLAC files, converts all others to 16-bit FLAC
331+
- **MP3 mode**: Preserves existing MP3 files, converts FLAC/ALAC to 320kbps MP3
332+
- **ALAC mode**: Optimizes existing 16-bit ALAC files, converts all others to 16-bit ALAC
333+
- Sample rate handling preserves audio characteristics (48kHz family stays 48kHz, 44.1kHz family stays 44.1kHz)
334+
335+
## Testing
301336
- FFmpeg is now required for both local and Docker execution due to ALAC support
302337
- Updated error messages to reflect FFmpeg requirement
303338
- Docker image already includes FFmpeg support

README.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ Lilt stands for "lightweight intelligent lossless transcoder". It is also a form
1212
- 🍎 **ALAC Support**: Converts ALAC (.m4a) files to FLAC format
1313
- 16-bit 44.1kHz/48kHz ALAC files are converted to FLAC with the same quality
1414
- Hi-Res ALAC files are converted to 16-bit FLAC following the same rules as FLAC files
15-
- 📉 Downsamples high sample rate files:
15+
-**Format Enforcement**: Convert all audio files to a specific output format:
16+
- **FLAC**: Convert all FLAC, ALAC, and MP3 files to 16-bit FLAC
17+
- **MP3**: Convert all FLAC and ALAC files to 320kbps MP3 (preserves existing MP3 files)
18+
- **ALAC**: Convert all FLAC and MP3 files to 16-bit ALAC (optimizes existing ALAC files)
19+
- �📉 Downsamples high sample rate files:
1620
- 384kHz, 192kHz, or 96kHz → 48kHz
1721
- 352.8kHz, 176.4kHz, 88.2kHz → 44.1kHz
1822
- 🔄 Preserves existing 16-bit FLAC files without unnecessary conversion
1923
- 📝 Preserves ID3 tags and cover art from original files using FFmpeg (default: enabled; use --no-preserve-metadata to disable)
20-
- 🎶 Copies MP3 files without modification
24+
- 🎶 Copies MP3 files without modification (unless format enforcement is enabled)
2125
- 🖼️ Optional: Copies JPG and PNG images from the source directory
2226
- 🐳 Docker support for containerized execution
2327
- 💻 Cross-platform: Windows, macOS, Linux (x64, ARM64, x86, ARM)
@@ -85,12 +89,13 @@ lilt <source_directory> [options]
8589
### Options:
8690

8791
```
88-
--target-dir <dir> Specify target directory (default: ./transcoded)
89-
--copy-images Copy JPG and PNG files
90-
--no-preserve-metadata Do not preserve ID3 tags and cover art using FFmpeg (default: false)
91-
--use-docker Use Docker to run Sox instead of local installation
92-
--docker-image <img> Specify Docker image (default: ardakilic/sox_ng:latest)
93-
--self-update Check for updates and self-update if newer version available
92+
--target-dir <dir> Specify target directory (default: ./transcoded)
93+
--copy-images Copy JPG and PNG files
94+
--no-preserve-metadata Do not preserve ID3 tags and cover art using FFmpeg (default: false)
95+
--enforce-output-format <fmt> Enforce output format for all files: flac, mp3, or alac
96+
--use-docker Use Docker to run Sox instead of local installation
97+
--docker-image <img> Specify Docker image (default: ardakilic/sox_ng:latest)
98+
--self-update Check for updates and self-update if newer version available
9499
```
95100

96101
### Examples:
@@ -113,6 +118,24 @@ lilt.exe "C:\Music\MyAlbum" --target-dir "C:\Music\MyAlbum-16bit" --use-docker
113118
./lilt ~/Music/MyAlbum --target-dir ~/Music/MyAlbum-16bit --use-docker
114119
```
115120

121+
Convert all files to MP3:
122+
```bash
123+
# Windows
124+
lilt.exe "C:\Music\MyAlbum" --enforce-output-format mp3 --target-dir "C:\Music\MyAlbum-MP3"
125+
126+
# macOS/Linux
127+
./lilt ~/Music/MyAlbum --enforce-output-format mp3 --target-dir ~/Music/MyAlbum-MP3
128+
```
129+
130+
Convert all files to ALAC:
131+
```bash
132+
# Windows
133+
lilt.exe "C:\Music\MyAlbum" --enforce-output-format alac --target-dir "C:\Music\MyAlbum-ALAC"
134+
135+
# macOS/Linux
136+
./lilt ~/Music/MyAlbum --enforce-output-format alac --target-dir ~/Music/MyAlbum-ALAC
137+
```
138+
116139
Check for updates:
117140
```bash
118141
lilt --self-update
@@ -139,6 +162,8 @@ Alternative Docker images you can use:
139162

140163
## How It Works
141164

165+
### Default Behavior (without --enforce-output-format)
166+
142167
1. The tool scans the source directory recursively for `.flac`, `.m4a` (ALAC), and `.mp3` files
143168
2. **For FLAC files:**
144169
- If a FLAC file is **24-bit**, it is converted to **16-bit** using SoX
@@ -154,6 +179,26 @@ Alternative Docker images you can use:
154179
6. If `--copy-images` is enabled, `.jpg` and `.png` files are copied to the target directory
155180
7. The original folder structure is preserved in the target directory
156181

182+
### Format Enforcement Mode (with --enforce-output-format)
183+
184+
When using `--enforce-output-format`, all audio files are converted to the specified format:
185+
186+
#### FLAC Mode (`--enforce-output-format flac`)
187+
- **FLAC files**: Converted to 16-bit FLAC if needed, or copied if already 16-bit
188+
- **ALAC files**: Converted to 16-bit FLAC
189+
- **MP3 files**: Copied as-is (MP3 files are not converted to lossless formats)
190+
191+
#### MP3 Mode (`--enforce-output-format mp3`)
192+
- **FLAC files**: Converted to 320kbps MP3
193+
- **ALAC files**: Converted to 320kbps MP3
194+
- **MP3 files**: Copied without modification
195+
- Sample rate is intelligently preserved (48kHz family → 48kHz, 44.1kHz family → 44.1kHz)
196+
197+
#### ALAC Mode (`--enforce-output-format alac`)
198+
- **FLAC files**: Converted to 16-bit ALAC (.m4a)
199+
- **MP3 files**: Copied as-is (MP3 files are not converted to lossless formats)
200+
- **ALAC files**: Converted to 16-bit ALAC if needed, or copied if already 16-bit
201+
157202
## Technical Details
158203

159204
- Written in Go for excellent cross-platform compatibility and performance

0 commit comments

Comments
 (0)