[Bash脚本]APE/Flac分轨转换成MP3,并正确写入id3v2
August 1st, 2008 | by vvoody |硬盘里有一些APE/Flac文件,越发觉得它们太占地方。这两种格式对我来说唯一的好处是,可以找到好唱片。故我准备把它们转换成mp3格式。网上搜了一下,发现这篇和那篇文章提供了一些解决方案。但前者需要生成中间文件,而且只支持flac格式; 后者无法写入mp3的tag信息,也就是 id3。所以,自己hack一下,有了下面两个shell脚本。
没啥技术含量,就是把各种工具组合一下,一劳永逸了;-) 我对shell用得不熟,如脚本有错误或不精确处,还请指正。
您需要:
- mac – 用于处理APE格式的;
- flac – 用于处理flac格式,多数发行版都已安装;
- lame – 著名的mp3编码器;
- cuetools – 从cue文件中提取信息;
- mutagen – 一个处理音乐文件tag信息的python库,我们用它来向mp3写入id3v2;
- shntool – 像是一个容器,在各种音乐格式间转换,非常强大!(墙ed)
如果你是用的Slackware,我用src2pkg把除了flac的都打了包,可以从这儿下载
用前请把你的 cue 文件转换成 UTF-8 编码。
这里有一个格式化加亮的代码版本。
用处一:
如果你有一个大的单个未分轨的APE/Flac文件,以及它对应的 cue 文件,想把它分轨并转换成mp3的话,并且要有tag信息的话,那么把下面这个脚本放到该 APE/Flac 所在目录,执行 ./one2mp3s.sh hifi.ape hifi.cue。自己根据实际情况改,支持 APE/Flac 格式。
#!/bin/bash # Split the big single ape/flac file and then convert to mp3 format. # Lastly, write the id3(version 2) tag info into the mp3s. # # Ensure that your cue file is *UTF-8* encoding. NO GB* id3! # # Required: mac, flac, lame, cuetools, mutagen, shntool # # Edited from Brian's Archive CUE/FLAC Splitter v0.1 # ;-) vvoody if [ "$1" == "--help" ]; then echo "=======================================" echo " Usage: $0 your.ape|flac your.cue" 1>&2 echo "=======================================" exit 0 fi if [ $# -lt 2 ]; then echo "=======================================" echo " Usage: $0 your.ape|flac your.cue" 1>&2 echo "=======================================" exit 1 fi hifile=$1 # .ape or .flac lossless audio file cuefile=$2 tracks=$(cueprint -d '%N' "$cuefile") # Store the idv3 info. We will write them to the mp3s later. id3count=1 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's 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 'cust ext=mp3 lame -b 192 - %f' "$hifile" # Remove the pregrap file, or it will make write the id3 incorrectly. if [ -f "00.pregap.mp3" ]; then rm -f 00.pregap.mp3 echo "00.pregap.mp3 found! Removed it." fi # Write the id3v2 into the mp3 files. acount=1 for mp3file in *.mp3; do mid3v2 --artist="${artist[$acount]}" --album="${album[acount]}" --track="${tracknum[acount]}" \ --song="${title[acount]}" "$mp3file" acount=$[$acount + 1] done
用处二:
如果你有一堆已经分轨的 APE/Flac 的文件,以及它们对应的 cue 文件,想把它们转换成mp3,并且要有tag信息,那么把下面这个脚本放到该 APE/Flac 所在目录,执行 ./all2mp3s.sh hifi.cue flac。支持 APE/Flac 格式。
#!/bin/bash # Convert the ape/flac files(splited) to mp3 format. # 01.a.flac, 02.xy.flac,... -> 01.a.mp3, 02.xy.mp3, ... # # ;-) vvoody if [ "$1" == "--help" ]; then echo "Usage: $0 your.cue orig_format" exit 1 fi cuefile=$1 ext=$2 count=1 for f in *.${ext}; do artist=$(cueprint -n$count -t '%p' "$cuefile") album=$(cueprint -n$count -t '%T' "$cuefile") tracknum=$(cueprint -n$count -t '%02n' "$cuefile") title=$(cueprint -n$count -t '%t' "$cuefile") shntool conv -i $ext -o 'cust ext=mp3 lame -b 192 - %f' "$f" mp3file="$(basename "$f" .$ext).mp3" echo "$mp3file" mid3v2 --artist="$artist" --album="$ablum" --track="$tracknum" \ --song="$title" "$mp3file" count=$[$count + 1] done
Enjoy
12 Responses to “[Bash脚本]APE/Flac分轨转换成MP3,并正确写入id3v2”
By latteye on Aug 2, 2008
Mozilla Firefox 3.0.1 Windows XP Mozilla Firefox 3.0.1 Windows XPthis command can put id3 info into mp3 files:
lame –noreplaygain -b 320 –ta “${artist[$convertcount]}” –tl “${album[$convertcount]}” –tn “${tracknum[$convertcount]}” –tt “${title[$convertcount]}” “$wavenum.wav” “${tracknames[$convertcount]}.mp3″
rm ./”$wavenum.wav”
Reply
By 18 on Aug 6, 2008
Mozilla Firefox 3.0.1 Linux Mozilla Firefox 3.0.1 Linux潮潮你天才了!我很早以前就在找这么个玩意儿。
以前我电脑里接近10G的APE音乐,都是一个一个用Monkey’s Audio转成WAV再转MP3的,只是装Ubuntu的时候把那个分区整个删掉了…
不过貌似Windows下有现成的这种工具——千千静听,可以直接把APE转成MP3 or another 支持的格式,你知道否?
Reply
By vvoody on Aug 6, 2008
Opera 9.51 Linux Opera 9.51 Linux@18
千千静听知道,但从没用过。
如果你要改成别的格式,我的脚本你改一下就行。
不过,mp3够普遍了吧,你还想转成什么格式?ogg?wma?
Reply
By bitstream on Aug 22, 2008
Mozilla Firefox 3.0.1 Fedora Linux Mozilla Firefox 3.0.1 Fedora Linux不知道APE/Flac是干吗的。:-)
Reply
By vvoody on Aug 24, 2008
Opera 9.52 Linux Opera 9.52 Linux@bitstream
嘿嘿,一种音乐格式啦~
Reply
By vvoody on Aug 24, 2008
Opera 9.52 Linux Opera 9.52 Linux我留个言~
Reply
By 墮天使-祥 on Sep 30, 2008
Mozilla Firefox 3.0.1 Windows XP Mozilla Firefox 3.0.1 Windows XP可惜我使用Fedora 9,很多軟件都要自己下載。
Reply
vvoody Reply:
Mozilla Firefox 2.0.0.16 LinuxSeptember 30th, 2008 at 23:41
离开了这么些强大的工具,很难办到的。
Reply
By helphouse.cn on Feb 28, 2010
Mozilla Firefox 3.0.18 Windows XP Mozilla Firefox 3.0.18 Windows XP助房网-装修改造、房屋维修专业网站!
http://www.HelpHouse.cn
[URL=http://www.helphouse.cn]www.helphouse.cn[/url]
Reply
By ghd on Jul 30, 2010
Mozilla Firefox 3.6.3 Windows XP Mozilla Firefox 3.6.3 Windows XPi believe you are a good writer, but have you erver thought to write some special rtcals for peopel who likes shopping very much wansantg2zxy.
Reply