-
-
Save keeferrourke/13613bfaae34c0d7528f12ea5d7cafc5 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 [-bBITRATE] | |
which metaflac | |
if [ $? -ne 0 ]; then | |
echo "flac tools are not installed. Run \`sudo dnf install flac\` and try again." | |
exit 1 | |
fi | |
which opusenc | |
if [ $? -ne 0 ]; then | |
echo "opus-tools is not installed. Run \`sudo dnf install opus-tools\` and try again" | |
exit 1 | |
fi | |
# "global" variables | |
wd="$PWD" | |
br=256 | |
artfile="$1/.tempart.img" | |
cleanup=0 | |
hasart=0 | |
# cleanup actions | |
cleanup() { | |
if [ -f "$artfile" ]; then | |
rm "$artfile" | |
fi | |
} | |
# check script usage | |
[ -z "$1" ] && echo "File path is required." && exit 2; | |
# parse args | |
if [ ! -z "$2" ] && [ "${2:0:2}" == "-b" ]; then | |
br="$(sed 's/[^0-9]//g' <<< $2)" | |
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 | |
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" \ | |
"$f" "$wd/${basefilename/%flac/opus}" | |
# 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: [23:17 04 November 2018] Turns out I made a mistake before and thought that metaflac would re-encode tagged image data to the suitable format for the specified output. Obviously this wasn't the case so this script output badly named images which wouldn't open in some viewers.
The new version of this script now uses file
to determine the mimetype of the image, and uses that to provide a better file extension. file --extension
(which is supposed to list valid file extensions) was giving me garbage output so I hacked in a solution that works for jpg and png files at the very least... Really you shouldn't be tagging your music with other image formats though π
Hey! This is an amazing script!
Here's one issue:
When you use --discard-comments
, opusenc
even discards the album-art (you can verify it by mediainfo's text view on the .opus
file ).
I didnt notice that because VLC stores a cache of artworks.
Though this can be easily fixed by passing --picture "$artfile"
the problem arises when the flac files have multiple artwork files (front cover, back cover etc ) , so only one of the artwork gets copied.
Also, if you remove the --discard-comments
, and keep the rest, opusenc will not overwrite the values, instead it duplicates them :/
So either I have to stick with 1 artwork or remove all the flags from the script :/
Here's my fork of your script.
I have
Added a -p
parameter to manually specify a cover image in case the flac doesn't have it (or you want to change it).
@DroidFreak32 thanks for the comments! I'm glad you found the script useful and were able to make improvements π
DISCNUMBERLAIMS
Whoops?
I had trouble getting album art embedded within resulting opus with this. After trying multiple times, I now just use Strawberry to convert stuff, and it works just as expected.
Not as convenient as firing up a script, but a very close second.
DISCNUMBERLAIMS
Whoops?
Whoops indeed :) Good old search/replace got me I guess :P
Nice but why not use ffmpeg?
What does this do?
This script will re-encode FLAC files found in the supplied directory to VBR OPUS and output to the directory from which the script was run.
Usage
flac2opus /path/to/directory [-b<target bitrate>]
where target bitrate is the average bitrate of the file; the higher the number the bigger the file.
A default target bitrate of 256kbps is used since this is probably close to transparent with FLAC, though 128kbps would probably be more than enough.
FAQ
Don't you know that opusenc copies metadata automagically?!
... Yes, I am aware that when converting from FLAC to OPUS via
opusenc
, metadata and images are normally automatically copied, however often the information in many of these comment tags is pretty well useless or populated with annoying information, so this script only writes tags that are useful to most music players.Will this script ever be updated?
I mean... maybe? Suggestions and edits are welcome. This works for my use cases though, so I won't really put much more time into this β hence the Gist and not a proper repo.