From f7e6758f3ab1d531c2fa74b68761af09d3b35094 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 27 Jan 2013 20:24:26 -0600 Subject: [PATCH] timeside/encoder/: factorise init function, raise IOError when trying to encode to a directory, add tests accordingly --- tests/testencoding.py | 58 ++++++++++++++++++++++++++++++++++++++-- timeside/encoder/core.py | 16 +++++++++++ timeside/encoder/flac.py | 12 --------- timeside/encoder/m4a.py | 8 +++--- timeside/encoder/mp3.py | 13 --------- timeside/encoder/ogg.py | 12 --------- timeside/encoder/wav.py | 12 --------- timeside/encoder/webm.py | 13 ++------- 8 files changed, 77 insertions(+), 67 deletions(-) diff --git a/tests/testencoding.py b/tests/testencoding.py index e63138e..56ab7fa 100644 --- a/tests/testencoding.py +++ b/tests/testencoding.py @@ -5,7 +5,7 @@ from unit_timeside import * import os.path class TestEncoding(TestCase): - "Test the low level streaming features" + "Test encoding features" def setUp(self): self.samplerate, self.channels, self.blocksize = 44100, 1, 1024 @@ -74,37 +74,91 @@ class TestEncoding(TestCase): print commands.getoutput('sndfile-info ' + self.sink) self.assertEquals(written_frames, total_frames) - self.tmpfile.close() + if hasattr(self, 'tmpfile'): self.tmpfile.close() class TestEncodingLongBlock(TestEncoding): + "Test encoding features with longer blocksize" def setUp(self): super(TestEncodingLongBlock, self).setUp() self.blocksize *= 8 class TestEncodingShortBlock(TestEncoding): + "Test encoding features with short blocksize" def setUp(self): super(TestEncodingShortBlock, self).setUp() self.blocksize = 64 class TestEncodingLowSamplerate(TestEncoding): + "Test encoding features with low samplerate" def setUp(self): super(TestEncodingLowSamplerate, self).setUp() self.samplerate = 8000 class TestEncodingHighSamplerate(TestEncoding): + "Test encoding features with high samplerate" def setUp(self): super(TestEncodingHighSamplerate, self).setUp() self.samplerate = 192000 class TestEncodingStereo(TestEncoding): + "Test encoding features with stereo" def setUp(self): super(TestEncodingStereo, self).setUp() self.channels = 2 +class TestEncodingToDevNull(TestEncoding): + "Test encoding features with /dev/null" + + def setUp(self): + self.samplerate, self.channels, self.blocksize = 44100, 1, 1024 + self.sink = '/dev/null' + +class TestEncodingToDirectory(TestEncoding): + "Test encoding features to a directory" + + def setUp(self): + self.samplerate, self.channels, self.blocksize = 44100, 1, 1024 + import tempfile + self.sink = tempfile.mkdtemp() + + def testWav(self): + "Test wav encoding" + from timeside.encoder.wav import WavEncoder + self.assertRaises(IOError,WavEncoder,self.sink) + + def testVorbis(self): + "Test vorbis encoding" + from timeside.encoder.ogg import VorbisEncoder + self.assertRaises(IOError,VorbisEncoder,self.sink) + + def testMp3(self): + "Test mp3 encoding" + from timeside.encoder.mp3 import Mp3Encoder + self.assertRaises(IOError,Mp3Encoder,self.sink) + + def testAac(self): + "Test aac encoding" + from timeside.encoder.m4a import AacEncoder + self.assertRaises(IOError,AacEncoder,self.sink) + + def testFlac(self): + "Test flac encoding" + from timeside.encoder.flac import FlacEncoder + self.assertRaises(IOError,FlacEncoder,self.sink) + + def testWebm(self): + "Test webm encoding" + from timeside.encoder.webm import WebMEncoder + self.assertRaises(IOError,WebMEncoder,self.sink) + + def tearDown(self): + from os import rmdir + rmdir(self.sink) + if __name__ == '__main__': unittest.main(testRunner=TestRunner()) diff --git a/timeside/encoder/core.py b/timeside/encoder/core.py index d656084..7718143 100644 --- a/timeside/encoder/core.py +++ b/timeside/encoder/core.py @@ -26,6 +26,22 @@ from timeside.tools import * class GstEncoder(Processor): + def __init__(self, output, streaming = False): + if isinstance(output, basestring): + import os.path + if os.path.isdir(output): + raise IOError("Encoder output must be a file, not a directory") + self.filename = output + else: + self.filename = None + self.streaming = streaming + + if not self.filename and not self.streaming: + raise Exception('Must give an output') + + self.eod = False + self.metadata = None + def release(self): pass diff --git a/timeside/encoder/flac.py b/timeside/encoder/flac.py index e9c488f..e35dfaa 100644 --- a/timeside/encoder/flac.py +++ b/timeside/encoder/flac.py @@ -29,18 +29,6 @@ class FlacEncoder(GstEncoder): """ gstreamer-based FLAC encoder """ implements(IEncoder) - def __init__(self, output, streaming=False): - if isinstance(output, basestring): - self.filename = output - else: - self.filename = None - self.streaming = streaming - - if not self.filename and not self.streaming: - raise Exception('Must give an output') - - self.eod = False - @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(FlacEncoder, self).setup(channels, samplerate, blocksize, totalframes) diff --git a/timeside/encoder/m4a.py b/timeside/encoder/m4a.py index 3114567..b0c65ec 100644 --- a/timeside/encoder/m4a.py +++ b/timeside/encoder/m4a.py @@ -29,11 +29,9 @@ class AacEncoder(GstEncoder): """ gstreamer-based AAC encoder """ implements(IEncoder) - def __init__(self, output): - self.file = None - if isinstance(output, basestring): - self.filename = output - else: + def __init__(self, output, streaming = False): + super(AacEncoder, self).__init__(output, streaming) + if self.streaming: raise Exception("Streaming not supported") @interfacedoc diff --git a/timeside/encoder/mp3.py b/timeside/encoder/mp3.py index f2ce3d9..3056d47 100644 --- a/timeside/encoder/mp3.py +++ b/timeside/encoder/mp3.py @@ -35,19 +35,6 @@ class Mp3Encoder(GstEncoder): """ gstreamer-based mp3 encoder """ implements(IEncoder) - def __init__(self, output, streaming=False): - if isinstance(output, basestring): - self.filename = output - else: - self.filename = None - - self.streaming = streaming - if not self.filename and not self.streaming: - raise Exception('Must give an output') - - self.metadata = None - self.eod = False - @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(Mp3Encoder, self).setup(channels, samplerate, blocksize, totalframes) diff --git a/timeside/encoder/ogg.py b/timeside/encoder/ogg.py index f222c4e..b834e15 100644 --- a/timeside/encoder/ogg.py +++ b/timeside/encoder/ogg.py @@ -28,18 +28,6 @@ class VorbisEncoder(GstEncoder): """ gstreamer-based vorbis encoder """ implements(IEncoder) - def __init__(self, output, streaming=False): - if isinstance(output, basestring): - self.filename = output - else: - self.filename = None - self.streaming = streaming - - if not self.filename and not self.streaming: - raise Exception('Must give an output') - - self.eod = False - @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(VorbisEncoder, self).setup(channels, samplerate, blocksize, totalframes) diff --git a/timeside/encoder/wav.py b/timeside/encoder/wav.py index e898d8a..32666e7 100644 --- a/timeside/encoder/wav.py +++ b/timeside/encoder/wav.py @@ -30,18 +30,6 @@ class WavEncoder(GstEncoder): """ gstreamer-based encoder """ implements(IEncoder) - def __init__(self, output, streaming=False): - if isinstance(output, basestring): - self.filename = output - else: - self.filename = None - self.streaming = streaming - - if not self.filename and not self.streaming: - raise Exception('Must give an output') - - self.eod = False - @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): super(WavEncoder, self).setup(channels, samplerate, blocksize, totalframes) diff --git a/timeside/encoder/webm.py b/timeside/encoder/webm.py index d228e9b..3b1dd60 100644 --- a/timeside/encoder/webm.py +++ b/timeside/encoder/webm.py @@ -29,18 +29,9 @@ class WebMEncoder(GstEncoder): """ gstreamer-based webm encoder and muxer """ implements(IEncoder) - def __init__(self, output, streaming=False, video=False): - if isinstance(output, basestring): - self.filename = output - else: - self.filename = None - self.streaming = streaming - - if not self.filename and not self.streaming: - raise Exception('Must give an output') - + def __init__(self, output, streaming = False): + super(WebMEncoder, self).__init__(output, streaming) self.video = False - self.eod = False @interfacedoc def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None): -- 2.39.5