Refactor Options and CombinedOptions
Options are now renamed to Option. CombinedOptions are renamed to OptionStore.
This commit is contained in:
parent
d12a628c3b
commit
f25981a46f
|
|
@ -2,18 +2,18 @@
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from .options import CombinedOptions
|
from .options import OptionStore
|
||||||
|
|
||||||
|
|
||||||
NO_AUDIO = ['-an']
|
NO_AUDIO = ['-an']
|
||||||
NO_VIDEO = ['-vn']
|
NO_VIDEO = ['-vn']
|
||||||
|
|
||||||
|
|
||||||
class Codec(CombinedOptions):
|
class Codec(OptionStore):
|
||||||
|
|
||||||
def __init__(self, name, *args):
|
def __init__(self, name, *args):
|
||||||
self.name = name
|
self.name = name
|
||||||
CombinedOptions.__init__(self, *args)
|
OptionStore.__init__(self, *args)
|
||||||
|
|
||||||
|
|
||||||
class VideoCodec(Codec):
|
class VideoCodec(Codec):
|
||||||
|
|
|
||||||
|
|
@ -16,38 +16,39 @@ from select import select
|
||||||
from subprocess import Popen, PIPE, STDOUT
|
from subprocess import Popen, PIPE, STDOUT
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from .options import CombinedOptions, Options
|
from .options import OptionStore, Option
|
||||||
|
|
||||||
|
|
||||||
class Input(CombinedOptions):
|
class Input(OptionStore):
|
||||||
|
|
||||||
def __init__(self, file, *args):
|
def __init__(self, file, *args):
|
||||||
self.file = file
|
self.file = file
|
||||||
CombinedOptions.__init__(self, *args)
|
OptionStore.__init__(self, *args)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return chain(CombinedOptions.__iter__(self), ['-i', self.file])
|
return chain(OptionStore.__iter__(self), ['-i', self.file])
|
||||||
|
|
||||||
|
class Output(OptionStore):
|
||||||
|
|
||||||
class Output(CombinedOptions):
|
|
||||||
|
|
||||||
def __init__(self, file, *args):
|
def __init__(self, file, *args):
|
||||||
self.file = file
|
self.file = file
|
||||||
CombinedOptions.__init__(self, *args)
|
OptionStore.__init__(self, *args)
|
||||||
|
|
||||||
def overwrite(self):
|
def overwrite(self):
|
||||||
self.add_option('-y', None)
|
self.add_option('-y', None)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return chain(CombinedOptions.__iter__(self), [self.file])
|
return chain(OptionStore.__iter__(self), [self.file])
|
||||||
|
|
||||||
|
|
||||||
class FFmpeg(CombinedOptions):
|
class FFmpeg(OptionStore):
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, binary="ffmpeg", *args):
|
def __init__(self, binary="ffmpeg", *args):
|
||||||
self.binary = binary
|
self.binary = binary
|
||||||
CombinedOptions.__init__(self, *args)
|
OptionStore.__init__(self, *args)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.pipe = Popen(self, executable=self.binary,
|
self.pipe = Popen(self, executable=self.binary,
|
||||||
|
|
|
||||||
|
|
@ -2,23 +2,23 @@
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from .options import CombinedOptions
|
from .options import OptionStore
|
||||||
|
|
||||||
|
|
||||||
class CombinedFilter(CombinedOptions):
|
class FilterStore(OptionStore):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ",".join(CombinedFilter.__iter__(self))
|
return ",".join(FilterStore.__iter__(self))
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for key, value in CombinedOptions.iteritems(self):
|
for key, value in OptionStore.iteritems(self):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
yield "=".join([key, str(value)])
|
yield "=".join([key, str(value)])
|
||||||
else:
|
else:
|
||||||
yield key
|
yield key
|
||||||
|
|
||||||
|
|
||||||
class VideoFilter(CombinedFilter):
|
class VideoFilter(FilterStore):
|
||||||
|
|
||||||
def blackframe(self, amount, threshold):
|
def blackframe(self, amount, threshold):
|
||||||
filter = self._format_parameter(amount, threshold)
|
filter = self._format_parameter(amount, threshold)
|
||||||
|
|
@ -171,14 +171,14 @@ class VideoFilter(CombinedFilter):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return chain(['-vf', CombinedFilter.__str__(self)])
|
return chain(['-vf', FilterStore.__str__(self)])
|
||||||
|
|
||||||
|
|
||||||
class AudioFilter(CombinedFilter):
|
class AudioFilter(FilterStore):
|
||||||
|
|
||||||
def null(self):
|
def null(self):
|
||||||
self.add_option('null', None)
|
self.add_option('null', None)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return chain(['-af', CombinedFilter.__str__(self)])
|
return chain(['-af', FilterStore.__str__(self)])
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
class Options(dict):
|
class Option(dict):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
dict.__init__(self, *args, **kwargs)
|
dict.__init__(self, *args, **kwargs)
|
||||||
|
|
@ -25,7 +25,7 @@ class Options(dict):
|
||||||
cls=self.__class__.__name__)
|
cls=self.__class__.__name__)
|
||||||
|
|
||||||
|
|
||||||
class CombinedOptions(object):
|
class OptionStore(object):
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
self._list = list(args)
|
self._list = list(args)
|
||||||
|
|
@ -52,7 +52,7 @@ class CombinedOptions(object):
|
||||||
return self._list.index(item)
|
return self._list.index(item)
|
||||||
|
|
||||||
def add_option(self, key, value):
|
def add_option(self, key, value):
|
||||||
self._list.append(Options({key: value}))
|
self._list.append(Option({key: value}))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def option_containers(self):
|
def option_containers(self):
|
||||||
|
|
|
||||||
6
test.py
6
test.py
|
|
@ -4,7 +4,7 @@ import unittest
|
||||||
|
|
||||||
from ffmpegwrapper import FFmpeg, Input, Output, \
|
from ffmpegwrapper import FFmpeg, Input, Output, \
|
||||||
VideoCodec, AudioCodec, VideoFilter
|
VideoCodec, AudioCodec, VideoFilter
|
||||||
from ffmpegwrapper.options import Options
|
from ffmpegwrapper.options import Option
|
||||||
|
|
||||||
|
|
||||||
class FFmpegTestCase(unittest.TestCase):
|
class FFmpegTestCase(unittest.TestCase):
|
||||||
|
|
@ -14,7 +14,7 @@ class FFmpegTestCase(unittest.TestCase):
|
||||||
self.assertEqual(list(input), ['-i', '/old'])
|
self.assertEqual(list(input), ['-i', '/old'])
|
||||||
self.assertEqual(input.file, '/old')
|
self.assertEqual(input.file, '/old')
|
||||||
|
|
||||||
option = Options({'-vf': 'x11grab'})
|
option = Option({'-vf': 'x11grab'})
|
||||||
input.append(option)
|
input.append(option)
|
||||||
self.assertEqual(list(input), ['-vf', 'x11grab', '-i', '/old'])
|
self.assertEqual(list(input), ['-vf', 'x11grab', '-i', '/old'])
|
||||||
self.assertEqual(input.pop(), option)
|
self.assertEqual(input.pop(), option)
|
||||||
|
|
@ -27,7 +27,7 @@ class FFmpegTestCase(unittest.TestCase):
|
||||||
self.assertEqual(list(output), ['/new'])
|
self.assertEqual(list(output), ['/new'])
|
||||||
self.assertEqual(output.file, '/new')
|
self.assertEqual(output.file, '/new')
|
||||||
|
|
||||||
option = Options({'-vcodec': 'libx264'})
|
option = Option({'-vcodec': 'libx264'})
|
||||||
output.append(option)
|
output.append(option)
|
||||||
self.assertEqual(list(output), ['-vcodec', 'libx264', '/new'])
|
self.assertEqual(list(output), ['-vcodec', 'libx264', '/new'])
|
||||||
self.assertEqual(output.pop(), option)
|
self.assertEqual(output.pop(), option)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue