Source

eyeD3 / src / eyed3 / plugins / statistics.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
404
405
# -*- coding: utf-8 -*-
################################################################################
#  Copyright (C) 2009  Travis Shirk <travis@pobox.com>
#
#  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 of the License, 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
#
################################################################################
from __future__ import print_function
import sys, os, operator
from collections import Counter

from eyed3 import id3, mp3
from eyed3.core import AUDIO_MP3
from eyed3.utils import guessMimetype, cli
from eyed3.plugins import LoaderPlugin

ID3_VERSIONS = [id3.ID3_V1_0, id3.ID3_V1_1,
                id3.ID3_V2_2, id3.ID3_V2_3, id3.ID3_V2_4]

_OP_STRINGS = {operator.le: "<=",
               operator.lt: "< ",
               operator.ge: ">=",
               operator.gt: "> ",
               operator.eq: "= ",
               operator.ne: "!=",
              }

class Rule(object):
    def test(self):
        raise NotImplementedError()


PREFERRED_ID3_VERSIONS = [ id3.ID3_V2_3,
                           id3.ID3_V2_4,
                         ]
class Id3TagRules(Rule):
    def test(self, path, audio_file):
        scores = []

        if audio_file is None:
            return None

        if not audio_file.tag:
            return [(-75, "Missing ID3 tag")];

        tag = audio_file.tag
        if tag.version not in PREFERRED_ID3_VERSIONS:
            scores.append((-30, "ID3 version not in %s" %
                                PREFERRED_ID3_VERSIONS))
        if not tag.title:
            scores.append((-20, "Tag missing title"))
        if not tag.artist:
            scores.append((-18, "Tag missing artist"))
        if not tag.album:
            scores.append((-16, "Tag missing album"))
        if not tag.track_num[0]:
            scores.append((-14, "Tag missing track number"))
        if not tag.track_num[1]:
            scores.append((-12, "Tag missing total # of tracks"))

        return scores


BITRATE_DEDUCTIONS = [(192, -20), (256, -10)]
class BitrateRule(Rule):
    def test(self, path, audio_file):
        scores = []

        if not audio_file:
            return None

        is_vbr, bitrate = audio_file.info.bit_rate
        for threshold, score in BITRATE_DEDUCTIONS:
            if bitrate < threshold:
                scores.append((score, "Bit rate < %d" % threshold))
                break

        return scores


VALID_MIME_TYPES = mp3.MIME_TYPES + [ "image/png",
                                      "image/gif",
                                      "image/jpeg",
                                    ]
class MimeTypeRule(Rule):
    def test(self, path, audio_file):
        mt = guessMimetype(path)
        if mt not in VALID_MIME_TYPES:
            return [(-100, "Unsupported file type: %s" % mt)]
        return None


VALID_ARTWORK_NAMES = ("cover", "cover-front", "cover-back")
class ArtworkRule(Rule):
    def test(self, path, audio_file):
        mt = guessMimetype(path)
        if mt and mt.startswith("image/"):
            name, ext = os.path.splitext(os.path.basename(path))
            if name not in VALID_ARTWORK_NAMES:
                return [(-10, "Artwork file not in %s" %
                              str(VALID_ARTWORK_NAMES))]

        return None


