2017-06-03 20:39:32 -04:00
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
from PyQt4 import uic, QtGui, QtCore
|
2017-06-04 00:19:10 -04:00
|
|
|
import os, subprocess
|
2017-06-03 20:39:32 -04:00
|
|
|
from . import __base__
|
|
|
|
|
|
|
|
class Component(__base__.Component):
|
|
|
|
'''Video'''
|
2017-06-04 13:00:36 -04:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.working = False
|
|
|
|
|
2017-06-03 20:39:32 -04:00
|
|
|
def widget(self, parent):
|
|
|
|
self.parent = parent
|
2017-06-03 22:58:40 -04:00
|
|
|
self.settings = parent.settings
|
2017-06-03 20:39:32 -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
|
|
|
|
|
|
|
|
page.lineEdit_video.textChanged.connect(self.update)
|
|
|
|
page.pushButton_video.clicked.connect(self.pickVideo)
|
|
|
|
|
2017-06-03 20:39:32 -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()
|
2017-06-03 20:39:32 -04:00
|
|
|
self.parent.drawPreview()
|
|
|
|
|
|
|
|
def previewRender(self, previewWorker):
|
2017-06-04 00:19:10 -04:00
|
|
|
self.width = int(previewWorker.core.settings.value('outputWidth'))
|
|
|
|
self.height = int(previewWorker.core.settings.value('outputHeight'))
|
|
|
|
frames = self.getVideoFrames(True)
|
2017-06-04 13:00:36 -04:00
|
|
|
if not hasattr(self, 'staticFrame') or not self.working and frames:
|
|
|
|
frame = Image.new("RGBA", (self.width, self.height), (0, 0, 0, 0))
|
|
|
|
if frames:
|
|
|
|
im = Image.open(frames[0])
|
|
|
|
im = self.resize(im)
|
|
|
|
frame.paste(im)
|
|
|
|
if not self.working:
|
|
|
|
self.staticFrame = frame
|
|
|
|
return self.staticFrame
|
2017-06-04 00:19:10 -04:00
|
|
|
|
|
|
|
def preFrameRender(self, **kwargs):
|
2017-06-04 13:00:36 -04:00
|
|
|
super().preFrameRender(**kwargs)
|
2017-06-04 00:19:10 -04:00
|
|
|
self.width = int(self.worker.core.settings.value('outputWidth'))
|
|
|
|
self.height = int(self.worker.core.settings.value('outputHeight'))
|
|
|
|
self.frames = self.getVideoFrames()
|
2017-06-04 13:00:36 -04:00
|
|
|
self.working = True
|
2017-06-03 20:39:32 -04:00
|
|
|
|
2017-06-04 13:00:36 -04:00
|
|
|
def frameRender(self, moduleNo, arrayNo, frameNo):
|
|
|
|
print(frameNo)
|
|
|
|
try:
|
|
|
|
if frameNo < len(self.frames)-1:
|
|
|
|
self.staticFrame = Image.new("RGBA", (self.width, self.height), (0, 0, 0, 0))
|
|
|
|
im = Image.open(self.frames[frameNo])
|
|
|
|
im = self.resize(im)
|
|
|
|
self.staticFrame.paste(im)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Video component encountered an error")
|
|
|
|
self.frames = []
|
|
|
|
return self.staticFrame
|
2017-06-03 20:39:32 -04:00
|
|
|
|
2017-06-03 22:58:40 -04:00
|
|
|
def loadPreset(self, pr):
|
|
|
|
self.page.lineEdit_video.setText(pr['video'])
|
2017-06-03 20:39:32 -04:00
|
|
|
|
|
|
|
def savePreset(self):
|
2017-06-03 22:58:40 -04:00
|
|
|
return {
|
|
|
|
'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)")
|
|
|
|
if filename:
|
|
|
|
self.settings.setValue("backgroundDir", os.path.dirname(filename))
|
|
|
|
self.page.lineEdit_video.setText(filename)
|
|
|
|
self.update()
|
2017-06-04 00:19:10 -04:00
|
|
|
|
2017-06-04 13:00:36 -04:00
|
|
|
def getVideoFrames(self, preview=False):
|
2017-06-04 00:19:10 -04:00
|
|
|
# recreate the temporary directory so it is empty
|
2017-06-04 13:00:36 -04:00
|
|
|
# FIXME: don't dump all the frames at once, don't dump more than sound length
|
|
|
|
# FIXME: make cancellable, report status to user, etc etc etc
|
2017-06-04 00:19:10 -04:00
|
|
|
if not self.videoPath:
|
|
|
|
return
|
2017-06-04 13:00:36 -04:00
|
|
|
name = os.path.basename(self.videoPath).split('.', 1)[0]
|
|
|
|
if preview:
|
|
|
|
filename = 'preview%s.jpg' % name
|
|
|
|
if os.path.exists(os.path.join(self.parent.core.tempDir, filename)):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
filename = name+'-frame%05d.jpg'
|
|
|
|
|
|
|
|
# recreate tempDir and dump needed frame(s)
|
2017-06-04 00:19:10 -04:00
|
|
|
self.parent.core.deleteTempDir()
|
|
|
|
os.mkdir(self.parent.core.tempDir)
|
2017-06-04 13:00:36 -04:00
|
|
|
if preview:
|
|
|
|
options = '-ss 10 -vframes 1'
|
2017-06-04 00:19:10 -04:00
|
|
|
else:
|
2017-06-04 13:00:36 -04:00
|
|
|
options = '' #'-vframes 99999'
|
2017-06-04 00:19:10 -04:00
|
|
|
subprocess.call( \
|
|
|
|
'%s -i "%s" -y %s "%s"' % ( \
|
|
|
|
self.parent.core.FFMPEG_BIN,
|
|
|
|
self.videoPath,
|
|
|
|
options,
|
|
|
|
os.path.join(self.parent.core.tempDir, filename)
|
|
|
|
),
|
|
|
|
shell=True
|
|
|
|
)
|
2017-06-04 13:00:36 -04:00
|
|
|
print('### Got Preview Frame From %s ###' % name if preview else '### Finished Dumping Frames From %s ###' % name)
|
2017-06-04 00:19:10 -04:00
|
|
|
return sorted([os.path.join(self.parent.core.tempDir, f) for f in os.listdir(self.parent.core.tempDir)])
|
|
|
|
|
|
|
|
def resize(self, im):
|
|
|
|
if im.size != (self.width, self.height):
|
|
|
|
im = im.resize((self.width, self.height), Image.ANTIALIAS)
|
|
|
|
return im
|