Thursday, April 22, 2010

周末整理音乐收藏

终于把所有的无损收藏都变成flac了,ape本来没啥不好的,但是几乎所有的便携播放设备都不支持。又创新打算出支持flac的播放器了,又可能败一只,所以就折腾了一下。其实人倒是没怎么动手,不过我弱弱的旧笔记本可是忙了好半天呐……

Convert all ape files to flac

#!/bin/bash
# by huangyuanjie at gmail dot com
# convert all ape files to flac

find $PWD -iname '*.ape' -print > todo

while read p ; do
d=`dirname "$p"`
f=`basename "${p}" .ape`
pn="${d}/${f}.flac"

echo "start converting \"${p}\" to"
echo " \"${pn}\" ..."
sleep 1
mac "${p}" - -d 2>/dev/null | flac -o "${pn}" -
echo "${p}" >> finished-convert
echo "converted \"${p}\" to "
echo " \"${pn}\" ...";

find "${d}" -iname '*.cue' -print > tmp
while read f; do
echo "fixing \"${f}\" ..."
cp "${f}" "${f}.orig"
sed -i 's/ape/flac/g' "${f}"
addsig.py "${f}" # add UTF-8 BOM so that foobar2000 can read it correctly
echo "fixed \"${f}\" ."
done < tmp
echo "${p}" >> finished-fix-cuesheet

rm -v "${p}"
echo

sleep 1
done < todo

rm tmp


Python script to add UTF-8 BOM



#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# by huangyuanjie at gmail dot com
# add UTF-8 BOM so to a text file

import sys
import codecs

def main():
if len(sys.argv) < 2:
sys.stderr.write("Error: no input file!")
exit(1)
path = sys.argv[1]
try:
file = open(path, "rb")
except IOError:
sys.stderr.write("Error: no such file \"%s\" !" % (path))
exit(2)

content = file.read()
file.close()
if str(content[0:len(codecs.BOM_UTF8)]) == str(codecs.BOM_UTF8):
return
else:
file = open(path, "w")
file.write(codecs.BOM_UTF8)
file.write(content)
file.close()

if __name__ == '__main__':
main()

No comments:

Post a Comment