Skip to content

Instantly share code, notes, and snippets.

@dentex
Created December 30, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dentex/77576076fe9af503bec851f6288e466d to your computer and use it in GitHub Desktop.
Save dentex/77576076fe9af503bec851f6288e466d to your computer and use it in GitHub Desktop.
Yet Another FLAC to MP3 script
#!/bin/bash
#
# Copyright 2008 Octavio Ruiz
# Distributed under the terms of the GNU General Public License v3
# $Header: $
#
# Yet Another FLAC to MP3 script
#
# Author:
# Octavio Ruiz (Ta^3) <tacvbo@tacvbo.net>
# Thanks:
# Those comments at:
# http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3
# WebPage:
# https://github.com/tacvbo/yaflac2mp3/tree
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY. YOU USE AT YOUR OWN RISK. THE AUTHOR
# WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY
# OTHER KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.
# See the GNU General Public License for more details.
#
# Modified by woohoo
#
# Additional modifications by dentex:
# 2018/07/05: ?
# 2022/12/30: add proper zenity progress
#
# you need zenity package for notifications.
# please note that you can put this script in ~/.local/share/nemo/scripts
# and it will show up in right-click menu in any folder in cinnamon.
#
# modify the lame options to your preference example change -b 320 to -b 128 or -b 192 or -b 256
# LAME_OPTS="--vbr-new -V 0 -b 256"
# LAME_OPTS="-V 0 --vbr-new"
LAME_OPTS="-b 320 -h --cbr"
old_IFS=${IFS}
IFS='
'
# when running from nemo-scripts, it's useful to find the current folder
base="`echo $NEMO_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
if [ -z "$NEMO_SCRIPT_SELECTED_FILE_PATHS" ]; then
dir="$base"
else
while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
dir="$base/$1"
fi
if [ "$dir" != "" ]; then
cd "$dir"
fi
# Create a temp file
tmp_file=$(mktemp)
id3v2=$(which id3v2)
# Populate array of files to process
readarray -d $'\0' files < <(find . -type f -regex '^.+\.flac$' -print0)
# Init progress vars
file_num=${#files[@]}
pr=0
(( i_pr=100/file_num ))
declare -i processed_files=0
# Loop through files
for fn in "${files[@]}"; do
vars=( `metaflac --no-utf8-convert --export-tags-to=- "${fn}"` )
for N_vars in ${!vars[@]}; do
export "$(echo "${vars[${N_vars}]%=*}" | tr [:upper:] [:lower:])=${vars[${N_vars}]#*=}"
done
dest=`echo "$fn"|sed -e 's/\.flac$/\.mp3/'`
# print zenity text
echo "# processing $dest ..."
# print zenity progress
echo $pr
flac -dc "$fn" |\
lame --ignore-tag-errors --add-id3v2 ${LAME_OPTS} \
${artist:+--ta} ${artist} \
${tracknumber:+--tn} ${tracknumber} \
${title:+--tt} ${title} \
${album:+--tl} ${album} \
${date:+--ty} ${date} \
${genre:+--tg} ${genre} \
${comment:+--tc} ${comment} \
- $dest
[[ -x ${id3v2} ]] && ${id3v2} \
${artist:+--artist} ${artist} \
${tracknumber:+--track} ${tracknumber} \
${title:+--song} ${title} \
${album:+--album} ${album} \
${date:+--year} ${date} \
${genre:+--genre} ${genre} \
${comment:+--comment} ${comment} \
$dest
# increment zenity progress for next iteration
(( pr=pr+i_pr ))
# keep track of processed file number
(( processed_files++ ))
echo $processed_files > $tmp_file
done | tee >(zenity --height 150 --width 300 --progress --title="processing files ..." --auto-close --auto-kill)
zenity --notification --text "Finished converting flac to mp3. ${IFS}Processed $(cat $tmp_file) files."
IFS=${old_IFS}
rm -f "$tmp_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment