From a236ef5a51f52e89ed9a69bd1d0f2d8d231ded45 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 27 Apr 2014 15:34:09 -0700 Subject: [PATCH] it's workinggggg --- convert.py | 12 ++++++++++++ unsync.py | 34 +++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 convert.py diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..96ff42c --- /dev/null +++ b/convert.py @@ -0,0 +1,12 @@ +import subprocess as sp +import tempfile + +def ogg(inf): + _, temppath = tempfile.mkstemp() + p1 = sp.Popen(["ffmpeg", "-loglevel", "warning", "-i", inf, "-f", "flac", "-"], stdout=sp.PIPE) + p2 = sp.Popen(["oggenc", "-Q", "-", "-o", temppath], stdin=p1.stdout, stdout=sp.PIPE) + p1.stdout.close() + _, err = p2.communicate() + if err: + raise Exception(err) + return temppath diff --git a/unsync.py b/unsync.py index 6acc894..9625c36 100755 --- a/unsync.py +++ b/unsync.py @@ -6,8 +6,11 @@ from __future__ import print_function import os import os.path import sys +import shutil import mutagen + import mutaext +import convert updatabletags = [\ 'albumartist', 'composer', 'comment' \ @@ -17,6 +20,9 @@ updatabletags = [\ updatabletags.extend(mutaext.replaygain_tags) updatabletags.extend(mutaext.extra_tags) +alltags = list(updatabletags) +alltags.extend(['artist', 'album', 'title']) + def G(d, k): try: return d[k] @@ -39,7 +45,7 @@ def shouldsync(md): sync = G(md, 'sync') try: rating = int(rating[0]) - except (IndexError, ValueError): + except: pass if sync: sync = sync[0].lower() @@ -102,6 +108,15 @@ def updatemetadata(mdold, mdnew): modified = True return modified +def makefilename(md): + fn = "" + title = md['title'][0] + artist = md['artist'][0] + album = md['album'][0] + fn = u"{1} - {2} - {0}".format(title,artist,album) + fn += ".ogg" + return fn + def run(args): if not len(args) in (2, 3): print("I need a path or two!", file=sys.stderr) @@ -129,8 +144,8 @@ def run(args): if inonly: return 0 - # TODO: don't print anything - print("Beginning matching...", file=sys.stderr) + + print("Matching...", file=sys.stderr) outdir = args[2] for p in paths(outdir): @@ -141,12 +156,21 @@ def run(args): print("UPD", p) md.save() else: - pass #print("DEL", p) - # delete files here + print("DEL", p) + os.remove(p) for md in tosync: if md.seen == False: print("ADD", md.path) + outf = os.path.join(outdir, makefilename(md)) + #print("TO", outf) + temppath = convert.ogg(md.path) + mdnew = mutagen.File(temppath, easy=True) + for tag in alltags: + if tag in md: + mdnew[tag] = md[tag] + mdnew.save() + shutil.move(temppath, outf) return 0