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

125 lines
3.7 KiB
Python
Raw Normal View History

2015-03-03 14:11:55 -05:00
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)
def __init__(self, parent=None):
QtCore.QObject.__init__(self)
parent.videoTask.connect(self.createVideo)
self.core = core.Core()
@pyqtSlot(str, str, QtGui.QFont, int, int, int, int, str, str)
def createVideo(self, backgroundImage, titleText, titleFont, fontSize, alignment, xOffset, yOffset, 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)
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
2015-03-03 14:11:55 -05:00
self.progressBarUpdate.emit(0)
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
'-an',
'-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
progressBarValue = 0
sampleSize = 1470
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)
else:
im = self.core.drawBars(lastSpectrum, getBackgroundAtIndex(bgI))
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)
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")
self.progressBarUpdate.emit(100)
2016-08-17 17:34:05 -04:00
self.videoCreated.emit()