Skip to content

Commit

Permalink
added ability to specify channels
Browse files Browse the repository at this point in the history
  • Loading branch information
mmguero committed May 30, 2023
1 parent 9abbd5b commit c064e6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = monkeyplug
version = 1.3.1
version = 1.3.2
author = Seth Grover
author_email = [email protected]
description = monkeyplug is a little script to mute profanity in audio files.
Expand Down
2 changes: 1 addition & 1 deletion src/monkeyplug/__init__.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
__all__ = []

Expand Down
38 changes: 28 additions & 10 deletions src/monkeyplug/monkeyplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import argparse
import base64
import errno
import json
import mmguero
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -194,6 +197,7 @@ def __init__(
mPath,
outputJson,
aParams=None,
aChannels=AUDIO_DEFAULT_CHANNELS,
wChunk=AUDIO_DEFAULT_WAV_FRAMES_CHUNK,
padMsecPre=0,
padMsecPost=0,
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -487,6 +493,7 @@ def EncodeCleanAudio(self):

#################################################################################


###################################################################################################
# RunMonkeyPlug
def RunMonkeyPlug():
Expand Down Expand Up @@ -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="<int>",
type=int,
default=AUDIO_DEFAULT_CHANNELS,
help=f"Audio output channels (default: {AUDIO_DEFAULT_CHANNELS})",
)
parser.add_argument(
"-f",
"--format",
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit c064e6c

Please sign in to comment.