class Stat(Counter):
    TOTAL = "total"

    def __init__(self, *args, **kwargs):
        super(Stat, self).__init__(*args, **kwargs)
        self[self.TOTAL] = 0
        self._key_names = {}

    def compute(self, file, audio_file):
        self[self.TOTAL] += 1
        self._compute(file, audio_file)

    def _compute(self, file, audio_file):
        pass

    def report(self):
        self._report()

    def _sortedKeys(self, most_common=False):
        def keyDisplayName(k):
            return self._key_names[k] if k in self._key_names else k

        key_map = {}
        for k in self.keys():
            key_map[keyDisplayName(k)] = k

        if not most_common:
            sorted_names = list(key_map.keys())
            sorted_names.remove(self.TOTAL)
            sorted_names.sort()
            sorted_names.append(self.TOTAL)
        else:
            most_common = self.most_common()
            sorted_names = []
            remainder_names = []
            for k, v in most_common:
                if k != self.TOTAL and v > 0:
                    sorted_names.append(keyDisplayName(k))
                elif k != self.TOTAL:
                    remainder_names.append(keyDisplayName(k))

            remainder_names.sort()
            sorted_names = sorted_names + remainder_names
            sorted_names.append(self.TOTAL)

        return [key_map[name] for name in sorted_names]

    def _report(self, most_common=False):
        keys = self._sortedKeys(most_common=most_common)

        key_col_width = 0
        val_col_width = 0
        for key in keys:
            key = self._key_names[key] if key in self._key_names else key
            key_col_width = max(key_col_width, len(str(key)))
            val_col_width = max(val_col_width, len(str(self[key])))
        key_col_width += 1
        val_col_width += 1

        for k in keys:
            key_name = self._key_names[k] if k in self._key_names else k
            value = self[k]
            percent = self.percent(k) if value and k != "total" else ""
            print("%(padding)s%(key)s:%(value)s%(percent)s" %
                  { "padding": ' ' * 4,
                    "key":   str(key_name).ljust(key_col_width),
                    "value": str(value).rjust(val_col_width),
                    "percent": " ( %s%.2f%%%s )" %
                                 (cli.GREEN, percent, cli.RESET) if percent
                                                                 else "",
                  })

    def percent(self, key):
        return (float(self[key]) / float(self["total"])) * 100


class AudioStat(Stat):
    def compute(self, audio_file):
        assert(audio_file)
        self["total"] += 1
        self._compute(audio_file)

    def _compute(self, audio_file):
        pass


class FileCounterStat(Stat):
    def __init__(self):
        super(FileCounterStat, self).__init__()
        for k in ("audio", "hidden", "audio (other)"):
            self[k] = 0

    def _compute(self, file, audio_file):
        if audio_file:
            self["audio"] += 1
        if os.path.basename(file).startswith('.'):
            self["hidden"] += 1
        mt = guessMimetype(file)
        if mt and mt.startswith("audio/") and not audio_file:
            self["unsupported (other)"] += 1

    def _report(self):
        print(cli.BOLD + cli.GREY + "Files:" + cli.RESET)
        super(FileCounterStat, self)._report()


class MimeTypeStat(Stat):
    def _compute(self, file, audio_file):
        mt = guessMimetype(file)
        self[mt] += 1

    def _report(self):
        print(cli.BOLD + cli.GREY + "Mime-Types:" + cli.RESET)
        super(MimeTypeStat, self)._report(most_common=True)


class Id3VersionCounter(AudioStat):
    def __init__(self):
        super(Id3VersionCounter, self).__init__()
        for v in ID3_VERSIONS:
            self[v] = 0
            self._key_names[v] = id3.versionToString(v)

    def _compute(self, audio_file):
        if audio_file.tag:
            self[audio_file.tag.version] += 1
        else:
            self[None] += 1

    def _report(self):
        print(cli.BOLD + cli.GREY + "ID3 versions:" + cli.RESET)
        super(Id3VersionCounter, self)._report()


class Id3FrameCounter(AudioStat):
    def _compute(self, audio_file):
        if audio_file.tag:
            for frame_id in audio_file.tag.frame_set:
                self[frame_id] += len(audio_file.tag.frame_set[frame_id])

    def _report(self):
        print(cli.BOLD + cli.GREY + "ID3 frames:" + cli.RESET)
        super(Id3FrameCounter, self)._report()


