From 19cfd450e9e597d7aa1483a96a0d5c717047830e Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Fri, 8 Jul 2011 12:40:42 +0200 Subject: [PATCH] zshrc: replace ogg2mp3_192 with improved 2mp3_192 --- doc/grmlzshrc.t2t | 8 +++++--- etc/zsh/zshrc | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/doc/grmlzshrc.t2t b/doc/grmlzshrc.t2t index 09311ea..1398a7a 100644 --- a/doc/grmlzshrc.t2t +++ b/doc/grmlzshrc.t2t @@ -427,6 +427,11 @@ user's life. Converts plaintext files to HTML using vim. The output is written to .html. +: **2mp3_192()** +Useful if you own players lacking ogg/flac support. +Takes a list of "*.ogg" and "*.flac" files and transcodes them to mp3 +with a variable bitrate of at least 192, while preserving basic id3 tags. + : **855resolution()** If 915resolution is available, issues a warning to the user to run it instead to modify the resolution on intel graphics chipsets. @@ -769,9 +774,6 @@ Example usages: % ls -l *(e:'nt /reference/file':) ``` -: **ogg2mp3_192()** -Recodes an ogg file to mp3 with a bitrate of 192. - : **oleo()** Translates the given word using the english - german online dictionary dict.leo.org. diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index 996e4e8..42b5671 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -3901,11 +3901,52 @@ allulimit() { ulimit -t unlimited } -# ogg2mp3 with bitrate of 192 -ogg2mp3_192() { - emulate -L zsh - oggdec -o - $1 | lame -b 192 - ${1:r}.mp3 -} +# 2mp3 transcodes flac and ogg to mp3 with bitrate of 192 while preserving basic tags +if check_com lame; then + 2mp3_192() { + emulate -L zsh + setopt extendedglob + unsetopt ksharrays + + local -a DECODE id3tags + local -A tagmap + local tagdata + local RC=0 + tagmap=("(#l)title" --tt "(#l)artist" --ta "(#l)tracknumber" --tn "(#l)genre" --tg "(#l)album" --tl) + + if [[ ${@[(i)*.ogg]} -le ${#@} ]] && ! check_com oggdec; then + echo "ERROR: please install oggdec" >&2 + return 1 + fi + if [[ ${@[(i)*.flac]} -le ${#@} ]] && ! check_com flac; then + echo "ERROR: please install flac" >&2 + return 1 + fi + + for af in "$@"; do + id3tags=() + case "$af" in + (*.flac) + DECODE=(flac -d -c "$af") + tagdata="$(metaflac --export-tags-to=- "$af")" + ;; + (*.ogg) + DECODE=(oggdec -Q -o - "$af") + tagdata="$(ogginfo "$af")" + ;; + (*) continue ;; + esac + for line (${(f)tagdata}) \ + [[ "$line" == (#s)[[:space:]]#(#b)([^=]##)=(*)(#e) && -n $tagmap[(k)$match[1]] ]] && \ + id3tags+=($tagmap[(k)$match[1]] "$match[2]") + [[ ${#id3tags} -gt 0 ]] && id3tags=(--add-id3v2 --ignore-tag-errors $id3tags) + $DECODE[*] | lame -b 192 -v -h --replaygain-fast "${id3tags[@]}" - "${af:r}.mp3" || {RC=$?; print "Error transcoding" "${af}"; } + done + # return 0 if no error or exit code if at least one error happened + return $RC + } + alias ogg2mp3_192 2mp3_192 +fi #f5# RFC 2396 URL encoding in Z-Shell urlencode() { -- 2.1.4