Skip to content

Commit 2d8bc48

Browse files
committed
feat: Make sox command as a variable, so sox_ng or docker images could be used
1 parent 430b70e commit 2d8bc48

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ This is a Bash script for transcoding and downsampling 24-bit FLAC files to 16-b
1919
- Install on macOS: `brew install sox`
2020
- Install on Windows: Use WSL and install depending on the subsystem.
2121

22+
You can also use alternative SoX implementations:
23+
- Docker image: `bigpapoo/sox`
24+
- SoX-NG: A drop-in replacement for SoX ([SoX-NG Project](https://codeberg.org/sox_ng/sox_ng/))
25+
- My Docker image for SoX-NG: `ardakilic/sox_ng`. Source code of the Image is [here](https://github.com/Ardakilic/sox_ng_dockerized).
26+
2227
## Usage
2328

2429
Run the script with the source directory as an argument:
@@ -32,6 +37,10 @@ Run the script with the source directory as an argument:
3237
- `--target-dir <dir>` : Specify the target directory (default: `./transcoded`).
3338
- `--copy-images` : Copy JPG and PNG image files.
3439

40+
### Environment Variables:
41+
42+
- `SOX_COMMAND`: Override the default `sox` command (default: "sox")
43+
3544
### Example Usage
3645

3746
Convert FLAC files from `Music/HiRes` and store the transcoded files in `Music/Converted`:
@@ -46,6 +55,20 @@ Convert FLAC files and copy images:
4655
./flac-converter.sh Music/HiRes --copy-images
4756
```
4857

58+
Using with Docker:
59+
60+
```bash
61+
SOX_COMMAND="docker run -v $(pwd):/work --rm bigpapoo/sox mysox" ./flac-converter.sh Music/HiRes
62+
# or my builds of sox_ng, which is better maintained and up-to-date
63+
SOX_COMMAND="docker run --rm -v $(pwd):/audio ardakilic/sox_ng" ./flac-converter.sh Music/HiRes
64+
```
65+
66+
Using with SoX-NG directly:
67+
68+
```bash
69+
SOX_COMMAND="sox_ng" ./flac-converter.sh Music/HiRes
70+
```
71+
4972
## How It Works
5073

5174
1. The script scans the source directory for `.flac` and `.mp3` files.

flac-converter.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# Copyright (C) 2025 Arda Kilicdagi
99
# Licensed under MIT License
1010

11+
# Define the sox command - can be overridden via environment variable
12+
: "${SOX_COMMAND:=sox}"
13+
1114
# Function to display usage
1215
show_usage() {
1316
echo "Usage: $0 <source_directory> [options]"
@@ -53,8 +56,8 @@ while [[ $# -gt 0 ]]; do
5356
done
5457

5558
# Check if sox is installed
56-
if ! command -v sox &> /dev/null; then
57-
echo "Error: sox is not installed. Please install sox to continue."
59+
if ! command -v ${SOX_COMMAND%% *} &> /dev/null; then
60+
echo "Error: sox (or Docker) is not installed. Please ensure it is installed to continue."
5861
exit 1
5962
fi
6063

@@ -70,7 +73,7 @@ mkdir -p "$TRANSCODED_DIR"
7073
# Function to get audio file info using sox
7174
get_audio_info() {
7275
local file="$1"
73-
local info=$(sox --i "$file")
76+
local info=$($SOX_COMMAND --i "$file")
7477
local bits=$(echo "$info" | grep "Sample Encoding" | grep -o "[0-9]\+")
7578
local rate=$(echo "$info" | grep "Sample Rate" | grep -o "[0-9]\+")
7679
echo "$bits $rate"
@@ -127,9 +130,9 @@ find "$SOURCE_DIR" \( -name "*.flac" -o -name "*.mp3" \) | while read -r file; d
127130
if [ "$needs_conversion" = true ]; then
128131
echo "Converting FLAC: $file"
129132
# Debugging
130-
# echo "sox --multi-threaded -G '$file' $bitrate_args '$target_file' $sample_rate_args dither"
133+
# echo "$SOX_COMMAND --multi-threaded -G '$file' $bitrate_args '$target_file' $sample_rate_args dither"
131134
# shellcheck disable=SC2086
132-
sox --multi-threaded -G "$file" $bitrate_args "$target_file" $sample_rate_args dither
135+
$SOX_COMMAND --multi-threaded -G "$file" $bitrate_args "$target_file" $sample_rate_args dither
133136
else
134137
echo "Copying FLAC: $file"
135138
cp "$file" "$target_file"

0 commit comments

Comments
 (0)