diff options
| author | DH4 | 2017-06-06 10:14:39 -0500 |
|---|---|---|
| committer | DH4 | 2017-06-06 10:14:39 -0500 |
| commit | 231af74ea2b247bd73fcdfc44657b7fea2ab1620 (patch) | |
| tree | 0ac0d11a5cdd0b70ed4e82b55cae80db76078cc2 /components/video.py | |
| parent | 0948afd6e8b0cf29cf4bdf570e48350caa225c0a (diff) | |
Code cleanup
Diffstat (limited to 'components/video.py')
| -rw-r--r-- | components/video.py | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/components/video.py b/components/video.py index de91264..67a96dd 100644 --- a/components/video.py +++ b/components/video.py @@ -1,12 +1,18 @@ from PIL import Image, ImageDraw from PyQt4 import uic, QtGui, QtCore -import os, subprocess, threading +import os +import subprocess +import threading from queue import PriorityQueue from . import __base__ + class Video: '''Video Component Frame-Fetcher''' - def __init__(self, ffmpeg, videoPath, width, height, frameRate, chunkSize, parent, loopVideo): + def __init__( + self, ffmpeg, videoPath, width, height, + frameRate, chunkSize, parent, loopVideo): + self.parent = parent self.chunkSize = chunkSize self.size = (width, height) @@ -27,15 +33,18 @@ class Video: '-filter:v', 'scale='+str(width)+':'+str(height), '-vcodec', 'rawvideo', '-', ] - + self.frameBuffer = PriorityQueue() self.frameBuffer.maxsize = int(frameRate) self.finishedFrames = {} - - self.thread = threading.Thread(target=self.fillBuffer, name=self.__doc__) + + self.thread = threading.Thread( + target=self.fillBuffer, + name=self.__doc__ + ) self.thread.daemon = True self.thread.start() - + def frame(self, num): while True: if num in self.finishedFrames: @@ -44,9 +53,12 @@ class Video: i, image = self.frameBuffer.get() self.finishedFrames[i] = image self.frameBuffer.task_done() - - def fillBuffer(self): - self.pipe = subprocess.Popen(self.command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=10**8) + + def fillBuffer(self): + self.pipe = subprocess.Popen( + self.command, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, bufsize=10**8 + ) while True: if self.parent.canceled: break @@ -58,26 +70,29 @@ class Video: continue self.currentFrame = self.pipe.stdout.read(self.chunkSize) - #print('creating frame #%s' % str(self.frameNo)) if len(self.currentFrame) != 0: self.frameBuffer.put((self.frameNo, self.currentFrame)) self.lastFrame = self.currentFrame + class Component(__base__.Component): '''Video''' def widget(self, parent): self.parent = parent self.settings = parent.settings - page = uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'video.ui')) + page = uic.loadUi(os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'video.ui' + )) self.videoPath = '' self.x = 0 self.y = 0 self.loopVideo = False - + page.lineEdit_video.textChanged.connect(self.update) page.pushButton_video.clicked.connect(self.pickVideo) page.checkBox_loop.stateChanged.connect(self.update) - + self.page = page return page @@ -85,46 +100,50 @@ class Component(__base__.Component): self.videoPath = self.page.lineEdit_video.text() self.loopVideo = self.page.checkBox_loop.isChecked() self.parent.drawPreview() - + def previewRender(self, previewWorker): width = int(previewWorker.core.settings.value('outputWidth')) height = int(previewWorker.core.settings.value('outputHeight')) self.chunkSize = 4*width*height frame = self.getPreviewFrame(width, height) if not frame: - return Image.new("RGBA", (width, height),(0,0,0,0)) + return Image.new("RGBA", (width, height), (0, 0, 0, 0)) else: return frame - + def preFrameRender(self, **kwargs): super().preFrameRender(**kwargs) width = int(self.worker.core.settings.value('outputWidth')) height = int(self.worker.core.settings.value('outputHeight')) self.chunkSize = 4*width*height - self.video = Video(self.parent.core.FFMPEG_BIN, self.videoPath, + self.video = Video( + self.parent.core.FFMPEG_BIN, self.videoPath, width, height, self.settings.value("outputFrameRate"), - self.chunkSize, self.parent, self.loopVideo) - + self.chunkSize, self.parent, self.loopVideo + ) + def frameRender(self, moduleNo, arrayNo, frameNo): return self.video.frame(frameNo) def loadPreset(self, pr): self.page.lineEdit_video.setText(pr['video']) - + def savePreset(self): return { - 'video' : self.videoPath, + 'video': self.videoPath, } - + def pickVideo(self): imgDir = self.settings.value("backgroundDir", os.path.expanduser("~")) - filename = QtGui.QFileDialog.getOpenFileName(self.page, - "Choose Video", imgDir, "Video Files (*.mp4 *.mov)") + filename = QtGui.QFileDialog.getOpenFileName( + self.page, "Choose Video", + imgDir, "Video Files (*.mp4 *.mov)" + ) if filename: self.settings.setValue("backgroundDir", os.path.dirname(filename)) self.page.lineEdit_video.setText(filename) self.update() - + def getPreviewFrame(self, width, height): if not self.videoPath or not os.path.exists(self.videoPath): return @@ -139,7 +158,10 @@ class Component(__base__.Component): '-ss', '90', '-vframes', '1', ] - pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=10**8) + pipe = subprocess.Popen( + command, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, bufsize=10**8 + ) byteFrame = pipe.stdout.read(self.chunkSize) image = Image.frombytes('RGBA', (width, height), byteFrame) pipe.stdout.close() |
