aboutsummaryrefslogtreecommitdiff
path: root/src/components/video.py
blob: 44f88a5e8f5412c801042459e80ee19ae7aa19eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from PIL import Image, ImageDraw
from PyQt5 import QtGui, QtCore, QtWidgets
import os
import math
import subprocess
import threading
from queue import PriorityQueue
from . import __base__


class Video:
    '''Video Component Frame-Fetcher'''
    def __init__(self, **kwargs):
        mandatoryArgs = [
            'ffmpeg',     # path to ffmpeg, usually core.FFMPEG_BIN
            'videoPath',
            'width',
            'height',
            'scale',      # percentage scale
            'frameRate',  # frames per second
            'chunkSize',  # number of bytes in one frame
            'parent',     # mainwindow object
            'component',  # component object
        ]
        for arg in mandatoryArgs:
            try:
                exec('self.%s = kwargs[arg]' % arg)
            except KeyError:
                raise __base__.BadComponentInit(arg, self.__doc__)

        self.frameNo = -1
        self.currentFrame = 'None'
        if 'loopVideo' in kwargs and kwargs['loopVideo']:
            self.loopValue = '-1'
        else:
            self.loopValue = '0'
        self.command = [
            self.ffmpeg,
            '-thread_queue_size', '512',
            '-r', str(self.frameRate),
            '-stream_loop', self.loopValue,
            '-i', self.videoPath,
            '-f', 'image2pipe',
            '-pix_fmt', 'rgba',
            '-filter:v', 'scale=%s:%s' % scale(
                self.scale, self.width, self.height, str),
            '-vcodec', 'rawvideo', '-',
        ]

        self.frameBuffer = PriorityQueue()
        self.frameBuffer.maxsize = self.frameRate
        self.finishedFrames = {}

        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:
                image = self.finishedFrames.pop(num)
                return finalizeFrame(
                    self.component, image, self.width, self.height)

            i, image = self.frameBuffer.get()
            self.finishedFrames[i] = image
            self.frameBuffer.task_done()

    def fillBuffer(self):
        pipe = subprocess.Popen(
            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.
            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)
                )
                self.parent.stopVideo()
                break

            self.currentFrame = pipe.stdout.read(self.chunkSize)
            if len(self.currentFrame) != 0:
                self.frameBuffer.put((self.frameNo, self.currentFrame))
                self.lastFrame = self.currentFrame


class Component(__base__.Component):
    '''Video'''

    modified = QtCore.pyqtSignal(int, dict)

    def widget(self, parent):
        self.parent = parent
        self.settings = parent.settings
        page = self.loadUi('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)
        page.checkBox_distort.stateChanged.connect(self.update)
        page.spinBox_scale.valueChanged.connect(self.update)
        page.spinBox_x.valueChanged.connect(self.update)
        page.spinBox_y.valueChanged.connect(self.update)

        self.page = page
        return page

    def update(self):
        self.videoPath = self.page.lineEdit_video.text()
        self.loopVideo = self.page.checkBox_loop.isChecked()
        self.distort = self.page.checkBox_distort.isChecked()
        self.scale = self.page.spinBox_scale.value()
        self.xPosition = self.page.spinBox_x.value()
        self.yPosition = self.page.spinBox_y.value()
        self.parent.drawPreview()
        super().update()

    def previewRender(self, previewWorker):
        self.videoFormats = previewWorker.core.videoFormats
        width = int(previewWorker.core.settings.value('outputWidth'))
        height = int(previewWorker.core.settings.value('outputHeight'))
        self.updateChunksize(width, height)
        frame = self.getPreviewFrame(width, height)
        if not frame:
            return self.blankFrame(width, height)
        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.blankFrame_ = self.blankFrame(width, height)
        self.updateChunksize(width, height)
        self.video = Video(
            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,
            component=self, scale=self.scale
        ) if os.path.exists(self.videoPath) else None

    def frameRender(self, moduleNo, arrayNo, frameNo):
        if self.video:
            return self.video.frame(frameNo)
        else:
            return self.blankFrame_

    def loadPreset(self, pr, presetName=None):
        super().loadPreset(pr, presetName)
        self.page.lineEdit_video.setText(pr['video'])
        self.page.checkBox_loop.setChecked(pr['loop'])
        self.page.checkBox_distort.setChecked(pr['distort'])
        self.page.spinBox_scale.setValue(pr['scale'])
        self.page.spinBox_x.setValue(pr['x'])
        self.page.spinBox_y.setValue(pr['y'])

    def savePreset(self):
        return {
            'preset': self.currentPreset,
            'video': self.videoPath,
            'loop': self.loopVideo,
            'distort': self.distort,
            'scale': self.scale,
            'x': self.xPosition,
            'y': self.yPosition,
        }

    def pickVideo(self):
        imgDir = self.settings.value("backgroundDir", os.path.expanduser("~"))
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(
            self.page, "Choose Video",
            imgDir, "Video Files (%s)" % " ".join(self.videoFormats)
        )
        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

        command = [
            self.parent.core.FFMPEG_BIN,
            '-thread_queue_size', '512',
            '-i', self.videoPath,
            '-f', 'image2pipe',
            '-pix_fmt', 'rgba',
            '-filter:v', 'scale=%s:%s' % scale(
                self.scale, width, height, str),
            '-vcodec', 'rawvideo', '-',
            '-ss', '90',
            '-vframes', '1',
        ]
        pipe = subprocess.Popen(
            command, stdout=subprocess.PIPE,
            stderr=subprocess.DEVNULL, bufsize=10**8
        )
        byteFrame = pipe.stdout.read(self.chunkSize)
        frame = finalizeFrame(self, byteFrame, width, height)
        pipe.stdout.close()
        pipe.kill()

        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 not arg.startswith('preset=') and '=' in arg:
            key, arg = arg.split('=', 1)
            if key == 'path' and os.path.exists(arg):
                if 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)
        super().command(arg)

    def commandHelp(self):
        print('Load a video:\n    path=/filepath/to/video.mp4')


def scale(scale, width, height, returntype=None):
    width = (float(width) / 100.0) * float(scale)
    height = (float(height) / 100.0) * float(scale)
    if returntype == str:
        return (str(math.ceil(width)), str(math.ceil(height)))
    elif returntype == int:
        return (math.ceil(width), math.ceil(height))
    else:
        return (width, height)


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'
        )
        return self.blankFrame(width, height)

    if self.scale != 100 \
            or self.xPosition != 0 or self.yPosition != 0:
        frame = self.blankFrame(width, height)
        frame.paste(image, box=(self.xPosition, self.yPosition))
    else:
        frame = image
    return frame