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/video_thread.py

133 lines
4.1 KiB
Python
Raw Permalink Normal View History

from PySide2 import QtCore, QtGui
from PySide2.QtCore import Signal, Slot
2015-03-03 14:11:55 -05:00
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 = Signal()
progressBarUpdate = Signal(int)
progressBarSetText = Signal(str)
2015-03-03 14:11:55 -05:00
def __init__(self, parent=None):
QtCore.QObject.__init__(self)
parent.videoTask.connect(self.createVideo)
self.core = core.Core()
@Slot(str, str, QtGui.QFont, int, int, int, int, tuple, tuple, str, str)
def createVideo(self, backgroundImage, titleText, titleFont, fontSize, alignment,\
xOffset, yOffset, textColor, visColor, inputFile, outputFile):
2015-03-03 14:11:55 -05:00
# print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
def getBackgroundAtIndex(i):
return self.core.drawBaseImage(
backgroundFrames[i],
titleText,
titleFont,
fontSize,
alignment,
xOffset,
yOffset,
textColor,
visColor)
2017-05-21 22:44:48 -04:00
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
2017-05-21 22:44:48 -04:00
self.progressBarSetText.emit('Loading audio file…')
2015-03-03 14:11:55 -05:00
completeAudioArray = self.core.readAudioFile(inputFile)
2016-09-10 09:09:58 -04:00
# test if user has libfdk_aac
encoders = sp.check_output(self.core.FFMPEG_BIN + " -encoders -hide_banner", shell=True)
if b'libfdk_aac' in encoders:
acodec = 'libfdk_aac'
else:
acodec = 'aac'
ffmpegCommand = [ self.core.FFMPEG_BIN,
2015-03-03 14:11:55 -05:00
'-y', # (optional) means overwrite the output file if it already exists.
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-s', '1280x720', # size of one frame
'-pix_fmt', 'rgb24',
'-r', '30', # frames per second
'-i', '-', # The input comes from a pipe
'-i', inputFile,
2016-09-10 09:09:58 -04:00
'-acodec', acodec, # output audio codec
'-b:a', "192k",
'-vcodec', "libx264",
'-pix_fmt', "yuv420p",
'-preset', "medium",
2016-09-10 09:09:58 -04:00
'-f', "mp4"]
if acodec == 'aac':
ffmpegCommand.append('-strict')
ffmpegCommand.append('-2')
ffmpegCommand.append(outputFile)
2016-09-10 09:09:58 -04:00
out_pipe = sp.Popen(ffmpegCommand,
2015-03-03 14:11:55 -05:00
stdin=sp.PIPE,stdout=sys.stdout, stderr=sys.stdout)
smoothConstantDown = 0.08
smoothConstantUp = 0.8
lastSpectrum = None
sampleSize = 1470
2017-05-21 22:44:48 -04:00
2015-03-03 14:11:55 -05:00
numpy.seterr(divide='ignore')
bgI = 0
2015-03-03 14:11:55 -05:00
for i in range(0, len(completeAudioArray), sampleSize):
# create video for output
lastSpectrum = self.core.transformData(
i,
completeAudioArray,
sampleSize,
smoothConstantDown,
smoothConstantUp,
lastSpectrum)
if imBackground != None:
im = self.core.drawBars(lastSpectrum, imBackground, visColor)
else:
im = self.core.drawBars(lastSpectrum, getBackgroundAtIndex(bgI), visColor)
if bgI < len(backgroundFrames)-1:
bgI += 1
2015-03-03 14:11:55 -05:00
# write to out_pipe
try:
2016-08-17 17:34:05 -04:00
out_pipe.stdin.write(im.tobytes())
2015-03-03 14:11:55 -05:00
finally:
True
# increase progress bar value
if progressBarValue + 1 <= (i / len(completeAudioArray)) * 100:
progressBarValue = numpy.floor((i / len(completeAudioArray)) * 100)
self.progressBarUpdate.emit(progressBarValue)
2017-05-21 22:44:48 -04:00
self.progressBarSetText.emit('%s%%' % str(int(progressBarValue)))
2015-03-03 14:11:55 -05:00
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
2015-03-03 14:11:55 -05:00
out_pipe.wait()
print("Video file created")
2017-05-22 16:58:05 -04:00
self.core.deleteTempDir()
2015-03-03 14:11:55 -05:00
self.progressBarUpdate.emit(100)
2017-05-21 22:44:48 -04:00
self.progressBarSetText.emit('100%')
2016-08-17 17:34:05 -04:00
self.videoCreated.emit()