aboutsummaryrefslogtreecommitdiff
path: root/video_thread.py
blob: 93ca7bd89824643c1ac797b348728a1af4a73fca (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
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PIL import Image, ImageDraw, ImageFont
from PIL.ImageQt import ImageQt
import core
import numpy
import subprocess as sp
import sys

class Worker(QtCore.QObject):

  videoCreated = pyqtSignal()
  progressBarUpdate = pyqtSignal(int)
  progressBarSetText = pyqtSignal(str)

  def __init__(self, parent=None):
    QtCore.QObject.__init__(self)
    self.core = core.Core()
    self.core.settings = parent.settings
    self.modules = parent.modules
    self.stackedWidget = parent.window.stackedWidget
    parent.videoTask.connect(self.createVideo)

  @pyqtSlot(str, str, str, list)
  def createVideo(self, backgroundImage, inputFile, outputFile, components):
    # print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
    def getBackgroundAtIndex(i):
        return self.core.drawBaseImage(backgroundFrames[i])

    progressBarValue = 0
    self.progressBarUpdate.emit(progressBarValue)
    self.progressBarSetText.emit('Loading background image…')

    backgroundFrames = self.core.parseBaseImage(backgroundImage)
    if len(backgroundFrames) < 2:
        # the base image is not a video so we can draw it now
        imBackground = getBackgroundAtIndex(0)
    else:
        # base images will be drawn while drawing the audio bars
        imBackground = None
        
    self.progressBarSetText.emit('Loading audio file…')
    completeAudioArray = self.core.readAudioFile(inputFile)

    # test if user has libfdk_aac
    encoders = sp.check_output(self.core.FFMPEG_BIN + " -encoders -hide_banner", shell=True)
    acodec = self.core.settings.value('outputAudioCodec')

    if b'libfdk_aac' in encoders and acodec == 'aac':
      acodec = 'libfdk_aac'

    ffmpegCommand = [ self.core.FFMPEG_BIN,
       '-y', # (optional) means overwrite the output file if it already exists.
       '-f', 'rawvideo',
       '-vcodec', 'rawvideo',
       '-s', self.core.settings.value('outputWidth')+'x'+self.core.settings.value('outputHeight'), # size of one frame
       '-pix_fmt', 'rgb24',
       '-r', self.core.settings.value('outputFrameRate'), # frames per second
       '-i', '-', # The input comes from a pipe
       '-an',
       '-i', inputFile,
       '-acodec', acodec, # output audio codec
       '-b:a', self.core.settings.value('outputAudioBitrate'),
       '-vcodec', self.core.settings.value('outputVideoCodec'),
       '-pix_fmt', self.core.settings.value('outputVideoFormat'),
       '-preset', self.core.settings.value('outputPreset'),
       '-f', self.core.settings.value('outputFormat')]

    if acodec == 'aac':
      ffmpegCommand.append('-strict')
      ffmpegCommand.append('-2')

    ffmpegCommand.append(outputFile)
    
    out_pipe = sp.Popen(ffmpegCommand,
        stdin=sp.PIPE,stdout=sys.stdout, stderr=sys.stdout)

    # initialize components
    print('######################## Data')
    print('loaded components:',
        ["%s%s" % (num, str(component)) for num, component in enumerate(components)])
    staticComponents = {}
    sampleSize = 1470
    for compNo, comp in enumerate(components):
        properties = None
        properties = comp.preFrameRender(worker=self, completeAudioArray=completeAudioArray, sampleSize=sampleSize)
        if properties and 'static' in properties:
            staticComponents[compNo] = None

    # create video for output
    numpy.seterr(divide='ignore')
    frame = getBackgroundAtIndex(0)
    bgI = 0
    for i in range(0, len(completeAudioArray), sampleSize):
        newFrame = Image.new("RGBA", (int(self.core.settings.value('outputWidth')), int(self.core.settings.value('outputHeight'))),(0,0,0,255))
        if imBackground:
            newFrame.paste(imBackground)
        else:
            newFrame.paste(getBackgroundAtIndex(bgI))

        # composite all frames returned by the components in order
        for compNo, comp in enumerate(components):
            if compNo in staticComponents and staticComponents[compNo] != None:
                newFrame = Image.alpha_composite(newFrame,staticComponents[compNo])
            else:
                newFrame = Image.alpha_composite(newFrame,comp.frameRender(compNo, i))
            if i == 0 and compNo in staticComponents:
                staticComponents[compNo] = comp.frameRender(compNo, i)

        if not imBackground:
            # increment background video frame for next iteration
            if bgI < len(backgroundFrames)-1:
                bgI += 1
                
      # write to out_pipe
        try:
            frame = Image.new("RGB", (int(self.core.settings.value('outputWidth')), int(self.core.settings.value('outputHeight'))),(0,0,0))
            frame.paste(newFrame)
            out_pipe.stdin.write(frame.tobytes())
        finally:
            True

        # increase progress bar value
        if progressBarValue + 1 <= (i / len(completeAudioArray)) * 100:
            progressBarValue = numpy.floor((i / len(completeAudioArray)) * 100)
            self.progressBarUpdate.emit(progressBarValue)
            self.progressBarSetText.emit('%s%%' % str(int(progressBarValue)))

    numpy.seterr(all='print')

    out_pipe.stdin.close()
    if out_pipe.stderr is not None:
      print(out_pipe.stderr.read())
      out_pipe.stderr.close()
    # out_pipe.terminate() # don't terminate ffmpeg too early
    out_pipe.wait()
    print("Video file created")
    self.core.deleteTempDir()
    self.progressBarUpdate.emit(100)
    self.progressBarSetText.emit('100%')
    self.videoCreated.emit()