LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1018|回复: 5

[分享]一个分割大个 ape/flac/tta/wv/wav 文件的小脚本[/讨论]

[复制链接]
发表于 2009-9-3 16:49:22 | 显示全部楼层 |阅读模式
Since MPD still doesn't support (external) cuesheets, I decided to split those full-album-length ape/flac/tta files into one song per file.

Here is a little script I've came up with just for this purpose.

It basically reads the cuesheet (if not specified, the one in the same dir with the big guy), splits the (decoded) big guy into little brothers and sisters, and encodes them to flac.
It also writes tags to the resulting flacs. ALBUM, ARTIST, TRACKNUMBER, and TITLE, that is.
  1. #!/bin/bash
  2. function help()
  3. {
  4. cat << HELP
  5.         % nocue % < beta >
  6.         ==> Splits big ape|flac|tta|wv|wav into pieces with tags in them
  7.         ==> Needs a cuesheet, bchunk, and a little disk space
  8.         ==> Output format can be: [flac]
  9.         Usage: `basename "$0"` [-c cuesheet] file
  10. HELP
  11. }
  12. function fullpath()
  13. {
  14.         local DIR NAME
  15.         DIR=$(cd "$(dirname "$1")" && pwd)
  16.         NAME=$(basename "$1")
  17.         echo "$DIR"/"$NAME"
  18. }
  19. SIG='LoLiLoLiCoN'
  20. CUE=''
  21. BIG=''
  22. OPT_TEMP=$( getopt --longoptions cue:,help --options c:h -- "$@" )
  23. eval set -- "$OPT_TEMP"
  24. while : ; do case "$1" in
  25.         --cue|-c)        CUE=$(fullpath "$2") ; shift 2 ;;
  26.         --help|-h)        help  ; exit 0 ;;
  27.         --)                shift ; break  ;;
  28.         *)                help  ; exit 1 ;;
  29. esac ; done
  30. BIG="$1"
  31. if [ ! -f "$BIG" ] ; then help ; exit 1 ; else BIG=$(fullpath "$BIG") ; fi
  32. DIR=$(dirname "$BIG") && cd "$DIR"
  33. if [ "$?" -gt "0" ] ; then  echo "==> Can't enter working dir. Quit..." ; exit 1 ; fi
  34. if [ -f "$SIG"* ] ; then echo "==> WTF? $SIG stolen?!" ; exit 1 ; fi
  35. case "$BIG" in
  36.         *.[Aa][Pp][Ee])
  37.                 mac "$BIG" "$SIG".wav -d ;;
  38.         *.[Ff][Ll][Aa][Cc])
  39.                 flac -d -o "$SIG".wav "$BIG" ;;
  40.         *.[Tt][Tt][Aa])
  41.                 ttaenc -d -o "$SIG".wav "$BIG" ;;
  42.         *.[Ww][Vv])
  43.                 wvunpack "$BIG" -o "$SIG".wav ;;
  44.         *.[Ww][Aa][Vv])
  45.                 mv "$BIG" "$SIG".wav ;;
  46.         *)        echo "==> Unsupported input. Quit..." ; exit 1 ;;
  47. esac
  48. if [ "$?" -gt "0" ] ; then  echo "==> Decoder required. Grab it and try again. Quit..." ; exit 1 ; fi
  49. if [ -z "$CUE" ] ; then
  50.         N_CUE=$(ls -1 *.[Cc][Uu][Ee] | wc -l)
  51.         if [ "$N_CUE" == "1" ] ; then CUE=$(ls *.[Cc][Uu][Ee])
  52.         else
  53.                 echo "==> There are more than 1 cuesheets in the same dir."
  54.                 ls *.[Cc][Uu][Ee]
  55.                 echo "==> Use -c option to specify one. Quit..." ; exit 1
  56.         fi
  57. fi
  58. bchunk -w "$SIG".wav "$CUE" "$SIG"
  59. IFS='
  60. '
  61. TITLES=($(grep 'TITLE' "$CUE" | cut -d" -f2))
  62. ARTISTS=($(grep 'PERFORMER' "$CUE" | cut -d" -f2))
  63. ALBUM="${TITLES[0]}"
  64. N_TRACK="${#TITLES[@]}"
  65. unset IFS
  66. for ((N=1 ; N < N_TRACK ; N++)) ; do
  67.         NN=$(printf '%.2d' $N)
  68.         flac  -T "TRACKNUMBER=$NN" -T "ARTIST=${ARTISTS[$N]}" \
  69.               -T "ALBUM=${ALBUM}"  -T "TITLE=${TITLES[$N]}" \
  70.                      -o "$NN. ${TITLES[$N]}".flac "$SIG"$NN.wav
  71.               #GENRE? YEAR? DATE? no, keep it simple. You can do the tag stuff later anyway.
  72. done
  73. rm -f "$SIG"*.wav
复制代码
But there are several things I'd like to ask/point out:

1. cuesheets are not always encoded in utf8, which means it might get read incorrectly. iconv is great at charset conversion, but you must specify the input encoding yourself. Is there a way to automate input encoding detection, so all this charset conversion becomes painless? AFAIK, enca needs a language option and file doesn't always give you the right answer. If this automation can't be achieved, you'll have to check if the cuesheet is read correctly under your locale.

2. The script is based on my cuesheet format assumption. I don't know how a cuesheet can differ from others. But I used this template below. If I missed something, please let me know.
  1. CATALOG 4988017622131
  2. PERFORMER "Avril Lavigne"
  3. TITLE "Under My Skin"
  4. FILE "CDImage.ape" WAVE
  5.   TRACK 01 AUDIO
  6.     TITLE "Take Me Away"
  7.     PERFORMER "Avril Lavigne"
  8.     ISRC USAR10400373
  9.     INDEX 01 00:00:00
  10.   TRACK 02 AUDIO
  11.     TITLE "Together"
  12.     PERFORMER "Avril Lavigne"
  13.     ISRC USAR10400483
  14.     INDEX 01 02:57:45
  15.   TRACK 03 AUDIO
  16.     TITLE "Don't Tell Me"
  17.     PERFORMER "Avril Lavigne"
  18.     ISRC USAR10400119
  19.     INDEX 01 06:12:17
  20.   TRACK 04 AUDIO
  21.     TITLE "He Wasn't"
  22.     PERFORMER "Avril Lavigne"
  23.     ISRC USAR10400484
  24.     INDEX 01 09:34:18
  25.   TRACK 05 AUDIO
  26.     TITLE "How Does It Feel"
  27.     PERFORMER "Avril Lavigne"
  28.     ISRC USAR10400485
  29.     INDEX 00 12:33:45
  30.     INDEX 01 12:34:01
  31.   TRACK 06 AUDIO
  32.     TITLE "My Happy Ending"
  33.     PERFORMER "Avril Lavigne"
  34.     ISRC USAR10400486
  35.     INDEX 01 16:18:71
  36.   TRACK 07 AUDIO
  37.     TITLE "Nobody's Home"
  38.     PERFORMER "Avril Lavigne"
  39.     ISRC USAR10400487
  40.     INDEX 01 20:21:27
  41.   TRACK 08 AUDIO
  42.     TITLE "Forgotten"
  43.     PERFORMER "Avril Lavigne"
  44.     ISRC USAR10400488
  45.     INDEX 01 23:53:58
  46.   TRACK 09 AUDIO
  47.     TITLE "Who Knows"
  48.     PERFORMER "Avril Lavigne"
  49.     ISRC USAR10400489
  50.     INDEX 01 27:11:12
  51.   TRACK 10 AUDIO
  52.     TITLE "Fall To Pieces"
  53.     PERFORMER "Avril Lavigne"
  54.     ISRC USAR10400490
  55.     INDEX 01 30:40:60
  56.   TRACK 11 AUDIO
  57.     TITLE "Freak Out"
  58.     PERFORMER "Avril Lavigne"
  59.     ISRC USAR10400491
  60.     INDEX 01 34:09:36
  61.   TRACK 12 AUDIO
  62.     TITLE "Slipped Away"
  63.     PERFORMER "Avril Lavigne"
  64.     ISRC USAR10400492
  65.     INDEX 00 37:21:31
  66.     INDEX 01 37:22:58
  67.   TRACK 13 AUDIO
  68.     TITLE "I Always Get What I Want"
  69.     PERFORMER "Avril Lavigne"
  70.     ISRC USAR10400371
  71.     INDEX 00 40:56:65
  72.     INDEX 01 40:57:25
  73.   TRACK 14 AUDIO
  74.     TITLE "Nobody's Home (LIVE Acoustic)"
  75.     PERFORMER "Avril Lavigne"
  76.     ISRC USAR10400380
  77.     INDEX 00 43:28:54
  78.     INDEX 01 43:30:50
复制代码
# The 1st and 4th line, as well as all the ISRC lines are omitted.

3. Output format can be easily enhanced. ogg, mp3, aac, or whatever you like. But I just like it simple.

4. Well, I do know about shntool and cue2tracks, but never got them working properly... Guess I'm just too lazy to try them out.

Please help me to improve this.
发表于 2009-9-3 19:16:36 | 显示全部楼层
平时都是用虚拟机里的foobar转换的……

刚才试了一下,cue2tracks很好用啊,tag也都导到分割好的文件里去了。是不是你编译shntool之前没有先安装mac和ttaenc,导致不支持ape和tta?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-9-3 20:27:34 | 显示全部楼层
cue2tracks这个脚本是不错~ 看了下, 稍微有点长, 还没仔细瞧 ; 要读懂这个还得把cuetools也看看

我有装mac和ttaenc的, 也成功转过一个ape, 不过有个疑问... shntool干吗要patch过的mac?? 我的是没有patch的...

我使用bchunk, 感觉它比较专一... lol
回复 支持 反对

使用道具 举报

发表于 2009-9-3 21:48:56 | 显示全部楼层
还是XMMS2好...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-9-3 22:25:46 | 显示全部楼层
呵呵, mpd2不晓得什么时候出来

XMMS2用什么client好?
回复 支持 反对

使用道具 举报

发表于 2009-9-4 09:57:25 | 显示全部楼层
Post by lolilolicon;2023688
呵呵, mpd2不晓得什么时候出来

XMMS2用什么client好?


呃...我用lxmusic, 打了个添加CUE文件的补丁以后一切都还好啦...
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表