From c064e6cc3aabbedffcd78907fb6e537880cfaa1b Mon Sep 17 00:00:00 2001 From: SG Date: Mon, 29 May 2023 21:52:28 -0600 Subject: [PATCH] added ability to specify channels --- setup.cfg | 2 +- src/monkeyplug/__init__.py | 2 +- src/monkeyplug/monkeyplug.py | 38 ++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/setup.cfg b/setup.cfg index b7ea52f..8b5818f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = monkeyplug -version = 1.3.1 +version = 1.3.2 author = Seth Grover author_email = mero.mero.guero@gmail.com description = monkeyplug is a little script to mute profanity in audio files. diff --git a/src/monkeyplug/__init__.py b/src/monkeyplug/__init__.py index 3f7822c..ee3f4b1 100644 --- a/src/monkeyplug/__init__.py +++ b/src/monkeyplug/__init__.py @@ -1,6 +1,6 @@ """monkeyplug is a little script to mute profanity in audio files.""" -__version__ = "1.3.1" +__version__ = "1.3.2" __author__ = "Seth Grover " __all__ = [] diff --git a/src/monkeyplug/monkeyplug.py b/src/monkeyplug/monkeyplug.py index d6afa41..5313b9a 100755 --- a/src/monkeyplug/monkeyplug.py +++ b/src/monkeyplug/monkeyplug.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import argparse +import base64 import errno import json import mmguero @@ -16,14 +17,15 @@ from urllib.parse import urlparse ################################################################################################### +CHANNELS_REPLACER = 'CHANNELS' AUDIO_DEFAULT_PARAMS_BY_FORMAT = { - "flac": ["-c:a", "flac", "-ar", "44100", "-ac", "2"], - "m4a": ["-c:a", "aac", "-b:a", "128K", "-ar", "44100", "-ac", "2"], - "aac": ["-c:a", "aac", "-b:a", "128K", "-ar", "44100", "-ac", "2"], - "mp3": ["-c:a", "libmp3lame", "-b:a", "128K", "-ar", "44100", "-ac", "2"], - "ogg": ["-c:a", "libvorbis", "-qscale:a", "5", "-ar", "44100", "-ac", "2"], - "opus": ["-c:a", "libopus", "-b:a", "128K", "-ar", "48000", "-ac", "2"], - "ac3": ["-c:a", "ac3", "-b:a", "128K", "-ar", "44100", "-ac", "2"], + "flac": ["-c:a", "flac", "-ar", "44100", "-ac", CHANNELS_REPLACER], + "m4a": ["-c:a", "aac", "-b:a", "128K", "-ar", "44100", "-ac", CHANNELS_REPLACER], + "aac": ["-c:a", "aac", "-b:a", "128K", "-ar", "44100", "-ac", CHANNELS_REPLACER], + "mp3": ["-c:a", "libmp3lame", "-b:a", "128K", "-ar", "44100", "-ac", CHANNELS_REPLACER], + "ogg": ["-c:a", "libvorbis", "-qscale:a", "5", "-ar", "44100", "-ac", CHANNELS_REPLACER], + "opus": ["-c:a", "libopus", "-b:a", "128K", "-ar", "48000", "-ac", CHANNELS_REPLACER], + "ac3": ["-c:a", "ac3", "-b:a", "128K", "-ar", "44100", "-ac", CHANNELS_REPLACER], } AUDIO_CODEC_TO_FORMAT = { "aac": "m4a", @@ -35,6 +37,7 @@ } AUDIO_DEFAULT_FORMAT = "mp3" +AUDIO_DEFAULT_CHANNELS = 2 AUDIO_MATCH_FORMAT = "MATCH" AUDIO_INTERMEDIATE_PARAMS = ["-c:a", "pcm_s16le", "-ac", "1", "-ar", "16000"] AUDIO_DEFAULT_WAV_FRAMES_CHUNK = 8000 @@ -194,6 +197,7 @@ def __init__( mPath, outputJson, aParams=None, + aChannels=AUDIO_DEFAULT_CHANNELS, wChunk=AUDIO_DEFAULT_WAV_FRAMES_CHUNK, padMsecPre=0, padMsecPost=0, @@ -281,7 +285,9 @@ def __init__( # they specified custom ffmpeg encoding params self.aParams = aParams if self.aParams.startswith("base64:"): - self.aParams = base64.b64decode(self.aParams[7:]).decode("utf-8").split(' ') + self.aParams = base64.b64decode(self.aParams[7:]).decode("utf-8") + self.aParams = self.aParams.split(' ') + self.aParams = [str(aChannels) if aParam == CHANNELS_REPLACER else aParam for aParam in self.aParams] # if we're actually just replacing the audio stream(s) inside a video file, the actual output file is still a video file self.outputVideoFileFormat = ( @@ -487,6 +493,7 @@ def EncodeCleanAudio(self): ################################################################################# + ################################################################################################### # RunMonkeyPlug def RunMonkeyPlug(): @@ -545,10 +552,19 @@ def RunMonkeyPlug(): parser.add_argument( "-a", "--audio-params", - help=f"Audio parameters for ffmpeg (default depends on output audio codec\")", + help=f"Audio parameters for ffmpeg (default depends on output audio codec)", dest="aParams", default=None, ) + parser.add_argument( + "-c", + "--channels", + dest="aChannels", + metavar="", + type=int, + default=AUDIO_DEFAULT_CHANNELS, + help=f"Audio output channels (default: {AUDIO_DEFAULT_CHANNELS})", + ) parser.add_argument( "-f", "--format", @@ -614,7 +630,8 @@ def RunMonkeyPlug(): try: parser.error = parser.exit args = parser.parse_args() - except SystemExit: + except SystemExit as sy: + mmguero.eprint(sy) parser.print_help() exit(2) @@ -635,6 +652,7 @@ def RunMonkeyPlug(): args.modelPath, args.outputJson, aParams=args.aParams, + aChannels=args.aChannels, wChunk=args.readFramesChunk, padMsecPre=args.padMsecPre if args.padMsecPre > 0 else args.padMsec, padMsecPost=args.padMsecPost if args.padMsecPost > 0 else args.padMsec,