Grissiom 帮我完善的 APE/Flac 转 mp3/ogg 脚本

October 19th, 2008 | by vvoody |

一开始我的脚本只支持转换成 mp3,后来 grissiom 提出并自己增加了 ogg 格式的支持,下面贴出比较完善的版本。
主要改进:

  • 增加转换格式选项,支持 mp3/ogg;
  • 把 your.ape|flac 这个参数去掉了,现在脚本可以从 cue 里面提取文件名;
  • 修改了程序说明,优化程序结构;
  • 写入 mp3 的 id3 采用 slackbuilds.org 上的 id3v2,你也可以继续采用 mutagen,修改相应注释即可;

感谢 Grissiom ^_^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
 
# Split the big single ape/flac file to mp3 or ogg files.
# Lastly, write the id3(version 2) tag info into the mp3s or oggs.
#
# Ensure that your cue file is *UTF-8* encoding. NO GB* id3!
#
# Required: mac, flac, lame, cuetools, id3v2(or mutagen), shntool, vorbis-tools
#
# Edited from Brian Archive CUE/FLAC Splitter v0.1
# ;-) vvoody <wxj.g.sh{at}gmail.com>
# ogg convert function added at 2008-10-19
#     by Grissiom <http://grissiom.blogspot.com/>
 
echo_usage() {
	echo
	echo "usage: $0 [options [encoding options]] <cuefile>"
	echo
	echo "OPTIONS:"
	echo "    mp3: split sndfile into mp3 files."
	echo "    ogg: split sndfile into ogg files."
	echo "    encoding options: options pass to the encoder,"
        echo "                  must enclosed in quotation marks"
	echo
}
 
case $1 in
	"--help" )
		echo_usage
		exit 0
		;;
	"mp3" )
		ext='mp3'
		encmd='cust ext=mp3 lame -b 192 - %f'
		;;
	"ogg" )
		ext='ogg'
		encmd='cust ext=ogg oggenc - -o %f'
		;;
	* )
		echo_usage
		exit 1
		;;
esac
 
 
if [ $# -eq 2 ]; then
	cuefile=$2
elif [ $# -eq 3 ]; then
	enopt=$2
	cuefile=$3
else
	echo_usage
	exit 1
fi
 
sndfile=`egrep '^FILE' $cuefile | awk -F'"' '{print $2}'`
# According to http://digitalx.org/cuesheetsyntax.php ,
# the file name may not be enclosed  in quotation marks.
if [ -z $sndfile ]; then
	sndfile=`egrep '^FILE' $cuefile | awk -F' ' '{print $2}'`
fi
 
tracks=$(cueprint -d '%N' "$cuefile")
genre=$(cueprint -d '%G' "$cuefile")
 
# Store the idv3 info. We will write them to the mp3s later.
id3count=1
echo "Disk Genre: "$genre
echo $tracks "tracks altogether."
echo
while [ $id3count -le $tracks ]; do
	artist[$id3count]=$(cueprint -n$id3count -t '%p' "$cuefile")
	album[$id3count]=$(cueprint -n$id3count -t '%T' "$cuefile")
	tracknum[$id3count]=$(cueprint -n$id3count -t '%02n' "$cuefile")
	title[$id3count]=$(cueprint -n$id3count -t '%t' "$cuefile")
	echo "Artist      - ${artist[$id3count]}"
	echo "Album       - ${album[$id3count]}"
	echo "Track No.   - ${tracknum[$id3count]}"
	echo "Track Title - ${title[$id3count]}"
	echo
	id3count=$[$id3count + 1]
done
 
echo "=================================================="
 
# Split and convert the single ape/flac file.
# Each mp3 name is like: "07.Yesterday Once More.mp3"
# Default bit rate is 128, you can customize it by using -b option.
# More output format, see `man shntool`
 shntool split -f "$cuefile" -t '%n.%t' -o "$encmd" "$sndfile"
 
# Remove the pregrap file, or it will make write the id3 incorrectly.
if [ -f "00.pregap."$ext ]; then
	rm -f 00.pregap.$ext
	echo "00.pregap.$ext found! Removed it."
fi
 
 
# Write the id3v2 into the output files.
acount=1
for outfile in *.$ext; do
	if [ $ext = 'mp3' ]; then
# 		mid3v2 --artist="${artist[$acount]}" \
# 		--album="${album[acount]}" \
# 		--track="${tracknum[acount]}" \
# 		--song="${title[acount]}" \
# 		"$outfile"
		id3tag \
		--artist="${artist[$acount]}" \
		--album="${album[acount]}" \
		--track="${tracknum[acount]}" \
		--song="${title[acount]}" \
		--total="${tracks}" \
		--genre="${genre}" \
		"$outfile"
	elif [ $ext = 'ogg' ]; then
		vorbiscomment -aR \
		-t "Artist=${artist[$acount]}" \
		-t "Album=${album[acount]}" \
		-t "Title=${title[acount]}" \
		-t "Genre=${genre}" \
		-t "totaltracks=${tracks}" \
		-t "tracknumber=${tracknum[acount]}" \
		"$outfile"
	fi
	acount=$[$acount + 1]
done
 
# End of script.

Post a Comment

To submit your comment, click the image below where it asks you to...
根据下面图中的提示点击后提交留言... Clickcha - The One-click Captcha