This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
pyaudviz/components/video.py

175 lines
5.8 KiB
Python
Raw Normal View History

from PIL import Image, ImageDraw
from PyQt4 import uic, QtGui, QtCore
2017-06-06 11:14:39 -04:00
import os
import subprocess
import threading
from queue import PriorityQueue
from . import __base__
class Video:
'''Video Component Frame-Fetcher'''
2017-06-06 20:50:53 -04:00
def __init__(self, **kwargs):
mandatoryArgs = ['ffmpeg', 'videoPath', 'width', 'height',
'frameRate', 'chunkSize', 'parent']
for arg in mandatoryArgs:
try:
exec('self.%s = kwargs[arg]' % arg)
except KeyError:
raise __base__.BadComponentInit(arg, self.__doc__)
2017-06-06 11:14:39 -04:00
self.frameNo = -1
self.currentFrame = 'None'
2017-06-06 20:50:53 -04:00
if 'loopVideo' in kwargs and kwargs['loopVideo']:
self.loopValue = '-1'
else:
self.loopValue = '0'
self.command = [
2017-06-06 20:50:53 -04:00
self.ffmpeg,
'-thread_queue_size', '512',
2017-06-06 20:50:53 -04:00
'-r', str(self.frameRate),
'-stream_loop', self.loopValue,
2017-06-06 20:50:53 -04:00
'-i', self.videoPath,
'-f', 'image2pipe',
'-pix_fmt', 'rgba',
2017-06-06 20:50:53 -04:00
'-filter:v', 'scale='+str(self.width)+':'+str(self.height),
'-vcodec', 'rawvideo', '-',
]
2017-06-06 11:14:39 -04:00
self.frameBuffer = PriorityQueue()
2017-06-06 20:50:53 -04:00
self.frameBuffer.maxsize = self.frameRate
self.finishedFrames = {}
2017-06-06 11:14:39 -04:00
self.thread = threading.Thread(
target=self.fillBuffer,
name=self.__doc__
)
self.thread.daemon = True
self.thread.start()
2017-06-06 11:14:39 -04:00
def frame(self, num):
while True:
if num in self.finishedFrames:
image = self.finishedFrames.pop(num)
2017-06-06 20:50:53 -04:00
return Image.frombytes('RGBA', (self.width, self.height), image)
i, image = self.frameBuffer.get()
self.finishedFrames[i] = image
self.frameBuffer.task_done()
2017-06-06 11:14:39 -04:00
def fillBuffer(self):
2017-06-06 20:50:53 -04:00
pipe = subprocess.Popen(
2017-06-06 11:14:39 -04:00
self.command, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, bufsize=10**8
)
while True:
if self.parent.canceled:
break
self.frameNo += 1
# If we run out of frames, use the last good frame and loop.
if len(self.currentFrame) == 0:
self.frameBuffer.put((self.frameNo-1, self.lastFrame))
continue
2017-06-06 20:50:53 -04:00
self.currentFrame = pipe.stdout.read(self.chunkSize)
if len(self.currentFrame) != 0:
self.frameBuffer.put((self.frameNo, self.currentFrame))
self.lastFrame = self.currentFrame
2017-06-06 11:14:39 -04:00
class Component(__base__.Component):
'''Video'''
def widget(self, parent):
self.parent = parent
2017-06-03 22:58:40 -04:00
self.settings = parent.settings
2017-06-06 11:14:39 -04:00
page = uic.loadUi(os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'video.ui'
))
2017-06-03 22:58:40 -04:00
self.videoPath = ''
self.x = 0
self.y = 0
self.loopVideo = False
2017-06-06 11:14:39 -04:00
2017-06-03 22:58:40 -04:00
page.lineEdit_video.textChanged.connect(self.update)
page.pushButton_video.clicked.connect(self.pickVideo)
page.checkBox_loop.stateChanged.connect(self.update)
2017-06-06 11:14:39 -04:00
self.page = page
return page
def update(self):
2017-06-03 22:58:40 -04:00
self.videoPath = self.page.lineEdit_video.text()
self.loopVideo = self.page.checkBox_loop.isChecked()
self.parent.drawPreview()
2017-06-06 11:14:39 -04:00
def previewRender(self, previewWorker):
width = int(previewWorker.core.settings.value('outputWidth'))
height = int(previewWorker.core.settings.value('outputHeight'))
self.chunkSize = 4*width*height
2017-06-06 08:55:22 -04:00
frame = self.getPreviewFrame(width, height)
if not frame:
2017-06-06 11:14:39 -04:00
return Image.new("RGBA", (width, height), (0, 0, 0, 0))
2017-06-06 08:55:22 -04:00
else:
return frame
2017-06-06 11:14:39 -04:00
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
2017-06-06 11:14:39 -04:00
self.video = Video(
2017-06-06 20:50:53 -04:00
ffmpeg=self.parent.core.FFMPEG_BIN, videoPath=self.videoPath,
width=width, height=height, chunkSize=self.chunkSize,
frameRate=int(self.settings.value("outputFrameRate")),
parent=self.parent, loopVideo=self.loopVideo
2017-06-06 11:14:39 -04:00
)
def frameRender(self, moduleNo, arrayNo, frameNo):
return self.video.frame(frameNo)
2017-06-03 22:58:40 -04:00
def loadPreset(self, pr):
self.page.lineEdit_video.setText(pr['video'])
self.page.checkBox_loop.setChecked(pr['loop'])
2017-06-06 11:14:39 -04:00
def savePreset(self):
2017-06-03 22:58:40 -04:00
return {
2017-06-06 11:14:39 -04:00
'video': self.videoPath,
'loop': self.loopVideo,
2017-06-03 22:58:40 -04:00
}
2017-06-06 11:14:39 -04:00
2017-06-03 22:58:40 -04:00
def pickVideo(self):
imgDir = self.settings.value("backgroundDir", os.path.expanduser("~"))
2017-06-06 11:14:39 -04:00
filename = QtGui.QFileDialog.getOpenFileName(
self.page, "Choose Video",
imgDir, "Video Files (*.mp4 *.mov)"
)
2017-06-03 22:58:40 -04:00
if filename:
self.settings.setValue("backgroundDir", os.path.dirname(filename))
self.page.lineEdit_video.setText(filename)
self.update()
2017-06-06 11:14:39 -04:00
def getPreviewFrame(self, width, height):
2017-06-06 08:55:22 -04:00
if not self.videoPath or not os.path.exists(self.videoPath):
return
command = [
self.parent.core.FFMPEG_BIN,
'-thread_queue_size', '512',
'-i', self.videoPath,
'-f', 'image2pipe',
'-pix_fmt', 'rgba',
'-filter:v', 'scale='+str(width)+':'+str(height),
'-vcodec', 'rawvideo', '-',
'-ss', '90',
'-vframes', '1',
]
2017-06-06 11:14:39 -04:00
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()
pipe.kill()
return image