Skip to content

Instantly share code, notes, and snippets.

@KingYes
Created November 26, 2013 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingYes/7659440 to your computer and use it in GitHub Desktop.
Save KingYes/7659440 to your computer and use it in GitHub Desktop.
Encode audio files (VBR Profile) and preserve id3 tags used with `lame` program.
#!/bin/python
# -*- coding: utf-8 -*-
"""
Encode audio files (VBR Profile) and preserve id3 tags
used with `lame` program.
@Author: Yakir Sitbon <http://www.yakirs.net/>
@Version: 1.0
"""
import os
import sys
from mutagen.easyid3 import EasyID3
lame_fields = {
'title': 'tt',
'artist': 'ta',
'album': 'tl',
'date': 'ty',
'genre': 'tg',
'tracknumber': 'tn',
}
if 3 != len(sys.argv):
print 'Use: %s <source-file> <target-file>' % (sys.argv[0])
exit()
try:
source_id3_file = EasyID3(sys.argv[1])
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
exit()
except:
print "Unexpected error:", sys.exc_info()[0]
exit()
lame_args = []
for key in source_id3_file:
if key in lame_fields:
lame_args.append('--%s "%s"' % (lame_fields[key],
source_id3_file[key][0]))
lame_args.append(sys.argv[1])
lame_args.append(sys.argv[2])
lame_command = '%s %s' % ('lame --vbr-new -V2', ' '.join(lame_args))
os.system(lame_command.encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment