changed video init to use keywords

This commit is contained in:
tassaron 2017-06-06 20:50:53 -04:00
parent 231af74ea2
commit c946133da9
2 changed files with 32 additions and 20 deletions

View File

@ -84,3 +84,13 @@ class Component:
def reset(self): def reset(self):
self.canceled = False self.canceled = False
''' '''
class BadComponentInit(Exception):
def __init__(self, arg, name):
string = \
'''################################
Mandatory argument "%s" not specified
in %s instance initialization
###################################'''
print(string % (arg, name))
quit()

View File

@ -5,37 +5,38 @@ import subprocess
import threading import threading
from queue import PriorityQueue from queue import PriorityQueue
from . import __base__ from . import __base__
class Video: class Video:
'''Video Component Frame-Fetcher''' '''Video Component Frame-Fetcher'''
def __init__( def __init__(self, **kwargs):
self, ffmpeg, videoPath, width, height, mandatoryArgs = ['ffmpeg', 'videoPath', 'width', 'height',
frameRate, chunkSize, parent, loopVideo): 'frameRate', 'chunkSize', 'parent']
for arg in mandatoryArgs:
try:
exec('self.%s = kwargs[arg]' % arg)
except KeyError:
raise __base__.BadComponentInit(arg, self.__doc__)
self.parent = parent
self.chunkSize = chunkSize
self.size = (width, height)
self.frameNo = -1 self.frameNo = -1
self.currentFrame = 'None' self.currentFrame = 'None'
if loopVideo: if 'loopVideo' in kwargs and kwargs['loopVideo']:
self.loopValue = '-1' self.loopValue = '-1'
else: else:
self.loopValue = '0' self.loopValue = '0'
self.command = [ self.command = [
ffmpeg, self.ffmpeg,
'-thread_queue_size', '512', '-thread_queue_size', '512',
'-r', frameRate, '-r', str(self.frameRate),
'-stream_loop', self.loopValue, '-stream_loop', self.loopValue,
'-i', videoPath, '-i', self.videoPath,
'-f', 'image2pipe', '-f', 'image2pipe',
'-pix_fmt', 'rgba', '-pix_fmt', 'rgba',
'-filter:v', 'scale='+str(width)+':'+str(height), '-filter:v', 'scale='+str(self.width)+':'+str(self.height),
'-vcodec', 'rawvideo', '-', '-vcodec', 'rawvideo', '-',
] ]
self.frameBuffer = PriorityQueue() self.frameBuffer = PriorityQueue()
self.frameBuffer.maxsize = int(frameRate) self.frameBuffer.maxsize = self.frameRate
self.finishedFrames = {} self.finishedFrames = {}
self.thread = threading.Thread( self.thread = threading.Thread(
@ -49,13 +50,13 @@ class Video:
while True: while True:
if num in self.finishedFrames: if num in self.finishedFrames:
image = self.finishedFrames.pop(num) image = self.finishedFrames.pop(num)
return Image.frombytes('RGBA', self.size, image) return Image.frombytes('RGBA', (self.width, self.height), image)
i, image = self.frameBuffer.get() i, image = self.frameBuffer.get()
self.finishedFrames[i] = image self.finishedFrames[i] = image
self.frameBuffer.task_done() self.frameBuffer.task_done()
def fillBuffer(self): def fillBuffer(self):
self.pipe = subprocess.Popen( pipe = subprocess.Popen(
self.command, stdout=subprocess.PIPE, self.command, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, bufsize=10**8 stderr=subprocess.DEVNULL, bufsize=10**8
) )
@ -69,7 +70,7 @@ class Video:
self.frameBuffer.put((self.frameNo-1, self.lastFrame)) self.frameBuffer.put((self.frameNo-1, self.lastFrame))
continue continue
self.currentFrame = self.pipe.stdout.read(self.chunkSize) self.currentFrame = pipe.stdout.read(self.chunkSize)
if len(self.currentFrame) != 0: if len(self.currentFrame) != 0:
self.frameBuffer.put((self.frameNo, self.currentFrame)) self.frameBuffer.put((self.frameNo, self.currentFrame))
self.lastFrame = self.currentFrame self.lastFrame = self.currentFrame
@ -117,9 +118,10 @@ class Component(__base__.Component):
height = int(self.worker.core.settings.value('outputHeight')) height = int(self.worker.core.settings.value('outputHeight'))
self.chunkSize = 4*width*height self.chunkSize = 4*width*height
self.video = Video( self.video = Video(
self.parent.core.FFMPEG_BIN, self.videoPath, ffmpeg=self.parent.core.FFMPEG_BIN, videoPath=self.videoPath,
width, height, self.settings.value("outputFrameRate"), width=width, height=height, chunkSize=self.chunkSize,
self.chunkSize, self.parent, self.loopVideo frameRate=int(self.settings.value("outputFrameRate")),
parent=self.parent, loopVideo=self.loopVideo
) )
def frameRender(self, moduleNo, arrayNo, frameNo): def frameRender(self, moduleNo, arrayNo, frameNo):