2011年6月27日星期一

flac cue分割

以前遇到cue都是用wine模擬foobar 2000然後轉成分軌的ogg格式
前幾天想把無損音樂燒成CD,但是找不到任何燒錄程式是支持cue的
於是要手動把單個flac按cue分割成一個個單獨的文件
原本也想用foobar的,但是想想又不希望分軌後再次轉換格式而失真
所以就研究下命令行下的方式

先安裝:shnsplit cuetools
在Gentoo的Portage裹都有的,然後使用以下腳本
就可以把單個檔案分割成每首歌一個檔案,我沒有設定輸出格式所以是保留為wav
其實foobar 2000也是先轉成分軌的wav再把wav壓縮成需要輸出的格式
然後用cue裹的資訊填上去分軌後的檔案裹

$ cat ~/bin/cuetag.sh 
#! /bin/sh

# cuetag.sh - tag files based on cue/toc file information
# uses cueprint output
# usage: cuetag.sh  [file]...

CUEPRINT=cueprint
cue_file=""

usage()
{
 echo "usage: cuetag.sh <cuefile|tocfile> <audio_file> "
}

# Vorbis Comments
# for FLAC and Ogg Vorbis files
vorbis()
{
 # FLAC tagging
 # --remove-vc-all overwrites existing comments
 METAFLAC="metaflac --remove-vc-all --import-vc-from=-"

 # Ogg Vorbis tagging
 # -w overwrites existing comments
 # -a appends to existing comments
 VORBISCOMMENT="vorbiscomment -w -c -"

    VORBISTAG=""
 case "$2" in
 *.[Ff][Ll][Aa][Cc])
  VORBISTAG=$METAFLAC
  ;;
 *.[Oo][Gg][Gg])
  VORBISTAG=$VORBISCOMMENT
  ;;
 esac

 # space seperated list of recomended stardard field names
 # see http://www.xiph.org/ogg/vorbis/doc/v-comment.html
 # TRACKTOTAL is not in the Xiph recomendation, but is in common use
 
 fields='TITLE VERSION ALBUM TRACKNUMBER TRACKTOTAL ARTIST PERFORMER COPYRIGHT LICENSE ORGANIZATION DESCRIPTION GENRE DATE LOCATION CONTACT ISRC'

 # fields' corresponding cueprint conversion characters
 # seperate alternates with a space

 TITLE='%t'
 VERSION=''
 ALBUM='%T'
 TRACKNUMBER='%n'
 TRACKTOTAL='%N'
 ARTIST='%c %p'
 PERFORMER='%p'
 COPYRIGHT=''
 LICENSE=''
 ORGANIZATION=''
 DESCRIPTION='%m'
 GENRE='%g'
 DATE=''
 LOCATION=''
 CONTACT=''
 ISRC='%i %u'

 (for field in $fields; do
  value=""
  for conv in `eval echo \\$$field`; do
   value=`$CUEPRINT -n $1 -t "$conv\n" $cue_file`

   if [ -n "$value" ]; then
    echo "$field=$value"
    break
   fi
  done
 done) | $VORBISTAG "$2"
}

wavname()
{
    name = `cueprint -n 1 -t "%t" $cue_file`
    mv "$2" "$name.wav"
}

id3()
{
 MP3INFO=mp3info

 # space seperated list of ID3 v1.1 tags
 # see http://id3lib.sourceforge.net/id3/idev1.html

 fields="TITLE ALBUM ARTIST YEAR COMMENT GENRE TRACKNUMBER"

 # fields' corresponding cueprint conversion characters
 # seperate alternates with a space

 TITLE='%t'
 ALBUM='%T'
 ARTIST='%p'
 YEAR=''
 COMMENT='%c'
 GENRE='%g'
 TRACKNUMBER='%n'

 for field in $fields; do
  value=""
  for conv in `eval echo \\$$field`; do
   value=`$CUEPRINT -n $1 -t "$conv\n" $cue_file`

   if [ -n "$value" ]; then
    break
   fi
  done

  if [ -n "$value" ]; then
   case $field in
   TITLE)
    $MP3INFO -t "$value" "$2"
    ;;
   ALBUM)
    $MP3INFO -l "$value" "$2"
    ;;
   ARTIST)
    $MP3INFO -a "$value" "$2"
    ;;
   YEAR)
    $MP3INFO -y "$value" "$2"
    ;;
   COMMENT)
    $MP3INFO -c "$value" "$2"
    ;;
   GENRE)
    $MP3INFO -g "$value" "$2"
    ;;
   TRACKNUMBER)
    $MP3INFO -n "$value" "$2"
    ;;
   esac
  fi
 done
}

main()
{
 if [ $# -lt 2 ]; then
  usage
  exit
 fi

 cue_file="$1"
 shift
    wav_file="$1"
    shift
    echo "cue: $cue_file"
    echo "wav: $wav_file"

    #Split the wav_file
    cuebreakpoints "$cue_file" | shnsplit -O never -d ./ "$wav_file"

 ntrack=`cueprint -d '%N' "$cue_file"`
 trackno=1

 #if [ $# -ne $ntrack ]; then
  #echo "warning: number of files does not match number of tracks"
 #fi

 #for file in $@; do
    for file in `ls split-track*.wav`; do
  case $file in
  *.[Ff][Ll][Aa][Cc])
   #vorbis $trackno "$file"
            name=`cueprint -n $trackno -t "%t" "$cue_file"`
            echo mv "$file" "#$trackno $name.flac"
            mv "$file" "#$trackno $name.flac"
   ;;
  *.[Oo][Gg][Gg])
   #vorbis $trackno "$file"
            name=`cueprint -n $trackno -t "%t" "$cue_file"`
            echo mv "$file" "#$trackno $name.ogg"
            mv "$file" "#$trackno $name.ogg"
   ;;
  *.[Mm][Pp]3)
   #id3 $trackno "$file"
            name=`cueprint -n $trackno -t "%t" "$cue_file"`
            echo mv "$file" "#$trackno $name.mp3"
            mv "$file" "#$trackno $name.mp3"
   ;;
        *.[Ww][Aa][Vv])
            name=`cueprint -n $trackno -t "%t" "$cue_file"`
            echo mv "$file" "#$trackno $name.wav"
            mv "$file" "#$trackno $name.wav"
            ;;
  *)
   echo "$file: uknown file type"
   ;;
  esac
  trackno=$(($trackno + 1))
 done
}

main "$@"

發佈留言

熱門文章