-
-
Save DroidFreak32/d6708cdd63c9478427be53682fc1c147 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Copyright 2017 Keefer Rourke <[email protected]> | |
# | |
# Permission to use, copy, modify, and/or distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCNUMBERLAIMS ALL WARRANTIES WITH | |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | |
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
# PERFORMANCE OF THIS SOFTWARE. | |
# usage: flac2opus PATH [-b BITRATE] [ -p <path to cover image>] | |
which metaflac >> /dev/null | |
if [ $? -ne 0 ]; then | |
echo "flac tools are not installed. Run \`sudo dnf install flac\` and try again." | |
exit 1 | |
fi | |
which opusenc >> /dev/null | |
if [ $? -ne 0 ]; then | |
echo "opus-tools is not installed. Run \`sudo dnf install opus-tools\` and try again" | |
exit 1 | |
fi | |
# check script usage | |
if [ -z "$1" ]; then | |
echo "File path is required." && exit 2; | |
else | |
[ ! -d "$1" ] && echo "File path is required." && exit 2; | |
fi | |
# "global" variables | |
wd="$PWD" | |
br=256 | |
artfile="$1/.tempart.img" | |
cleanup=0 | |
hasart=0 | |
coverspecified=0 | |
POSITIONAL=() | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
-b|--bitrate) | |
br="$(sed 's/[^0-9]//g' <<< $2)" | |
shift # past argument | |
shift # past value | |
;; | |
-p|--picture) | |
coverspecified=1 | |
hasart=1 | |
cover="$2" | |
shift # past argument | |
shift # past value | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
# cleanup actions | |
cleanup() { | |
if [ -f "$artfile" ]; then | |
rm "$artfile" | |
fi | |
} | |
# parse each flac file in the specified directory and output to opus format | |
for f in "$1"/*.flac; do | |
# give output correct extension | |
basefilename=${f##*/} | |
# export metadata; I don't necessarily trust or want every single tag in | |
# the exported file, so let's grab the relevant tags and make opus ignore | |
# extraneous data | |
TITLE=$(metaflac "$f" --show-tag=TITLE | sed s/.*=//g) | |
ARTIST=$(metaflac "$f" --show-tag=ARTIST | sed s/.*=//g) | |
ALBUMARTIST=$(metaflac "$f" --show-tag=ALBUMARTIST | sed s/.*=//g) | |
ALBUM=$(metaflac "$f" --show-tag=ALBUM | sed s/.*=//g) | |
DATE=$(metaflac "$f" --show-tag=DATE | sed s/.*=//g) | |
GENRE=$(metaflac "$f" --show-tag=GENRE | sed s/.*=//g) | |
DISCNUMBER=$(metaflac "$f" --show-tag=DISCNUMBER | sed s/.*=//g) | |
TRACKNUMBER=$(metaflac "$f" --show-tag=TRACKNUMBER | sed s/.*=//g) | |
TRACKTOTAL=$(metaflac "$f" --show-tag=TRACKTOTAL | sed s/.*=//g) | |
LYRICS=$(metaflac "$f" --show-tag=LYRICS | sed s/.*=//g) | |
# for the curious I suppose | |
#metaflac "$f" --export-tags-to=- | |
# if we don't already have an art file, let's try to extract one from the | |
# current flac file | |
if [ $hasart -eq 0 ]; then | |
metaflac "$f" --export-picture-to="$artfile" | |
hasart=1 | |
cleanup=1 | |
fi | |
# convert flac via opusenc and tag the new file; pictures should be copied | |
# automagically if $coverspecified isn't set. | |
if [ $coverspecified -eq 1 ]; then | |
opusenc --vbr --bitrate "$br" \ | |
--discard-comments --date "$DATE" \ | |
--title "$TITLE" --artist "$ARTIST" --album "$ALBUM" --genre "$GENRE" \ | |
--comment "ALBUMARTIST=$ALBUMARTIST" --comment "DISCNUMBER=$DISCNUMBER" \ | |
--comment "TRACKNUMBER=$TRACKNUMBER" --comment "TRACKTOTAL=$TRACKTOTAL" \ | |
--comment "LYRICS=$LYRICS" \ | |
--picture "$cover" \ | |
"$f" "$wd/${basefilename/%flac/ogg}" | |
else | |
# opusenc --vbr --bitrate "$br" --date "$DATE" \ | |
# --title "$TITLE" --artist "$ARTIST" --album "$ALBUM" --genre "$GENRE" \ | |
# --comment "ALBUMARTIST=$ALBUMARTIST" --comment "DISCNUMBER=$DISCNUMBER" \ | |
# --comment "TRACKNUMBER=$TRACKNUMBER" --comment "TRACKTOTAL=$TRACKTOTAL" \ | |
# --comment "LYRICS=$LYRICS" \ | |
# "$f" "$wd/${basefilename/%flac/opus}" | |
opusenc --vbr --bitrate "$br" \ | |
"$f" "$wd/${basefilename/%flac/opus}" | |
fi | |
# cleanup and exit if failure | |
if [ $? -ne 0 ]; then | |
cleanup | |
exit 1 | |
fi | |
done | |
# move the temp artfile to destination with meaningful extension | |
if [ -f "$artfile" ]; then | |
mime=$(file -b --mime-type "$artfile" | sed 's/.*\///g') | |
cp "$artfile" "$wd/front.$mime" | |
fi | |
cleanup | |
exit 0 |
Update 18/11/17 :
If -p is specified, set hasart=1
so that unwanted artwork won't get copied
Awesome. Happy to see this fork! Thanks for fixing some of things I was too lazy to do :)
Hello, I wanted to share my version with you.
This version is multi-threaded and can strip ID3 tags while preserving metadata of flac files.
Though, in this one, metadata is copied "AS IS" because I like to keep all Music Brainz tags.
It can also delete opus files if present before encoding.
And the output folder is passed as an argument to the script (and only if required by the chosen actions).
-h
option explains everything.
I am not a very good bash script writer, I think that it can be simplified or better written but, at least, for me, everything works great.
@berturion Thanks!
Found another issue with some FLAC files.
FLAC files are not supposed to contain ID3 tags. This breaks compatibility with opusenc. (Opusenc won't be able to parse the file)
If the FLAC file that have ID3 tags can be found out if the output is "ID3" after running this:
Source
I need to try adding it to the script to prevent errors.
Experimental script to convert: