Skip to content

Instantly share code, notes, and snippets.

@rfc1459
Created January 27, 2012 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfc1459/1690183 to your computer and use it in GitHub Desktop.
Save rfc1459/1690183 to your computer and use it in GitHub Desktop.
Damn iTunes Store...
#!/usr/bin/python -u
# Quick and dirty script to transcode to FLAC all AAC-LC files
# rooted into the current working directory (subdirs incl.)
import os
import sys
import subprocess
import magic
def collect_aac_lc():
cwd = os.path.abspath(os.getcwd())
m4a = []
m = magic.open(magic.MAGIC_NONE)
m.load()
for root, dirs, files in os.walk(cwd):
for f in files:
if f.endswith('.m4a'):
fullpath = os.path.join(root, f)
if m.file(fullpath).find('AAC-LC') > -1:
m4a.append(fullpath)
m.close()
return m4a
def convert_to_flac(aacs):
errs = []
for aac in aacs:
(fn, _) = os.path.splitext(aac)
flac = fn + '.flac'
ps = subprocess.Popen(['/usr/bin/ffmpeg', '-i', aac, '-acodec', 'flac', flac], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if ps.returncode == 0:
os.unlink(aac)
sys.stdout.write(".")
else:
errs.append(aac)
sys.stdout.write("!")
return errs
if __name__ == '__main__':
m4as = collect_aac_lc()
l = len(m4as)
if l == 0:
print "[!] No AAC-LC files, exiting"
sys.exit(1)
else:
print "[*] Found %d AAC-LC file%s" % (l, 's' if l > 1 else '')
sys.stdout.write("[*] Transcoding")
errs = convert_to_flac(m4as)
print ""
for f in errs:
print "[!] %s was not transcoded successfully, kept" % (f,)
print "[*] Done"
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment