Source

rgain / rgain / script / collectiongain.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# -*- coding: utf-8 -*-
# kate: indent-width 4; indent-mode python
#
# Copyright (c) 2009, 2010, 2012 Felix Krull <f_krull@gmx.de>
#
# This program 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, or (at your option)
# any later version.
# 
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import contextlib
import multiprocessing
import cStringIO
import sys
import os.path
try:
    from hashlib import md5
except ImportError:
    from md5 import new as md5
try:
    import cPickle as pickle
except ImportError:
    import pickle

import mutagen
from mutagen.id3 import TXXX

from rgain import rgio
from rgain.script import ou, un, Error, common_options
from rgain.script.replaygain import do_gain


# all of collectiongain
def relpath(path, base):
    size = len(base)
    if os.path.basename(base):
        # this means ``base`` does not end with a separator
        size += 1
    return path[size:]


def read_cache(cache_file):
    if not os.path.isfile(cache_file):
        files = {}
    else:
        try:
            f = open(cache_file, "rb")
            files = pickle.load(f)
        except Exception, exc:
            print ou(u"Error while reading the cache - %s" % exc)
        finally:
            try:
                f.close()
            except NameError:
                pass

    return files


def write_cache(cache_file, files):
    cache_dir = os.path.dirname(cache_file)

    try:
        if not os.path.isdir(cache_dir):
            os.makedirs(cache_dir, 0755)
        f = open(cache_file, "wb")
        pickle.dump(files, f, 2)
    except Exception, exc:
        print ou(u"Error while writing the cache - %s" % exc)
    finally:
        try:
            f.close()
        except NameError:
            pass


def validate_cache(files):
    for filepath, record in files.items():
        if (isinstance(filepath, basestring) and
            hasattr(record, "__getitem__") and
            hasattr(record, "__len__") and
            len(record) == 3 and
            (isinstance(record[0], basestring) or
             record[0] is None) and
            (isinstance(record[1], int) or
             isinstance(record[1], float)) and
            isinstance(record[2], bool)
        ):
            continue
        else:
            # funny record, purge it
            del files[filepath]


def get_album_id(music_dir, filepath):
    properpath = os.path.join(music_dir, filepath)
    ext = os.path.splitext(filepath)[1]
    try:
        tags = mutagen.File(properpath)
    except Exception, exc:
        raise Error(u"%s: %s" % (filepath, exc))

    album_id = None
    if ext == ".mp3":
        for frame in tags.itervalues():
            if isinstance(frame, TXXX) and frame.desc == "MusicBrainz Album Id":
                album_id = frame.text[0]
                break
    else:
        try:
            album_id = tags.get("musicbrainz_albumid")[0]
        except TypeError:
            pass

    if album_id is not None:
        return album_id

    if ext == ".mp3":
        if "TALB" in tags:
            album = tags["TALB"].text[0]
        else:
            album = None
    else:
        album = tags.get("album", [""])[0]

    if album:
        if ext == ".mp3":
            artist = None
            for frame in tags.itervalues():
                if isinstance(frame, TXXX) and "albumartist" in frame.desc:
                    # these heuristics are a bit fragile
                    artist = frame.text[0]
                    break
            if not artist:
                # TODO: is this correct?
                if "TPE1" in tags:
                    artist = tags["TPE1"].text[0]
        else:
            artist = tags.get("albumartist") or tags.get("artist")
            if artist:
                artist = artist[0]

        if not artist:
            artist = u""
        album_id = u"%s - %s" % (artist, album)
    else:
        album_id = None

    return album_id


def collect_files(music_dir, files, cache, supported_formats):
    i = 0
    for dirpath, dirnames, filenames in os.walk(music_dir):
        for filename in filenames:
            filepath = un(relpath(os.path.join(dirpath, filename), music_dir),
                                  sys.getfilesystemencoding())
            properpath = os.path.join(dirpath, filename)
            mtime = os.path.getmtime(properpath)

            # check the cache
            if filepath in cache:
                cache[filepath] = True
                record = files[filepath]
                if mtime <= record[1]:
                    # the file's still ok
                    continue

            ext = os.path.splitext(filename)[1]
            if ext in supported_formats:
                i += 1
                print ou(u"  [%i] %s |" % (i, filepath)),
                album_id = get_album_id(music_dir, filepath)
                print ou(album_id or u"<single track>")
                # fields here: album_id, mtime, already_processed
                files[filepath] = (album_id, mtime, False)


def transform_cache(files, ignore_cache=False):
    # transform ``files`` into a usable data structure
    albums = {}
    single_tracks = []
    for filepath, (album_id, mtime, processed) in files.iteritems():
        if album_id is not None:
            albums.setdefault(album_id, []).append(filepath)
        else:
            single_tracks.append(filepath)

    # purge anything that's marked as processed, if desired
    if not ignore_cache:
        for album_id, album_files in albums.items():
            keep = False
            for filepath in album_files:
                if not files[filepath][2]:
                    keep = True
                    break
            if not keep:
                del albums[album_id]

        for filepath in single_tracks[:]:
            if files[filepath][2]:
                single_tracks.remove(filepath)

    return albums, single_tracks


def update_cache(files, music_dir, tracks, album_id):
    for filepath in tracks:
        mtime = os.path.getmtime(os.path.join(music_dir, filepath))
        files[filepath] = (album_id, mtime, True)

@contextlib.contextmanager
def stdstreams(stdout, stderr):
    old_stdout = sys.stdout
    old_stderr = sys.stderr
    sys.stdout = stdout
    sys.stderr = stderr
    try:
        yield
    finally:
        sys.stdout = old_stdout
        sys.stderr = old_stderr