class BitrateCounter(AudioStat):
    def __init__(self):
        super(BitrateCounter, self).__init__()
        self["cbr"] = 0
        self["vbr"] = 0
        self.bitrate_keys = [(operator.le, 96),
                             (operator.le, 112),
                             (operator.le, 128),
                             (operator.le, 160),
                             (operator.le, 192),
                             (operator.le, 256),
                             (operator.le, 320),
                             (operator.gt, 320),
                            ]
        for k in self.bitrate_keys:
            self[k] = 0
            op, bitrate = k
            self._key_names[k] = "%s %d" % (_OP_STRINGS[op], bitrate)

    def _compute(self, audio_file):
        if audio_file.type != AUDIO_MP3 or audio_file.info is None:
            self["total"] -=1
            return

        vbr, br =  audio_file.info.bit_rate
        if vbr:
            self["vbr"] += 1
        else:
            self["cbr"] += 1

        for key in self.bitrate_keys:
            key_op, key_br = key
            if key_op(br, key_br):
                self[key] += 1
                break

    def _report(self):
        print(cli.BOLD + cli.GREY + "MP3 bitrates:" + cli.RESET)
        super(BitrateCounter, self)._report(most_common=True)

    def _sortedKeys(self, most_common=False):
        keys = super(BitrateCounter, self)._sortedKeys(most_common=most_common)
        keys.remove("cbr")
        keys.remove("vbr")
        keys.insert(0, "cbr")
        keys.insert(1, "vbr")
        return keys


class RuleViolationStat(Stat):
    def _report(self):
        print(cli.BOLD + cli.GREY + "Rule Violations:" + cli.RESET)
        super(RuleViolationStat, self)._report(most_common=True)


class StatisticsPlugin(LoaderPlugin):
    NAMES = ['stats']
    SUMMARY = u"Computes statistics for all audio files scanned."

    def __init__(self, arg_parser):
        super(StatisticsPlugin, self).__init__(arg_parser)

        g = self.arg_group
        self.arg_group.add_argument(
                "--verbose", action="store_true", default=False,
                help="Show details for each file with rule violations.")

        self._stats = []
        self._rules_stat = RuleViolationStat()

        self._stats.append(FileCounterStat())
        self._stats.append(MimeTypeStat())
        self._stats.append(Id3VersionCounter())
        self._stats.append(Id3FrameCounter())
        self._stats.append(BitrateCounter())
        self._stats.append(self._rules_stat)

        self._score_sum = 0
        self._score_count = 0
        self._rules_log = {}
        self._rules = [ Id3TagRules(),
                        MimeTypeRule(),
                        ArtworkRule(),
                        BitrateRule(),
                      ]

    def handleFile(self, path):
        super(StatisticsPlugin, self).handleFile(path)
        sys.stdout.write('.')
        sys.stdout.flush()

        for stat in self._stats:
            if isinstance(stat, AudioStat):
                if self.audio_file:
                    stat.compute(self.audio_file)
            else:
                stat.compute(path, self.audio_file)

        self._score_count += 1
        total_score = 100
        for rule in self._rules:
            scores = rule.test(path, self.audio_file) or []
            if scores:
                if path not in self._rules_log:
                    self._rules_log[path] = []

                for score, text in scores:
                    self._rules_stat[text] += 1
                    self._rules_log[path].append((score, text))
                    # += because negative values are returned
                    total_score += score

        self._score_sum += total_score

    def handleDone(self):
        print()
        for stat in self._stats:
            stat.report()
            print()

        # Detailed rule violations
        if self.args.verbose:
            for path in self._rules_log:
                print(path)
                for score, text in self._rules_log[path]:
                    print("\t%s%d%s (%s)" % (cli.RED, score, cli.RESET, text))

        def prettyScore():
            score = float(self._score_sum) / float(self._score_count)
            if score > 80:
                color = cli.GREEN
            elif score > 70:
                color = cli.YELLOW
            else:
                color = cli.RED
            return (score, color)

        score, color = prettyScore()
        print("%sScore%s = %s%d%%%s" % (cli.BOLD, cli.BOLD_OFF,
                                        color, score, cli.RESET))
        if not self.args.verbose:
            print("Run with --verbose to see files and their rule violations")
        print()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.