aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2012-12-27 19:10:34 +0100
committerLaurent Bachelier <laurent@bachelier.name>2012-12-27 19:10:34 +0100
commit7a11bb3e3f5c0c5e598cddc37d7b474431971642 (patch)
tree97001eb2a7f5473046aa6ae192a4e2f459df41b6
parentAdd --eta option to parallel (diff)
downloadbrutha-7a11bb3e3f5c0c5e598cddc37d7b474431971642.tar.xz
Option to ignore lossy files with too high rate/bit
-rw-r--r--brutha/__main__.py4
-rw-r--r--brutha/file.py29
-rw-r--r--brutha/tree.py6
3 files changed, 35 insertions, 4 deletions
diff --git a/brutha/__main__.py b/brutha/__main__.py
index 14ea60e..f575090 100644
--- a/brutha/__main__.py
+++ b/brutha/__main__.py
@@ -27,6 +27,8 @@ def main():
help='Maximum sample rate allowed (e.g. 44100)', metavar='RATE')
parser.add_argument('-B', '--maxbits', type=int,
help='Maximum bit depth allowed (e.g. 16)', metavar='BITS')
+ parser.add_argument('-L', '--lossycheck', action='store_true',
+ help='Ignore lossy files with too high sample rate or bit depth')
parser.add_argument('-e', '--echo', action='store_true',
help='Show started commands')
parser.add_argument('-x', '--execute', action='store_true',
@@ -39,7 +41,7 @@ def main():
tree = Tree(args.src, args.dest,
{'quality': args.quality, 'gain': args.gain, 'delete': args.delete,
- 'maxrate': args.maxrate, 'maxbits': args.maxbits})
+ 'maxrate': args.maxrate, 'maxbits': args.maxbits, 'lossycheck': args.lossycheck})
if args.execute:
s = StringIO()
p = uprint(s)
diff --git a/brutha/file.py b/brutha/file.py
index 3d4ad79..36e9905 100644
--- a/brutha/file.py
+++ b/brutha/file.py
@@ -9,6 +9,10 @@ import mutagen
from brutha.util import escape
+class NotAllowed(Exception):
+ pass
+
+
def mtime(path):
"""
Get a comparable modification time for a file.
@@ -44,7 +48,7 @@ class File(object):
str(i.tm_mon).zfill(2), str(i.tm_mday).zfill(2),
str(i.tm_hour).zfill(2), str(i.tm_min).zfill(2),
str(i.tm_sec).zfill(2))
- commands.append("touch -t%s -c -m %s" % (stamp, escape(self.dest())))
+ commands.append('touch -t%s -c -m %s' % (stamp, escape(self.dest())))
def src(self):
return os.path.join(self.path, self.name)
@@ -95,4 +99,25 @@ class LossyFile(File):
return commands
def copy(self, commands):
- commands.append("cp %s %s" % (escape(self.src()), escape(self.dest())))
+ if not self.sample_ok():
+ raise NotAllowed("Sample rate or bit depth too high")
+ commands.append('cp %s %s' % (escape(self.src()), escape(self.dest())))
+
+ def sample_ok(self):
+ if not self.options['lossycheck']:
+ return True
+
+ f = mutagen.File(self.src())
+ try:
+ if self.options['maxrate'] and f.info.sample_rate > self.options['maxrate']:
+ return False
+ except AttributeError:
+ pass
+
+ try:
+ if self.options['maxbits'] and f.info.bits_per_sample > self.options['maxbits']:
+ return False
+ except AttributeError:
+ pass
+
+ return True
diff --git a/brutha/tree.py b/brutha/tree.py
index 7ced5e1..66bbc62 100644
--- a/brutha/tree.py
+++ b/brutha/tree.py
@@ -2,6 +2,7 @@
import os
from brutha.directory import Directory, NotInteresting
+from brutha.file import NotAllowed
from brutha.util import escape
@@ -11,7 +12,7 @@ class Tree(object):
self.path = path
self.destpath = destpath
self.options = {'quality': 8, 'gain': False, 'delete': False,
- 'maxrate': None, 'maxbits': None}
+ 'maxrate': None, 'maxbits': None, 'lossycheck': True}
if options:
self.options.update(options)
@@ -32,6 +33,9 @@ class Tree(object):
wanted.extend(d.wanted())
except NotInteresting:
pass
+ except NotAllowed as e:
+ commands.append(['echo %s' % escape('%s: %s' % (root, str(e)))])
+
if self.options['delete']:
c = list(self.delete(wanted))
if c: