From e5d8e2db3c94528d133041c85da1528157db5afe Mon Sep 17 00:00:00 2001 From: tassaron Date: Fri, 6 May 2022 23:23:20 -0400 Subject: ffmpeg filter_complex creation code is more readable --- src/components/spectrum.py | 231 +++++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 112 deletions(-) diff --git a/src/components/spectrum.py b/src/components/spectrum.py index 7e037d6..30d5426 100644 --- a/src/components/spectrum.py +++ b/src/components/spectrum.py @@ -173,124 +173,131 @@ class Component(Component): return frame def makeFfmpegFilter(self, preview=False, startPt=0): - if preview: - w, h = self.previewSize - else: - w, h = (self.width, self.height) - color = self.page.comboBox_color.currentText().lower() - genericPreview = self.settings.value("pref_genericPreview") + '''Makes final FFmpeg filter command''' - if self.filterType == 0: # Spectrum - if self.amplitude == 0: - amplitude = 'sqrt' - elif self.amplitude == 1: - amplitude = 'cbrt' - elif self.amplitude == 2: - amplitude = '4thrt' - elif self.amplitude == 3: - amplitude = '5thrt' - elif self.amplitude == 4: - amplitude = 'lin' - elif self.amplitude == 5: - amplitude = 'log' - filter_ = ( - f'showspectrum=s={w}x{h}:' - 'slide=scroll:' - f'win_func={self.page.comboBox_window.currentText()}:' - f'color={color}:' - f'scale={amplitude},' - 'colorkey=color=black:' - 'similarity=0.1:blend=0.5' - ) - elif self.filterType == 1: # Histogram - if self.amplitude1 == 0: - amplitude = 'log' - elif self.amplitude1 == 1: - amplitude = 'lin' - if self.display == 0: - display = 'log' - elif self.display == 1: - display = 'sqrt' - elif self.display == 2: - display = 'cbrt' - elif self.display == 3: - display = 'lin' - elif self.display == 4: - display = 'rlog' - filter_ = ( - f'ahistogram=r={str(self.settings.value("outputFrameRate"))}:' - f's={w}x{h}:' - 'dmode=separate:' - f'ascale={amplitude}:' - f'scale={display}' - ) - elif self.filterType == 2: # Vector Scope - if self.amplitude2 == 0: - amplitude = 'log' - elif self.amplitude2 == 1: - amplitude = 'sqrt' - elif self.amplitude2 == 2: - amplitude = 'cbrt' - elif self.amplitude2 == 3: - amplitude = 'lin' - m = self.page.comboBox_mode.currentText() - filter_ = ( - f'avectorscope=s={w}x{h}:' - f'draw={"line" if self.draw else "dot"}:' - f'm={m}:' - f'scale={amplitude}:' - f'zoom={str(self.zoom)}' - ) - elif self.filterType == 3: # Musical Scale - filter_ = ( - f'showcqt=r={str(self.settings.value("outputFrameRate"))}:' - f's={w}x{h}:' - 'count=30:' - 'text=0:' - f'tc={str(self.tc)},' - 'colorkey=color=black:' - 'similarity=0.1:blend=0.5' - ) - elif self.filterType == 4: # Phase - filter_ = ( - f'aphasemeter=r={str(self.settings.value("outputFrameRate"))}:' - f's={w}x{h}:' - 'video=1 [atrash][vtmp1]; ' - '[atrash] anullsink; ' - '[vtmp1] colorkey=color=black:' - 'similarity=0.1:blend=0.5, ' - 'crop=in_w/8:in_h:(in_w/8)*7:0 ' + def getFilterComplexCommand(): + '''Inner function that creates the final, complex part of the filter command''' + nonlocal self + genericPreview = self.settings.value("pref_genericPreview") + + def getFilterComplexCommandForType(): + '''Determine portion of filter command that changes depending on selected type''' + nonlocal self + if preview: + w, h = self.previewSize + else: + w, h = (self.width, self.height) + color = self.page.comboBox_color.currentText().lower() + + if self.filterType == 0: # Spectrum + if self.amplitude == 0: + amplitude = 'sqrt' + elif self.amplitude == 1: + amplitude = 'cbrt' + elif self.amplitude == 2: + amplitude = '4thrt' + elif self.amplitude == 3: + amplitude = '5thrt' + elif self.amplitude == 4: + amplitude = 'lin' + elif self.amplitude == 5: + amplitude = 'log' + filter_ = ( + f'showspectrum=s={w}x{h}:' + 'slide=scroll:' + f'win_func={self.page.comboBox_window.currentText()}:' + f'color={color}:' + f'scale={amplitude},' + 'colorkey=color=black:' + 'similarity=0.1:blend=0.5' + ) + elif self.filterType == 1: # Histogram + if self.amplitude1 == 0: + amplitude = 'log' + elif self.amplitude1 == 1: + amplitude = 'lin' + if self.display == 0: + display = 'log' + elif self.display == 1: + display = 'sqrt' + elif self.display == 2: + display = 'cbrt' + elif self.display == 3: + display = 'lin' + elif self.display == 4: + display = 'rlog' + filter_ = ( + f'ahistogram=r={str(self.settings.value("outputFrameRate"))}:' + f's={w}x{h}:' + 'dmode=separate:' + f'ascale={amplitude}:' + f'scale={display}' + ) + elif self.filterType == 2: # Vector Scope + if self.amplitude2 == 0: + amplitude = 'log' + elif self.amplitude2 == 1: + amplitude = 'sqrt' + elif self.amplitude2 == 2: + amplitude = 'cbrt' + elif self.amplitude2 == 3: + amplitude = 'lin' + m = self.page.comboBox_mode.currentText() + filter_ = ( + f'avectorscope=s={w}x{h}:' + f'draw={"line" if self.draw else "dot"}:' + f'm={m}:' + f'scale={amplitude}:' + f'zoom={str(self.zoom)}' + ) + elif self.filterType == 3: # Musical Scale + filter_ = ( + f'showcqt=r={str(self.settings.value("outputFrameRate"))}:' + f's={w}x{h}:' + 'count=30:' + 'text=0:' + f'tc={str(self.tc)},' + 'colorkey=color=black:' + 'similarity=0.1:blend=0.5' + ) + elif self.filterType == 4: # Phase + filter_ = ( + f'aphasemeter=r={str(self.settings.value("outputFrameRate"))}:' + f's={w}x{h}:' + 'video=1 [atrash][vtmp1]; ' + '[atrash] anullsink; ' + '[vtmp1] colorkey=color=black:' + 'similarity=0.1:blend=0.5, ' + 'crop=in_w/8:in_h:(in_w/8)*7:0 ' + ) + return filter_ + + + if self.filterType < 2: + exampleSnd = exampleSound('freq') + elif self.filterType == 2 or self.filterType == 4: + exampleSnd = exampleSound('stereo') + elif self.filterType == 3: + exampleSnd = exampleSound('white') + compression = 'compand=gain=4,' if self.compress else '' + aformat = 'aformat=channel_layouts=mono,' if self.mono and self.filterType not in (2, 4) else '' + filter_ = getFilterComplexCommandForType() + hflip = 'hflip, ' if self.mirror else '' + trim = 'trim=start=%s:end=%s, ' % ("{0:.3f}".format(startPt + 12), "{0:.3f}".format(startPt + 12.5)) if preview else '' + scale_ = 'scale=%sx%s' % scale(self.scale, self.width, self.height, str) + hue = ', hue=h=%s:s=10' % str(self.hue) if self.hue > 0 and self.filterType != 3 else '' + convolution = ', convolution=-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2' if self.filterType == 3 else '' + + return ( + f"{exampleSnd if preview and genericPreview else '[0:a] '}" + f"{compression}{aformat}{filter_} [v1]; " + f"[v1] {hflip}{trim}{scale_}{hue}{convolution} [v]" ) - if self.filterType < 2: - exampleSnd = exampleSound('freq') - elif self.filterType == 2 or self.filterType == 4: - exampleSnd = exampleSound('stereo') - elif self.filterType == 3: - exampleSnd = exampleSound('white') return [ '-filter_complex', - '%s%s%s%s [v1]; ' - '[v1] %s%s%s%s%s [v]' % ( - exampleSnd if preview and genericPreview else '[0:a] ', - 'compand=gain=4,' if self.compress else '', - 'aformat=channel_layouts=mono,' - if self.mono and self.filterType not in (2, 4) else '', - filter_, - 'hflip, ' if self.mirror else '', - 'trim=start=%s:end=%s, ' % ( - "{0:.3f}".format(startPt + 12), - "{0:.3f}".format(startPt + 12.5) - ) if preview else '', - 'scale=%sx%s' % scale( - self.scale, self.width, self.height, str), - ', hue=h=%s:s=10' % str(self.hue) - if self.hue > 0 and self.filterType != 3 else '', - ', convolution=-2 -1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2:-2 ' - '-1 0 -1 1 1 0 1 2:-2 -1 0 -1 1 1 0 1 2' - if self.filterType == 3 else '' - ), + getFilterComplexCommand(), '-map', '[v]', ] -- cgit v1.2.3