def do_gain_async(queue, job_key, files, ref_level, force, dry_run, album,
        mp3_format):
    output = cStringIO.StringIO()
    try:
        with stdstreams(output, output):
            if album:
                print ou(u"%s:" % job_key[1]),
            do_gain(files, ref_level, force, dry_run, album, mp3_format)
            print
    except Exception, exc:
        # We can't reliably serialise and pass the exception information to the
        # driver process so we stringify it here.
        queue.put((job_key, output.getvalue(), unicode(exc)))
    else:
        queue.put((job_key, output.getvalue(), None))


def do_gain_all(music_dir, albums, single_tracks, files, ref_level=89,
              force=False, dry_run=False, mp3_format=None, jobs=0,
              stop_on_error=False):
    pool = multiprocessing.Pool(None if jobs == 0 else jobs)
    manager = multiprocessing.Manager()
    queue = manager.Queue()
    num_jobs = 0

    print "Dispatching jobs ..."
    if single_tracks:
        pool.apply_async(do_gain_async, [queue, (single_tracks, None),
            [os.path.join(music_dir, path) for path in single_tracks],
            ref_level, force, dry_run, False, mp3_format])
        num_jobs += 1

    for album_id, album_files in albums.iteritems():
        #print ou(u"%s:" % album_id),
        pool.apply_async(do_gain_async, [queue, (album_files, album_id),
            [os.path.join(music_dir, path) for path in album_files],
            ref_level, force, dry_run, True, mp3_format])
        num_jobs += 1
    pool.close()

    print "Now waiting for results ..."
    failed_jobs = []
    try:
        all_jobs = num_jobs
        successful = 0
        while num_jobs > 0:
            job_key, output, exc = queue.get()
            num_jobs -= 1
            if exc:
                failed_jobs.append((job_key, output, exc))
            else:
                successful += 1
                print output.strip()
                print "Successfully finished %s of %s." % (successful, all_jobs)
                print
            # Update cache.
            if not dry_run:
                tracks, album_id = job_key
                update_cache(files, music_dir, tracks, album_id)
    finally:
        try:
            pool.terminate()
        except Exception:
            # terminate ends rather horribly so we just silence it.
            pass
        if len(failed_jobs) > 0:
            print "Unfortunately, there were some errors:"
            for key, output, exc in failed_jobs:
                print output.strip()
                print >> sys.stderr, ou(exc)
                print
        print "%s successful, %s failed." % (successful, len(failed_jobs))


def do_collectiongain(music_dir, ref_level=89, force=False, dry_run=False,
                      mp3_format=None, ignore_cache=False, jobs=0):
    music_dir = un(music_dir, sys.getfilesystemencoding())

    music_abspath = os.path.abspath(music_dir)
    musicpath_hash = md5(music_abspath.encode("utf-8")).hexdigest()
    cache_file = os.path.join(os.path.expanduser("~"), ".cache",
                              "collectiongain-cache.%s" % musicpath_hash)

    # load the cache
    files = read_cache(cache_file)

    # yeah, side-effects are bad, I know
    validate_cache(files)
    cache = dict.fromkeys(files.iterkeys(), False)

    print "Collecting files ..."

    # whenever this part is stopped (KeyboardInterrupt/other exception), the
    # cache is written to disk so all progress persists
    try:
        collect_files(music_dir, files, cache,
                      rgio.BaseFormatsMap(mp3_format).supported_formats)
        # clean cache
        for filepath, visited in cache.items():
            if not visited:
                del cache[filepath]
                del files[filepath]
        # hopefully gets rid of at least one huge data structure
        del cache

        albums, single_tracks = transform_cache(files, ignore_cache)

        # gain everything that has survived the cleansing
        do_gain_all(music_dir, albums, single_tracks, files, ref_level, force,
                  dry_run, mp3_format, jobs)
    finally:
        validate_cache(files)
        write_cache(cache_file, files)

    print "All finished."


def collectiongain_options():
    opts = common_options()

    opts.add_option("--ignore-cache", help="Don't trust implicit assumptions "
                    "about what was already done, instead check all files for "
                    "Replay Gain data explicitly.", dest="ignore_cache",
                    action="store_true")
    opts.add_option("-j", "--jobs", help="Specifies the number of jobs to run "
                    "simultaneously. Must be >= 1. By default, this is set to "
                    "the number of CPU cores in the system to provide best "
                    "performance.", dest="jobs", action="store", type="int")

    opts.set_defaults(ignore_cache=False, jobs=None)

    opts.set_usage("%prog [options] MUSIC_DIR")
    opts.set_description("Calculate Replay Gain for a large set of audio files "
                         "without asking many questions. This program "
                         "calculates an album ID for any audo file in "
                         "MUSIC_DIR. Then, album gain will be applied to all "
                         "files with the same album ID. The album ID is "
                         "created from file tags as follows: If an 'album' tag "
                         "is present, it is joined with the contents of an "
                         "'albumartist' tag, or, if that isn't set, the "
                         "contents of the 'artist' tag, or nothing if there is "
                         "no 'artist' tag as well. On the other hand, if no "
                         "'album' tag is present, the file is assumed to be a "
                         "single track without album; in that case, no album "
                         "gain will be calculated for that file.")
    
    return opts


def collectiongain():
    optparser = collectiongain_options()
    opts, args = optparser.parse_args()
    if len(args) != 1:
        optparser.error("pass one directory path")
    if opts.jobs is not None and opts.jobs < 1:
        optparser.error("jobs must be at least 1")
    
    try:
        do_collectiongain(args[0], opts.ref_level, opts.force, opts.dry_run,
                          opts.mp3_format, opts.ignore_cache, opts.jobs)
    except Error, exc:
        print
        print >> sys.stderr, ou(unicode(exc))
        sys.exit(1)
    except KeyboardInterrupt:
        print "Interrupted."


if __name__ == "__main__":
    collectiongain()