Skip to content

Commit 8b21e00

Browse files
authored
Merge pull request #801 from TheCaptain989/lidarr-flac2mp3
lidarr: flac2mp3 release 2.3.2
2 parents 3432605 + f9088e2 commit 8b21e00

File tree

2 files changed

+119
-40
lines changed

2 files changed

+119
-40
lines changed

README.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,32 @@ Development Container info:
1616

1717
2. Configure the Docker container with all the port, volume, and environment settings from the *original container documentation* here:
1818
**[linuxserver/lidarr](https://hub.docker.com/r/linuxserver/lidarr "Docker container")**
19-
1. Add a **DOCKER_MODS** environment variable to the `docker run` command, as follows:
20-
- Stable release: `-e DOCKER_MODS=linuxserver/mods:lidarr-flac2mp3`
21-
- Dev/test release: `-e DOCKER_MODS=thecaptain989/lidarr-flac2mp3:latest`
22-
23-
*Example Docker CLI Configuration*
19+
1. Add a **DOCKER_MODS** environment variable to your `compose.yml` file or `docker run` command, as follows:
20+
- Stable release: `DOCKER_MODS=linuxserver/mods:lidarr-flac2mp3`
21+
- Dev/test release: `DOCKER_MODS=thecaptain989/lidarr-flac2mp3:latest`
22+
23+
*Example Docker Compose YAML Configuration*
24+
```yaml
25+
version: "2.1"
26+
services:
27+
lidarr:
28+
image: lscr.io/linuxserver/lidarr
29+
container_name: lidarr
30+
environment:
31+
- PUID=1000
32+
- PGID=1000
33+
- TZ=America/Chicago
34+
- DOCKER_MODS=linuxserver/mods:lidarr-flac2mp3
35+
volumes:
36+
- /path/to/appdata/config:/config
37+
- /path/to/music:/music
38+
- /path/to/downloads:/downloads
39+
ports:
40+
- 8686:8686
41+
restart: unless-stopped
42+
```
43+
44+
*Example Docker Run Command*
2445
```shell
2546
docker run -d \
2647
--name=lidarr \
@@ -34,10 +55,10 @@ Development Container info:
3455
-v /path/to/downloads:/downloads \
3556
--restart unless-stopped \
3657
lscr.io/linuxserver/lidarr
37-
```
58+
```
3859

3960
*Example Synology Configuration*
40-
![flac2mp3](.assets/lidarr-synology.png "Synology container settings")
61+
![flac2mp3](.assets/lidarr-synology.png "Synology container settings")
4162

4263
2. Start the container.
4364

@@ -66,13 +87,12 @@ To supply arguments to the script, you **must** either use one of the **[include
6687
The script may be called with optional command line arguments.
6788

6889
The syntax for the command line is:
69-
`flac2mp3 [{-d|--debug} [<level>]] [{-b|--bitrate} <bitrate> | {-v|--quality} <quality> | {-a|--advanced} "<options>" {-e|--extension} <extension>] [{-f|--file} <audio_file>] [{-k|--keepfile}] [{-o|--output} <directory>] [{-r|--regex} '<regex>'] [{-t|--tags} <taglist>]`
90+
`flac2mp3 [{-b|--bitrate} <bitrate> | {-v|--quality} <quality> | {-a|--advanced} "<options>" {-e|--extension} <extension>] [{-f|--file} <audio_file>] [{-k|--keep-file}] [{-o|--output} <directory>] [{-r|--regex} '<regex>'] [{-t|--tags} <taglist>] [{-l|--log} <log_file>] [{-d|--debug} [<level>]]`
7091

7192
Where:
7293

7394
Option|Argument|Description
7495
---|---|---
75-
-d, --debug|\[\<level\>\]|Enables debug logging. Level is optional.<br/>Default of 1 (low).<br/>2 includes JSON and FFmpeg output.<br/>3 contains even more JSON output.
7696
-b, --bitrate|\<bitrate\>|Sets the output quality in constant bits per second (CBR).<br/>Examples: 160k, 240k, 300000<br/>**Note:** May not be specified with `-v`, `-a`, or `-e`.
7797
-v, --quality|\<quality\>|Sets the output variable bit rate (VBR).<br/>Specify a value between 0 and 9, with 0 being the highest quality.<br/>See the [FFmpeg MP3 Encoding Guide](https://trac.ffmpeg.org/wiki/Encode/MP3) for more details.<br/>**Note:** May not be specified with `-b`, `-a`, or `-e`.
7898
-a, --advanced|\"\<options\>\"|Advanced ffmpeg options.<br/>The specified `options` replace all script defaults and are sent directly to ffmpeg.<br/>The `options` value must be enclosed in quotes.<br/>See [FFmpeg Options](https://ffmpeg.org/ffmpeg.html#Options) for details on valid options, and [Guidelines for high quality audio encoding](https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio) for suggested usage.<br/>**Note:** Requires the `-e` option to also be specified. May not be specified with `-v` or `-b`.<br/>![warning] **WARNING:** You must specify an audio codec (by including a `-c:a <codec>` ffmpeg option) or the resulting file will contain no audio!<br/>![warning] **WARNING:** Invalid `options` could result in script failure!
@@ -81,7 +101,9 @@ Option|Argument|Description
81101
-o, --output|\<directory\>|Converted audio file(s) are saved to `directory` instead of being located in the same directory as the source audio file.<br/>The path will be created if it does not exist.
82102
-k, --keep-file| |Do not delete the source file or move it to the Lidarr Recycle bin.<br/>**Note:** This also disables importing the new files into Lidarr after conversion.
83103
-r, --regex|'\<regex\>'|Sets the regular expression used to select input files.<br/>The `regex` value should be enclosed in single quotes and escaped properly.<br/>Defaults to `[.]flac$`.
104+
-l, --log|\<log_file\>|The log filename<br/>Default of /config/log/flac2mp3.txt
84105
-t, --tags|\<taglist\>|Comma separated list of metadata tags to apply automated corrections to.<br/>See [Metadata Corrections](./README.md#metadata-corrections) section.
106+
-d, --debug|\[\<level\>\]|Enables debug logging. Level is optional.<br/>Default of 1 (low).<br/>2 includes JSON and FFmpeg output.<br/>3 contains even more JSON output.
85107
--help| |Display help and exit.
86108
--version| |Display version and exit.
87109

@@ -159,7 +181,7 @@ Then put `/config/flac2mp3-custom.sh` in the **Path** field in place of `/usr/lo
159181
### Environment Variable
160182
The `flac2mp3.sh` script also allows the use of arguments provided by the `FLAC2MP3_ARGS` environment variable. This allows advanced use cases without having to provide a custom script.
161183

162-
For example, the following value would convert any .mp3 to Opus:
184+
For example, the following value in your `docker run` command would convert any .mp3 to Opus:
163185
```
164186
-e FLAC2MP3_ARGS='-a "-vn -c:a libopus -b:a 192k" -e .opus -r "[.]mp3$"'
165187
```
@@ -186,7 +208,7 @@ Using this function, you can easily process all of your audio files in any subdi
186208
187209
#### Script Execution Differences in Batch Mode
188210
Because the script is not called from within Lidarr, expect the following behavior while in Batch Mode:
189-
* *The file name must be specified on the command line*<br/>(The `-f` option places the script in Batch Mode)
211+
* *The filename must be specified on the command line*<br/>(The `-f` option places the script in Batch Mode)
190212
* *Lidarr APIs are not called and its database is not updated.*<br/>This may require a manual import of converted music files or an artist rescan.
191213
* *Original audio files are deleted.*<br/>The Recycle Bin function is not available. (Modifiable using the `-k` option.)
192214

@@ -197,22 +219,23 @@ find /music/ -type f -name "*.flac" | while read file; do /usr/local/bin/flac2mp
197219
```
198220

199221
### Logs
200-
A log file is created for the script activity called:
222+
By default, a log file is created for the script activity called:
201223

202224
`/config/logs/flac2mp3.txt`
203225

204-
This log can be downloaded from Lidarr under *System* > *Log Files*
226+
This log can be downloaded from Lidarr under *System* > *Log Files*. The log filename can be modified with the `--log` command-line option.
205227

206228
Log rotation is performed, with 5 log files of 1MB each kept, matching Lidarr's log retention.
207229
>![danger] **NOTE:** If debug logging is enabled with a level above 1, the log file can grow very large very quickly and is much more likely to be rotated. *Do not leave high-level debug logging enabled permanently.*
208230

209231
#### Metadata Corrections
210-
This feature is not meant for general purpose use. It is only documented for completeness.
232+
This feature is not meant for general purpose use. It is only documented here for completeness.
211233

212234
List of supported tags and metadata corrections that are applied:
213235

214236
|Tag|Original|Correction
215237
|---|---|---
238+
|title|Parenthesis for live\|remix, etc. "()"|Square brackets "\[]"
216239
|disc|1|1/1
217240
|genre|/Pop/|"Pop"
218241
| |/Indie/|"Alternative & Indie"
@@ -221,7 +244,16 @@ List of supported tags and metadata corrections that are applied:
221244
| |/Punk\|Alternative/|"Alternative & Punk"
222245
| |/Rock/|"Rock"
223246

224-
## Credits
247+
# Uninstall
248+
To completely remove the mod:
249+
1. Delete the custom script from Lidarr's *Settings* > *Connect* screen that you created in the [Installation](./README.md#installation) section above.
250+
2. Stop and delete the Lidarr container.
251+
3. Exclude the **DOCKER_MODS** environment variable from your `compose.yaml` file or the `docker run` command when re-creating the Lidarr container.
252+
253+
___
254+
255+
# Credits
256+
225257
This would not be possible without the following:
226258

227259
[Lidarr](https://lidarr.audio/ "Lidarr homepage")

root/usr/local/bin/flac2mp3.sh

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
# stat
1515
# nice
1616
# basename
17+
# dirname
1718
# printenv
1819
# chmod
1920
# tr
21+
# sed
2022

2123
# Exit codes:
2224
# 0 - success; or test
2325
# 1 - no audio tracks detected
2426
# 2 - ffmpeg not found
2527
# 3 - invalid command line arguments
28+
# 4 - log file is not writable
2629
# 5 - specified audio file not found
2730
# 6 - error when creating output directory
2831
# 7 - unknown eventtype environment variable
@@ -58,13 +61,9 @@ Audio conversion script designed for use with Lidarr
5861
Source: https://github.com/TheCaptain989/lidarr-flac2mp3
5962
6063
Usage:
61-
$0 [{-d|--debug} [<level>]] [{-b|--bitrate} <bitrate> | {-v|--quality} <quality> | {-a|--advanced} \"<options>\" {-e|--extension} <extension>] [{-f|--file} <audio_file>] [{-k|--keepfile}] [{-o|--output} <directory>] [{-r|--regex} '<regex>'] [{-t|--tags} <taglist>]
64+
$0 [{-b|--bitrate} <bitrate> | {-v|--quality} <quality> | {-a|--advanced} \"<options>\" {-e|--extension} <extension>] [{-f|--file} <audio_file>] [{-k|--keep-file}] [{-o|--output} <directory>] [{-r|--regex} '<regex>'] [{-t|--tags} <taglist>] [{-l|--log} <log_file>] [{-d|--debug} [<level>]]
6265
6366
Options:
64-
-d, --debug [<level>] Enable debug logging
65-
level is optional, between 1-3
66-
1 is lowest, 3 is highest
67-
[default: 1]
6867
-b, --bitrate <bitrate> Set output quality in constant bits per second
6968
[default: 320k]
7069
Ex: 160k, 240k, 300000
@@ -102,6 +101,12 @@ Options:
102101
-t, --tags <taglist> Comma separated list of metadata tags to apply
103102
automated corrections to.
104103
Supports: disc, genre
104+
-l, --log <log_file> log filename
105+
[default: /config/log/flac2mp3.txt]
106+
-d, --debug [<level>] Enable debug logging
107+
level is optional, between 1-3
108+
1 is lowest, 3 is highest
109+
[default: 1]
105110
--help Display this help and exit
106111
--version Display script version and exit
107112
@@ -163,6 +168,16 @@ while (( "$#" )); do
163168
echo "$flac2mp3_script $flac2mp3_ver"
164169
exit 0
165170
;;
171+
-l|--log ) # Log file
172+
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
173+
export flac2mp3_log="$2"
174+
shift 2
175+
else
176+
echo "Error|Invalid option: $1 requires an argument." >&2
177+
usage
178+
exit 1
179+
fi
180+
;;
166181
-f|--file ) # Batch Mode
167182
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
168183
# Overrides detected *_eventtype
@@ -277,7 +292,7 @@ while (( "$#" )); do
277292
exit 3
278293
fi
279294
;;
280-
-*|--*=) # Unknown option
295+
-*) # Unknown option
281296
echo "Error|Unknown option: $1" >&2
282297
usage
283298
exit 20
@@ -508,9 +523,9 @@ function import_tracks {
508523
}
509524
# Get track media info from ffprobe
510525
function ffprobe {
511-
[ $flac2mp3_debug -ge 2 ] && echo "Debug|Executing: /usr/bin/ffprobe -hide_banner -loglevel $flac2mp3_ffmpeg_log -print_format json=compact=1 -show_format -show_entries \"format=tags : format_tags=disc,genre\" -i \"$1\"" | log
526+
[ $flac2mp3_debug -ge 2 ] && echo "Debug|Executing: /usr/bin/ffprobe -hide_banner -loglevel $flac2mp3_ffmpeg_log -print_format json=compact=1 -show_format -show_entries \"format=tags : format_tags=title,disc,genre\" -i \"$1\"" | log
512527
unset flac2mp3_ffprobe_json
513-
flac2mp3_ffprobe_json=$(/usr/bin/ffprobe -hide_banner -loglevel $flac2mp3_ffmpeg_log -print_format json=compact=1 -show_format -show_entries "format=tags : format_tags=disc,genre" -i "$1")
528+
flac2mp3_ffprobe_json=$(/usr/bin/ffprobe -hide_banner -loglevel $flac2mp3_ffmpeg_log -print_format json=compact=1 -show_format -show_entries "format=tags : format_tags=title,disc,genre" -i "$1")
514529
flac2mp3_return=$?; [ $flac2mp3_return -ne 0 ] && {
515530
flac2mp3_message="Error|[$flac2mp3_return] ffprobe error when inspecting track: \"$1\""
516531
echo "$flac2mp3_message" | log
@@ -524,20 +539,48 @@ function ffprobe {
524539
fi
525540
return $flac2mp3_return
526541
}
542+
# Exit program
543+
function end_script {
544+
# Cool bash feature
545+
flac2mp3_message="Info|Completed in $((SECONDS/60))m $((SECONDS%60))s"
546+
echo "$flac2mp3_message" | log
547+
[ "$1" != "" ] && flac2mp3_exitstatus=$1
548+
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Exit code ${flac2mp3_exitstatus:-0}" | log
549+
exit ${flac2mp3_exitstatus:-0}
550+
}
527551
### End Functions
528552

553+
# Check that log path exists
554+
if [ ! -d "$(dirname $flac2mp3_log)" ]; then
555+
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Log file path does not exist: '$(dirname $flac2mp3_log)'. Using log file in current directory."
556+
flac2mp3_log=./flac2mp3.txt
557+
fi
558+
559+
# Check that the log file exists
560+
if [ ! -f "$flac2mp3_log" ]; then
561+
echo "Info|Creating a new log file: $flac2mp3_log"
562+
touch "$flac2mp3_log" 2>&1
563+
fi
564+
565+
# Check that the log file is writable
566+
if [ ! -w "$flac2mp3_log" ]; then
567+
echo "Error|Log file '$flac2mp3_log' is not writable or does not exist." >&2
568+
flac2mp3_log=/dev/null
569+
flac2mp3_exitstatus=4
570+
fi
571+
529572
# Check for required binaries
530573
if [ ! -f "/usr/bin/ffmpeg" ]; then
531574
flac2mp3_message="Error|/usr/bin/ffmpeg is required by this script"
532575
echo "$flac2mp3_message" | log
533576
echo "$flac2mp3_message" >&2
534-
exit 2
577+
end_script 2
535578
fi
536579
if [ ! -f "/usr/bin/ffprobe" ]; then
537580
flac2mp3_message="Error|/usr/bin/ffprobe is required by this script"
538581
echo "$flac2mp3_message" | log
539582
echo "$flac2mp3_message" >&2
540-
exit 2
583+
end_script 2
541584
fi
542585

543586
# Log Debug state
@@ -563,7 +606,7 @@ if [[ "$lidarr_eventtype" = "Test" ]]; then
563606
flac2mp3_message="Info|Script was test executed successfully."
564607
echo "$flac2mp3_message" | log
565608
echo "$flac2mp3_message"
566-
exit 0
609+
end_script
567610
fi
568611

569612
# Log Batch mode
@@ -588,7 +631,7 @@ elif [ -f "$flac2mp3_config" ]; then
588631
[[ $flac2mp3_bindaddress = "*" ]] && flac2mp3_bindaddress=localhost
589632

590633
# Build URL to Lidarr API
591-
flac2mp3_api_url="http://$flac2mp3_bindaddress:$flac2mp3_port$flac2mp3_urlbase/api/v1"
634+
flac2mp3_api_url="http://$flac2mp3_bindaddress:$flac2mp3_port${flac2mp3_urlbase:+/$flac2mp3_urlbase}/api/v1"
592635

593636
# Check Lidarr version
594637
if get_version; then
@@ -616,15 +659,15 @@ if [ "$flac2mp3_type" = "batch" -a ! -f "$flac2mp3_tracks" ]; then
616659
flac2mp3_message="Error|Input file not found: \"$flac2mp3_tracks\""
617660
echo "$flac2mp3_message" | log
618661
echo "$flac2mp3_message" >&2
619-
exit 5
662+
end_script 5
620663
fi
621664

622665
# Check for empty tracks variable
623666
if [ -z "$flac2mp3_tracks" ]; then
624667
flac2mp3_message="Error|No audio tracks were detected or specified!"
625668
echo "$flac2mp3_message" | log
626669
echo "$flac2mp3_message" >&2
627-
exit 1
670+
end_script 1
628671
fi
629672

630673
# If specified, check if destination folder exists and create if necessary
@@ -635,7 +678,7 @@ if [ "$flac2mp3_output" -a ! -d "$flac2mp3_output" ]; then
635678
flac2mp3_message="Error|[$flac2mp3_return] mkdir returned an error. Unable to create output directory."
636679
echo "$flac2mp3_message" | log
637680
echo "$flac2mp3_message" >&2
638-
exit 6
681+
end_script 6
639682
}
640683
fi
641684

@@ -724,15 +767,23 @@ for flac2mp3_track in $flac2mp3_tracks; do
724767
if ffprobe "$flac2mp3_track"; then
725768
for flac2mp3_tag in $(echo $flac2mp3_tags | tr ',' '|'); do
726769
case "$flac2mp3_tag" in
770+
title ) # Fix for parenthesis in titles for live and mix names
771+
flac2mp3_tag_title=$(echo "$flac2mp3_ffprobe_json" | jq -crM '.format.tags | to_entries[] | select(.key | match("title"; "i")).value')
772+
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Original metadata: title=$flac2mp3_tag_title" | log
773+
flac2mp3_pattern='\([^)]+\)$' # Rough way to limit editing metadata for every track
774+
if [[ "$flac2mp3_tag_title" =~ $flac2mp3_pattern ]]; then
775+
flac2mp3_ffmpeg_metadata+="-metadata title=\"$(echo "$flac2mp3_tag_title" | sed -r 's/\((live|acoustic|demo|[^)]*((re)?mix(es)?|dub|version))\)$/[\1]/i')\" "
776+
fi
777+
;;
727778
disc ) # Fix one disc by itself
728-
flac2mp3_tag_disc=$(echo "$flac2mp3_ffprobe_json" | jq -crM '.format.tags.disc')
779+
flac2mp3_tag_disc=$(echo "$flac2mp3_ffprobe_json" | jq -crM '.format.tags | to_entries[] | select(.key | match("disc"; "i")).value')
729780
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Original metadata: disc=$flac2mp3_tag_disc" | log
730781
if [ "$flac2mp3_tag_disc" == "1" ]; then
731782
flac2mp3_ffmpeg_metadata+='-metadata disc="1/1" '
732783
fi
733784
;;
734785
genre ) # Fix multiple genres
735-
flac2mp3_tag_genre=$(echo "$flac2mp3_ffprobe_json" | jq -crM '.format.tags | to_entries[] | select(.key | match("genre";"i")).value')
786+
flac2mp3_tag_genre=$(echo "$flac2mp3_ffprobe_json" | jq -crM '.format.tags | to_entries[] | select(.key | match("genre"; "i")).value')
736787
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Original metadata: genre=$flac2mp3_tag_genre" | log
737788
# Only trigger on multiple genres
738789
if [[ $flac2mp3_tag_genre =~ \; ]]; then
@@ -821,9 +872,6 @@ for flac2mp3_track in $flac2mp3_tracks; do
821872
flac2mp3_import_list+="${flac2mp3_newTrack}|"
822873
done
823874
IFS=$' \t\n'
824-
# Remove trailing pipe
825-
flac2mp3_import_list="${flac2mp3_import_list%|}"
826-
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Track import list: \"$flac2mp3_import_list\"" | log
827875
#### END MAIN
828876

829877
#### Call Lidarr API to update database
@@ -835,6 +883,9 @@ elif [ $flac2mp3_keep -eq 1 ]; then
835883
elif [ -n "$flac2mp3_api_url" ]; then
836884
# Check for artist ID
837885
if [ -n "$lidarr_artist_id" ]; then
886+
# Remove trailing pipe
887+
flac2mp3_import_list="${flac2mp3_import_list%|}"
888+
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Track import list: \"$flac2mp3_import_list\"" | log
838889
# Scan for files to import into Lidarr
839890
export flac2mp3_import_count=$(echo $flac2mp3_import_list | awk -F\| '{print NF}')
840891
if [ $flac2mp3_import_count -ne 0 ]; then
@@ -906,8 +957,4 @@ else
906957
flac2mp3_exitstatus=20
907958
fi
908959

909-
# Cool bash feature
910-
flac2mp3_message="Info|Completed in $(($SECONDS/60))m $(($SECONDS%60))s"
911-
echo "$flac2mp3_message" | log
912-
[ $flac2mp3_debug -ge 1 ] && echo "Debug|Exit code ${flac2mp3_exitstatus:-0}" | log
913-
exit ${flac2mp3_exitstatus:-0}
960+
end_script

0 commit comments

Comments
 (0)