Skip to content

Instantly share code, notes, and snippets.

@thefinn93
Created May 4, 2015 23:52
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 thefinn93/4f2fa9821acef8a205f6 to your computer and use it in GitHub Desktop.
Save thefinn93/4f2fa9821acef8a205f6 to your computer and use it in GitHub Desktop.
Rivendell Exporter (and converter and ID3 tagger)
#!/usr/bin/env python
# Rivendell Exporter
# Copyright (C) 2015 Finn Herzfeld
#
# Rivendell Exporter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Rivendell Exporter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rivendell Exporter. If not, see <http://www.gnu.org/licenses/>.
## USAGE ##
# Create a config file such as the one included with script. The
# database settings are self explanatory, the file must include a
# source directory (source), a destination directory to output to
# (dest), and a format to convert to (format). The format should be
# a filename, it will be appended to filename, then fed to avconv.
# Provide the path to the config file as a command line argument.
## Dependencies ##
# There are several python libraries requires:
# * PyMySQL
# * eyed3
#
# Additionall, avconv must be installed in the system PATH
import pymysql.cursors
import eyed3
import os
import sys
import subprocess
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read(['settings.ini', 'settings.cfg'] + sys.argv[1:])
print(sys.argv[1:])
indir = config.get('files', 'source')
outdir = config.get('files', 'dest')
outformat = config.get('files', 'format')
connection = pymysql.connect(host=config.get('database', 'host'),
user=config.get('database', 'user'),
passwd=config.get('database', 'pass'),
db=config.get('database', 'db'),
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
def cleanfn(filename):
characters = [',', '.', '_', ' ']
return "".join([c for c in filename if c.isalpha() or c.isdigit() or c in characters]).rstrip()
try:
with connection.cursor() as cursor:
print("Connected to database")
# Read a single record
sql = "SELECT `NUMBER`, `TITLE`, `ARTIST`, `ALBUM`, `YEAR` FROM `CART`"
sql += "WHERE `GROUP_NAME`='MUSIC'"
cursor.execute(sql)
row = cursor.fetchone()
while(row is not None):
if row['ARTIST'] is None:
row['ARTIST'] = ""
print("No artist for track %s (%s)" % (row['NUMBER'], row['TITLE']))
elif not os.path.isdir("%s/%s" % (outdir, cleanfn(row['ARTIST']))):
print("Creating directory %s/%s" % (outdir, cleanfn(row['ARTIST'])))
os.mkdir("%s/%s" % (outdir, cleanfn(row['ARTIST'])))
inputfn = "{0}/{1:06d}_001.wav".format(indir, row['NUMBER'])
outputfn = "{0}/{1}/{2}.{3}".format(outdir,
cleanfn(row['ARTIST']),
cleanfn(row['TITLE']),
outformat)
command = ["avconv", "-i", inputfn, outputfn]
print("Converting %s by %s..." % (row['TITLE'], row['ARTIST']))
subprocess.call(command)
print("Complete! Updating ID3 tags on %s" % outputfn)
outfile = eyed3.load(outputfn)
outfile.tag.title = row['TITLE']
if row['ALBUM'] != "" and row['ALBUM'] is not None:
outfile.tag.album = row['ALBUM']
if row['ARTIST'] != "" and row['ARTIST'] is not None:
outfile.tag.artist = row['ARTIST']
outfile.tag.save()
row = cursor.fetchone()
finally:
print("Closing database connection")
connection.close()
[database]
host = localhost
user = rivendell
pass = supersecure
db = rivendell
[files]
source = /var/snd
dest = /mnt/externaldrive
format = mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment