📜 ⬆️ ⬇️

Convert flac to mp3 in one motion

Good day.

I once pumped up a mountain of music for myself, without knowing what format it is in, flac or mp3. I thought, "Well, there is a bunch of utilities that convert all this, then I will figure it out." I downloaded, searched the network, what I could convert, I found the All2mp3 program (I'm sitting on a poppy) ... What was my surprise when I found out that I can't just drop the Music folder into it and wait for it to run through all the subfolders, find everything flac files and convert them to mp3! I started to google, but I did not find any sane decision.


I wrote a small shell script that scrolls through folders, searches for flac, converts them to mp3, and can remove .flac if you ask. The script was written on the knee, so the new files are called * .flac.mp3. Also for his work need installed programs flac
and lame .
')
Here is the script itself:
#!/bin/sh #flac2mp3 error(){ echo $@ 1>&2; exit 1; } r=0 rem(){ if [ $r -eq 1 ]; then rm "$1" fi } flac2mp3(){ data=$1 echo " -----------------------Working on $data" if [ -d "$data" ]; then for file in "$data"*; do if [ -d "$file" ]; then file="$file"/ fi flac2mp3 "$file" wait done elif [[ "$data" == *.flac ]]; then flac -d --stdout "$data" > temp.wav wait echo " ---------------------------------$data to wav done" lame temp.wav "$data".mp3 wait echo " ---------------------------------$data to $data.mp3 done" rm temp.wav rem "$data" fi } if [ $# -eq 0 ]; then error "Wrong arguments" fi echo ============================================================ if [ $# -eq 1 ]; then flac2mp3 "$1" elif [ $# -eq 2 ] && [ $1 == "-r" ]; then r=1 flac2mp3 "$2" else error "Wrong arguments!" fi 


How to use it:


I checked it myself - everything works. At least there are mp3 files and nothing superfluous is removed, which is not bad, I think. I hope someone will come in handy.

PS I would be happy to comment on the code, since I write very rarely on the shell.

UPD: The script does not save tags! I apologize to those who have already blown up on this mine, I will now correct it.
UPD2: Since my solution does not work as I would like, the user mgyk kindly offered his version in the comments:
 #!/bin/bash if [ "${1}" == '' ]; then shellDir="$PWD" else shellDir="${1}" fi prefix=/home/share/music/_mp3 find "${shellDir}" -name '*.flac' -print | while read fn; do ARTIST=`metaflac "$fn" --show-tag=ARTIST | sed s/.*=//g` TITLE=`metaflac "$fn" --show-tag=TITLE | sed s/.*=//g` ALBUM=`metaflac "$fn" --show-tag=ALBUM | sed s/.*=//g` GENRE=`metaflac "$fn" --show-tag=GENRE | sed s/.*=//g` TRACKNUMBER=`metaflac "$fn" --show-tag=TRACKNUMBER | sed s/.*=//g` DATE=`metaflac "$fn" --show-tag=DATE | sed s/.*=//g` newpath="${prefix}/$ARTIST/$ALBUM" newfile=${TITLE}.mp3 echo $newfile mkdir -p "${newpath}" flac -c -d "${fn}" | lame -mj -q 0 --vbr-new -V 0 -s 44.1 - "${newpath}/${newfile}" id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "${newpath}/${newfile}" done 


Also, in the comments were proposed several programs that I did not find, and which perform the task:
Max
X Lossless decoder

Source: https://habr.com/ru/post/148171/


All Articles