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/src/components/video.py

318 lines
10 KiB
Python
Raw Normal View History

from PIL import Image, ImageDraw
2017-06-24 23:12:41 -04:00
from PyQt5 import QtGui, QtCore, QtWidgets
2017-06-06 11:14:39 -04:00
import os
2017-06-24 23:12:41 -04:00
import math
2017-06-06 11:14:39 -04:00
import subprocess
import threading
from queue import PriorityQueue
2017-07-02 20:46:48 -04:00
from component import Component
from toolkit.frame import BlankFrame
from toolkit.ffmpeg import testAudioStream
from toolkit import openPipe, checkOutput
class Video:
'''Opens a pipe to ffmpeg and stores a buffer of raw video frames.'''
2017-06-06 20:50:53 -04:00
def __init__(self, **kwargs):
mandatoryArgs = [
'ffmpeg', # path to ffmpeg, usually self.core.FFMPEG_BIN
'videoPath',
'width',
'height',
2017-06-15 15:09:45 -04:00
'scale', # percentage scale
'frameRate', # frames per second
'chunkSize', # number of bytes in one frame
2017-06-15 15:09:45 -04:00
'parent', # mainwindow object
'component', # component object
]
2017-06-06 20:50:53 -04:00
for arg in mandatoryArgs:
setattr(self, arg, kwargs[arg])
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',
'-filter_complex', '[0:v] scale=%s:%s' % scale(
2017-06-23 23:00:24 -04:00
self.scale, self.width, self.height, str),
'-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-15 15:09:45 -04:00
return finalizeFrame(
self.component, image, self.width, self.height)
i, image = self.frameBuffer.get()
self.finishedFrames[i] = image
self.frameBuffer.task_done()
2017-06-06 11:14:39 -04:00
def fillBuffer(self):
pipe = openPipe(
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.
2017-06-24 23:12:41 -04:00
try:
if len(self.currentFrame) == 0:
self.frameBuffer.put((self.frameNo-1, self.lastFrame))
continue
except AttributeError as e:
self.parent.showMessage(
msg='%s couldn\'t be loaded. '
'This is a fatal error.' % os.path.basename(
self.videoPath
),
detail=str(e),
icon='Warning'
2017-06-24 23:12:41 -04:00
)
self.parent.stopVideo()
break
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
2017-07-02 20:46:48 -04:00
class Component(Component):
name = 'Video'
version = '1.0.0'
def widget(self, *args):
2017-06-03 22:58:40 -04:00
self.videoPath = ''
self.badVideo = False
self.badAudio = False
2017-06-03 22:58:40 -04:00
self.x = 0
self.y = 0
self.loopVideo = False
super().widget(*args)
self.page.pushButton_video.clicked.connect(self.pickVideo)
self.trackWidgets(
{
'videoPath': self.page.lineEdit_video,
'loopVideo': self.page.checkBox_loop,
'useAudio': self.page.checkBox_useAudio,
'distort': self.page.checkBox_distort,
'scale': self.page.spinBox_scale,
'volume': self.page.spinBox_volume,
'xPosition': self.page.spinBox_x,
'yPosition': self.page.spinBox_y,
}, presetNames={
'videoPath': 'video',
'loopVideo': 'loop',
'xPosition': 'x',
'yPosition': 'y',
}
)
def update(self):
if self.page.checkBox_useAudio.isChecked():
self.page.label_volume.setEnabled(True)
self.page.spinBox_volume.setEnabled(True)
else:
self.page.label_volume.setEnabled(False)
self.page.spinBox_volume.setEnabled(False)
super().update()
2017-06-06 11:14:39 -04:00
def previewRender(self, previewWorker):
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
2017-06-15 15:09:45 -04:00
self.updateChunksize(width, height)
2017-06-06 08:55:22 -04:00
frame = self.getPreviewFrame(width, height)
if not frame:
return BlankFrame(width, height)
2017-06-06 08:55:22 -04:00
else:
return frame
2017-06-06 11:14:39 -04:00
2017-07-11 06:06:22 -04:00
def properties(self):
# TODO: Disallow selecting the same video you're exporting to
2017-07-11 06:06:22 -04:00
props = []
if not self.videoPath or self.badVideo \
or not os.path.exists(self.videoPath):
return ['error']
if self.useAudio:
props.append('audio')
self.testAudioStream()
if self.badAudio:
return ['error']
2017-07-11 06:06:22 -04:00
return props
def error(self):
if self.badAudio:
return "Could not identify an audio stream in this video."
if not self.videoPath:
return "There is no video selected."
2017-07-11 06:06:22 -04:00
if not os.path.exists(self.videoPath):
return "The video selected does not exist!"
if self.badVideo:
return "The video selected is corrupt!"
2017-07-11 06:06:22 -04:00
def testAudioStream(self):
self.badAudio = testAudioStream(self.videoPath)
2017-07-11 06:06:22 -04:00
def audio(self):
params = {}
if self.volume != 1.0:
params['volume'] = '=%s:replaygain_noclip=0' % str(self.volume)
return (self.videoPath, params)
2017-07-11 06:06:22 -04:00
def preFrameRender(self, **kwargs):
super().preFrameRender(**kwargs)
width = int(self.settings.value('outputWidth'))
height = int(self.settings.value('outputHeight'))
self.blankFrame_ = BlankFrame(width, height)
2017-06-15 15:09:45 -04:00
self.updateChunksize(width, height)
self.video = Video(
ffmpeg=self.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,
component=self, scale=self.scale
) if os.path.exists(self.videoPath) else None
2017-06-06 11:14:39 -04:00
def frameRender(self, layerNo, frameNo):
if self.video:
return self.video.frame(frameNo)
else:
return self.blankFrame_
2017-06-03 22:58:40 -04:00
def pickVideo(self):
imgDir = self.settings.value("componentDir", os.path.expanduser("~"))
2017-06-23 23:00:24 -04:00
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
2017-06-06 11:14:39 -04:00
self.page, "Choose Video",
imgDir, "Video Files (%s)" % " ".join(self.core.videoFormats)
2017-06-06 11:14:39 -04:00
)
if filename:
self.settings.setValue("componentDir", os.path.dirname(filename))
2017-06-03 22:58:40 -04:00
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
2017-06-15 15:09:45 -04:00
command = [
self.core.FFMPEG_BIN,
'-thread_queue_size', '512',
'-i', self.videoPath,
'-f', 'image2pipe',
'-pix_fmt', 'rgba',
'-filter_complex', '[0:v] scale=%s:%s' % scale(
2017-06-23 23:00:24 -04:00
self.scale, width, height, str),
'-vcodec', 'rawvideo', '-',
'-ss', '90',
'-vframes', '1',
]
pipe = openPipe(
2017-06-06 11:14:39 -04:00
command, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, bufsize=10**8
)
byteFrame = pipe.stdout.read(self.chunkSize)
2017-06-15 15:09:45 -04:00
frame = finalizeFrame(self, byteFrame, width, height)
pipe.stdout.close()
pipe.kill()
2017-06-15 15:09:45 -04:00
return frame
def updateChunksize(self, width, height):
if self.scale != 100 and not self.distort:
width, height = scale(self.scale, width, height, int)
self.chunkSize = 4*width*height
def command(self, arg):
if '=' in arg:
key, arg = arg.split('=', 1)
if key == 'path' and os.path.exists(arg):
if '*%s' % os.path.splitext(arg)[1] in self.core.videoFormats:
self.page.lineEdit_video.setText(arg)
self.page.spinBox_scale.setValue(100)
self.page.checkBox_loop.setChecked(True)
return
else:
print("Not a supported video format")
quit(1)
elif arg == 'audio':
if not self.page.lineEdit_video.text():
print("'audio' option must follow a video selection")
quit(1)
self.page.checkBox_useAudio.setChecked(True)
return
super().command(arg)
def commandHelp(self):
print('Load a video:\n path=/filepath/to/video.mp4')
print('Using audio:\n path=/filepath/to/video.mp4 audio')
2017-06-23 23:00:24 -04:00
2017-06-15 15:09:45 -04:00
def scale(scale, width, height, returntype=None):
width = (float(width) / 100.0) * float(scale)
height = (float(height) / 100.0) * float(scale)
if returntype == str:
2017-06-24 23:12:41 -04:00
return (str(math.ceil(width)), str(math.ceil(height)))
2017-06-15 15:09:45 -04:00
elif returntype == int:
2017-06-24 23:12:41 -04:00
return (math.ceil(width), math.ceil(height))
2017-06-15 15:09:45 -04:00
else:
return (width, height)
2017-06-23 23:00:24 -04:00
2017-06-15 15:09:45 -04:00
def finalizeFrame(self, imageData, width, height):
try:
if self.distort:
image = Image.frombytes(
'RGBA',
(width, height),
imageData)
else:
image = Image.frombytes(
'RGBA',
scale(self.scale, width, height, int),
imageData)
except ValueError:
print(
'### BAD VIDEO SELECTED ###\n'
'Video will not export with these settings'
)
self.badVideo = True
return BlankFrame(width, height)
2017-06-15 15:09:45 -04:00
if self.scale != 100 \
2017-06-23 23:00:24 -04:00
or self.xPosition != 0 or self.yPosition != 0:
frame = BlankFrame(width, height)
2017-06-15 15:09:45 -04:00
frame.paste(image, box=(self.xPosition, self.yPosition))
else:
frame = image
self.badVideo = False
2017-06-15 15:09:45 -04:00
return frame