Перекодирование одиночного lossless аудиофайла во FLAC по CUE-таблице

Материал из Linux Wiki
Перейти к навигацииПерейти к поиску

Скрипт для преобразования одиночного ape-файла, представляющего собой копию диска в отдельные FLAC-файлы с заполнением тегов и именованием файлов в соответствии с CUE-таблицой

Использование
cueape2flac "AUDIO FILE.ape" "TEXT FILE.cue"
Зависимости
monkeys-audio, flac, shntool, enconv, cuetools

#!/bin/bash
# By Rain ;)
# Usage: cueape2flac "AUDIO FILE.ape" "TEXT FILE.cue"
# Depends on monkeys-audio, flac, shntool

outdir='output'
direct='0'

error() {
echo "$1"
exit $2
}

test $(which flac) || error "Error: FLAC convertor not found" 1

if [ ! -z "${2}" ]; then
	test $(which shnsplit)	|| error "Please, install shntool first" 1
	test $(which enconv)	|| error "Please, install enca first" 1
	test $(which cuetag)	|| error "Please, install cuetools first" 1
else
	direct='0'
	echo "CUE-sheet not specified, so we simply convert ${1} to FLAC format"
fi

if	[[ "$(file -b "${1}")" =~ "Monkey's Audio" ]]; then
		test $(which mac)	|| error "Error: Monkey Audio convertor not found" 1
elif	[ "${1##*.}" == 'wv' ]; then
		test $(which wvunpack)	|| error "Error: WavPack decompressor not found" 1
fi

[ -e $outdir ] && echo "Warning: $outdir already exist" || echo "Creating output directory"
mkdir -p "$outdir"
tempfile="$(date +tempfile.%s)"

if [ "${direct}" == '0' ]; then
	if 	[[ "$(file -b "${1}")" =~ "Monkey's Audio" ]]; then
			echo "Converting "$1" from APE to WAV-format"
			mac "$1" "${outdir}/${tempfile}.wav" -d
	elif	[[ "$(file -b "${1}")" =~ "FLAC audio" ]]; then
			echo "Converting "$1" from FLAC to WAV-format"
			flac -d "${1}" -o "${outdir}/${tempfile}.wav"
	elif	[[ "$(file -b "${1}")" =~ "WAVE audio" ]]; then
			echo "Ok, ${1} ready to splitting, linking to ${outdir}"
			ln "${1}" "${outdir}/${tempfile}.wav"
	elif	[ "${1##*.}" == 'wv' ]; then
		echo "Converting "$1" from WavPack to WAV-format"
		wvunpack -m "${1}"
		mv "${1%.*}.wav" "${outdir}/${tempfile}.wav"
	else
		error "Can't recognize input file format" 1
	fi
else
	if 	[ "${1##*.}" == 'wv' ]; then
		echo "Converting "$1" from WavPack to WAV-format"
		wvunpack -m "${1}"
		mv "${1%.*}.wav" "${outdir}/${tempfile}.wav"
	else
		ln "${1}" "${outdir}/${tempfile}.${1##*.}"
	fi
fi

cd "${outdir}"

if [ -z "${2}" ]; then
	flac --best -V "${tempfile}.wav" -o "${1%.*}.flac"
else
	# Converting CUE-sheet
	cat ../"${2}" | enconv > "${2}"
	
	echo "Splitting "${1}" to multiple FLAC files"
	cat "${2}" | shnsplit -o 'flac ext=flac flac --best -V - -o %f' -t %n-%p-%a-%t "${tempfile}"*

	cuetag "${2}" *.flac
fi

rm -f "${tempfile}"*
exit 0

ToDo: для варианта FLAC->FLAC без CUE сделать сохранение тегов, добавить вытягивание CUE из FLAC