From 39e66ffa2d07b87b57ed90b369ab26aedf0a69e8 Mon Sep 17 00:00:00 2001 From: tassaron Date: Sun, 4 Jun 2017 13:00:36 -0400 Subject: video component almost working, rm hardcoded backgrounds --- video_thread.py | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) (limited to 'video_thread.py') diff --git a/video_thread.py b/video_thread.py index c97cc24..e74fffa 100644 --- a/video_thread.py +++ b/video_thread.py @@ -38,16 +38,17 @@ class Worker(QtCore.QObject): while not self.stopped: i = self.compositeQueue.get() - if self.imBackground is not None: - frame = self.imBackground - else: - frame = self.getBackgroundAtIndex(i[1]) + frame = Image.new( + "RGBA", + (self.width, self.height), + (0, 0, 0, 0) + ) for compNo, comp in reversed(list(enumerate(self.components))): if compNo in self.staticComponents and self.staticComponents[compNo] != None: frame = Image.alpha_composite(frame, self.staticComponents[compNo]) else: - frame = Image.alpha_composite(frame, comp.frameRender(compNo, i[0])) + frame = Image.alpha_composite(frame, comp.frameRender(compNo, i[0], i[1])) # frame.paste(compFrame, mask=compFrame) @@ -59,10 +60,8 @@ class Worker(QtCore.QObject): for i in range(0, len(self.completeAudioArray), self.sampleSize): self.compositeQueue.put([i, self.bgI]) - if not self.imBackground: - # increment background video frame for next iteration - if self.bgI < len(self.backgroundFrames)-1: - self.bgI += 1 + # increment tracked video frame for next iteration + self.bgI += 1 def previewDispatch(self): while not self.stopped: @@ -74,39 +73,18 @@ class Worker(QtCore.QObject): self.previewQueue.task_done() - def getBackgroundAtIndex(self, i): - background = Image.new( - "RGBA", - (self.width, self.height), - (0, 0, 0, 255) - ) - layer = self.core.drawBaseImage(self.backgroundFrames[i]) - background.paste(layer) - return background - - @pyqtSlot(str, str, str, list) - def createVideo(self, backgroundImage, inputFile, outputFile, components): + @pyqtSlot(str, str, list) + def createVideo(self, inputFile, outputFile, components): self.encoding.emit(True) self.components = components self.outputFile = outputFile + self.bgI = 0 # tracked video frame self.reset() self.width = int(self.core.settings.value('outputWidth')) self.height = int(self.core.settings.value('outputHeight')) # print('worker thread id: {}'.format(QtCore.QThread.currentThreadId())) progressBarValue = 0 self.progressBarUpdate.emit(progressBarValue) - self.progressBarSetText.emit('Loading background image…') - - self.backgroundImage = backgroundImage - - self.backgroundFrames = self.core.parseBaseImage(backgroundImage) - if len(self.backgroundFrames) < 2: - # the base image is not a video so we can draw it now - self.imBackground = self.getBackgroundAtIndex(0) - else: - # base images will be drawn while drawing the audio bars - self.imBackground = None - self.bgI = 0 self.progressBarSetText.emit('Loading audio file...') self.completeAudioArray = self.core.readAudioFile(inputFile, self) @@ -165,7 +143,7 @@ class Worker(QtCore.QObject): ) if properties and 'static' in properties: - self.staticComponents[compNo] = copy(comp.frameRender(compNo, 0)) + self.staticComponents[compNo] = copy(comp.frameRender(compNo, 0, 0)) self.progressBarUpdate.emit(100) self.compositeQueue = Queue() -- cgit v1.2.3 From 277e86f2795093deaa12f294c29ac2f951ae65cd Mon Sep 17 00:00:00 2001 From: tassaron Date: Sun, 4 Jun 2017 20:27:43 -0400 Subject: not dumping frames anymore, but not working yet either will finish later --- components/video.py | 69 +++++++++++++++++++++++++++++++++-------------------- core.py | 5 ++-- main.py | 2 +- video_thread.py | 1 - 4 files changed, 47 insertions(+), 30 deletions(-) (limited to 'video_thread.py') diff --git a/components/video.py b/components/video.py index 1a9f344..97b824c 100644 --- a/components/video.py +++ b/components/video.py @@ -30,11 +30,11 @@ class Component(__base__.Component): def previewRender(self, previewWorker): self.width = int(previewWorker.core.settings.value('outputWidth')) self.height = int(previewWorker.core.settings.value('outputHeight')) - frames = self.getVideoFrames(True) - if not hasattr(self, 'staticFrame') or not self.working and frames: + frame1 = self.getPreviewFrame() + if not hasattr(self, 'staticFrame') or not self.working and frame1: frame = Image.new("RGBA", (self.width, self.height), (0, 0, 0, 0)) - if frames: - im = Image.open(frames[0]) + if frame1: + im = Image.open(frame1) im = self.resize(im) frame.paste(im) if not self.working: @@ -77,39 +77,56 @@ class Component(__base__.Component): self.settings.setValue("backgroundDir", os.path.dirname(filename)) self.page.lineEdit_video.setText(filename) self.update() - - def getVideoFrames(self, preview=False): - # recreate the temporary directory so it is empty - # FIXME: don't dump all the frames at once, don't dump more than sound length - # FIXME: make cancellable, report status to user, etc etc etc + + def getPreviewFrame(self): if not self.videoPath: return name = os.path.basename(self.videoPath).split('.', 1)[0] - if preview: - filename = 'preview%s.jpg' % name - if os.path.exists(os.path.join(self.parent.core.tempDir, filename)): - return False - else: - filename = name+'-frame%05d.jpg' - - # recreate tempDir and dump needed frame(s) - self.parent.core.deleteTempDir() - os.mkdir(self.parent.core.tempDir) - if preview: - options = '-ss 10 -vframes 1' - else: - options = '' #'-vframes 99999' + filename = 'preview%s.jpg' % name + if os.path.exists(os.path.join(self.parent.core.tempDir, filename)): + # no, we don't need a new preview frame + return False + + # get a preview frame subprocess.call( \ '%s -i "%s" -y %s "%s"' % ( \ self.parent.core.FFMPEG_BIN, self.videoPath, - options, + '-ss 10 -vframes 1', os.path.join(self.parent.core.tempDir, filename) ), shell=True ) - print('### Got Preview Frame From %s ###' % name if preview else '### Finished Dumping Frames From %s ###' % name) - return sorted([os.path.join(self.parent.core.tempDir, f) for f in os.listdir(self.parent.core.tempDir)]) + print('### Got Preview Frame From %s ###' % name) + return os.path.join(self.parent.core.tempDir, filename) + + def getVideoFrames(self): + # FIXME: make cancellable, report status to user, etc etc etc + if not self.videoPath: + return + + command = [ + self.parent.core.FFMPEG_BIN, + '-i', self.videoPath, + '-f', 'image2pipe', + '-vcodec', 'rawvideo', '-', + '-pix_fmt', 'rgba', + ] + + # pipe in video frames from ffmpeg + in_pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=8**10) + # maybe bufsize=4*self.width*self.height+100 ? + chunk = 4*self.width*self.height + + frames = [] + while True: + byteFrame = in_pipe.stdout.read(chunk) + if len(byteFrame) == 0: + break + img = Image.frombytes('RGBA', (self.width, self.height), byteFrame, 'raw', 'RGBa') + frames.append(img) + + return frames def resize(self, im): if im.size != (self.width, self.height): diff --git a/core.py b/core.py index ecbf12c..99403f1 100644 --- a/core.py +++ b/core.py @@ -15,6 +15,8 @@ class Core(): def __init__(self): self.FFMPEG_BIN = self.findFfmpeg() self.tempDir = os.path.join(tempfile.gettempdir(), 'audio-visualizer-python-data') + if not os.path.exists(self.tempDir): + os.makedirs(self.tempDir) atexit.register(self.deleteTempDir) def findFfmpeg(self): @@ -94,8 +96,7 @@ class Core(): def deleteTempDir(self): try: - if os.path.exists(self.tempDir): - rmtree(self.tempDir) + rmtree(self.tempDir) except FileNotFoundError: pass diff --git a/main.py b/main.py index da72b1e..637ece8 100644 --- a/main.py +++ b/main.py @@ -253,7 +253,7 @@ class Main(QtCore.QObject): outputDir = self.settings.value("outputDir", expanduser("~")) fileName = QtGui.QFileDialog.getSaveFileName(self.window, - "Set Output Video File", outputDir, "Video Files (*.mkv)"); + "Set Output Video File", outputDir, "Video Files (*.mkv *.mp4)"); if not fileName == "": self.settings.setValue("outputDir", os.path.dirname(fileName)) diff --git a/video_thread.py b/video_thread.py index e74fffa..0542bc2 100644 --- a/video_thread.py +++ b/video_thread.py @@ -228,7 +228,6 @@ class Worker(QtCore.QObject): self.error = False self.canceled = False self.parent.drawPreview() - self.core.deleteTempDir() self.stopped = True self.encoding.emit(False) self.videoCreated.emit() -- cgit v1.2.3 From be18deece5843ac8d2c7af64704e3fb360a05a25 Mon Sep 17 00:00:00 2001 From: DH4 Date: Mon, 5 Jun 2017 04:54:58 -0500 Subject: Performance Tuning. FIXME: Video component frames are rendered out of order. Video component creates a severe performance bottleneck. --- components/video.py | 21 ++++++++++----------- main.py | 4 ++-- video_thread.py | 20 ++++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) (limited to 'video_thread.py') diff --git a/components/video.py b/components/video.py index b009baa..422b952 100644 --- a/components/video.py +++ b/components/video.py @@ -36,8 +36,6 @@ class Component(__base__.Component): frame = Image.new("RGBA", (self.width, self.height), (0, 0, 0, 0)) if frame1: im = Image.open(frame1) - self.realSize = im.size - im = self.resize(im) frame.paste(im) if not self.working: self.staticFrame = frame @@ -61,11 +59,9 @@ class Component(__base__.Component): return self.staticFrame # make a new frame - width, height = self.realSize - image = numpy.fromstring(byteFrame, dtype='uint8') - image = image.reshape((width, height, 4)) - image = Image.frombytes('RGBA', (width, height), image, 'raw', 'RGBa') - image = self.resize(image) + width = self.width + height = self.height + image = Image.frombytes('RGBA', (width, height), byteFrame) self.staticFrame = image return self.staticFrame @@ -80,7 +76,7 @@ class Component(__base__.Component): def pickVideo(self): imgDir = self.settings.value("backgroundDir", os.path.expanduser("~")) filename = QtGui.QFileDialog.getOpenFileName(self.page, - "Choose Video", imgDir, "Video Files (*.mp4)") + "Choose Video", imgDir, "Video Files (*.mp4 *.mov)") if filename: self.settings.setValue("backgroundDir", os.path.dirname(filename)) self.page.lineEdit_video.setText(filename) @@ -97,10 +93,11 @@ class Component(__base__.Component): # get a preview frame subprocess.call( \ - '%s -i "%s" -y %s "%s"' % ( \ + '%s -i "%s" -y %s %s "%s"' % ( \ self.parent.core.FFMPEG_BIN, self.videoPath, '-ss 10 -vframes 1', + '-filter:v scale='+str(self.width)+':'+str(self.height), os.path.join(self.parent.core.tempDir, filename) ), shell=True @@ -114,16 +111,18 @@ class Component(__base__.Component): command = [ self.parent.core.FFMPEG_BIN, + '-thread_queue_size', '512', '-i', self.videoPath, '-f', 'image2pipe', '-pix_fmt', 'rgba', + '-filter:v', 'scale='+str(self.width)+':'+str(self.height), '-vcodec', 'rawvideo', '-', ] # pipe in video frames from ffmpeg in_pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=10**8) - width, height = self.realSize - self.chunkSize = 4*width*height + #width, height = self.realSize + self.chunkSize = 4*self.width*self.height return in_pipe diff --git a/main.py b/main.py index 637ece8..36fc989 100644 --- a/main.py +++ b/main.py @@ -243,7 +243,7 @@ class Main(QtCore.QObject): inputDir = self.settings.value("inputDir", expanduser("~")) fileName = QtGui.QFileDialog.getOpenFileName(self.window, - "Open Music File", inputDir, "Music Files (*.mp3 *.wav *.ogg *.flac)"); + "Open Music File", inputDir, "Music Files (*.mp3 *.wav *.ogg *.fla *.aac)"); if not fileName == "": self.settings.setValue("inputDir", os.path.dirname(fileName)) @@ -253,7 +253,7 @@ class Main(QtCore.QObject): outputDir = self.settings.value("outputDir", expanduser("~")) fileName = QtGui.QFileDialog.getSaveFileName(self.window, - "Set Output Video File", outputDir, "Video Files (*.mkv *.mp4)"); + "Set Output Video File", outputDir, "Video Files (*.mp4 *.mov *.mkv *.avi *.webm *.flv)"); if not fileName == "": self.settings.setValue("outputDir", os.path.dirname(fileName)) diff --git a/video_thread.py b/video_thread.py index 0542bc2..ac4162c 100644 --- a/video_thread.py +++ b/video_thread.py @@ -37,20 +37,19 @@ class Worker(QtCore.QObject): def renderNode(self): while not self.stopped: i = self.compositeQueue.get() - - frame = Image.new( - "RGBA", - (self.width, self.height), - (0, 0, 0, 0) - ) + frame = None for compNo, comp in reversed(list(enumerate(self.components))): if compNo in self.staticComponents and self.staticComponents[compNo] != None: - frame = Image.alpha_composite(frame, self.staticComponents[compNo]) + if frame is None: + frame = self.staticComponents[compNo] + else: + frame = Image.alpha_composite(frame, self.staticComponents[compNo]) else: - frame = Image.alpha_composite(frame, comp.frameRender(compNo, i[0], i[1])) - - # frame.paste(compFrame, mask=compFrame) + if frame is None: + frame = comp.frameRender(compNo, i[0], i[1]) + else: + frame = Image.alpha_composite(frame, comp.frameRender(compNo, i[0], i[1])) self.renderQueue.put([i[0], frame]) self.compositeQueue.task_done() @@ -98,6 +97,7 @@ class Worker(QtCore.QObject): ffmpegCommand = [ self.core.FFMPEG_BIN, + '-thread_queue_size', '512', '-y', # (optional) means overwrite the output file if it already exists. '-f', 'rawvideo', '-vcodec', 'rawvideo', -- cgit v1.2.3