Skip to content

Commit

Permalink
add force encoding and encoded with selected encoder options
Browse files Browse the repository at this point in the history
  • Loading branch information
yajrendrag committed Apr 21, 2024
1 parent 77e84f2 commit 7094ad0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 4 additions & 0 deletions source/asad_audio_encoder/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

**<span style="color:#56adda">0.0.3</span>**
- added force encoding option
- added check so file not reprocessed if stream encoder equals configured encoder

**<span style="color:#56adda">0.0.2</span>**
- Added new "Default/None" option which doesn't set a bitrate (and uses the codec default)
- Changed codec names
Expand Down
2 changes: 1 addition & 1 deletion source/asad_audio_encoder/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"on_worker_process": 0
},
"tags": "audio,encoder,ffmpeg,library file test",
"version": "0.0.2"
"version": "0.0.3"
}
27 changes: 21 additions & 6 deletions source/asad_audio_encoder/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

class Settings(PluginSettings):
settings = {
"force_encoding": False,
"encoder": "libfdk_aac",
"channel_rate": "0",
"customize": False,
Expand All @@ -55,6 +56,9 @@ class Settings(PluginSettings):
def __init__(self, *args, **kwargs):
super(Settings, self).__init__(*args, **kwargs)
self.form_settings = {
"force_encoding": {
"label": "Check this option if you want to force encoding if the encoder already matches the selected encoder",
},
"encoder": {
"label": "Enter encoder",
"description": "Select audio encoder for the encoder:",
Expand Down Expand Up @@ -150,13 +154,21 @@ def __set_custom_suffix_form_settings(self):
values["display"] = 'hidden'
return values

def s2_encode(streams, probe_format, encoder, channel_rate, abspath):
def s2_encode(streams, probe_format, encoder, force_encoding, channel_rate, abspath):
try:
all_audio_streams=[(i, streams[i]["channels"], probe_format["bit_rate"]) for i in range(len(streams)) if "codec_type" in streams[i] and streams[i]["codec_type"] == 'audio' and "bit_rate" in probe_format]
all_audio_streams = [i for i in range(len(streams)) if "codec_type" in streams[i] and streams[i]["codec_type"] == 'audio']
except:
logger.error("Error finding audio stream to encode")
return [0,0,0]
return all_audio_streams

streams_to_encode = [(i, streams[i]["channels"], probe_format["bit_rate"]) for i in range(len(streams))
if ("codec_type" in streams[i] and streams[i]["codec_type"] == 'audio' and "codec_name" in streams[i] and streams[i]["codec_name"] != encoder and "bit_rate" in probe_format)
or (force_encoding == True and "codec_type" in streams[i] and streams[i]["codec_type"] == 'audio' and "bit_rate" in probe_format)]

if streams_to_encode != []:
return streams_to_encode
else:
return [0,0,0]

def on_library_management_file_test(data):
"""
Expand Down Expand Up @@ -192,11 +204,12 @@ def on_library_management_file_test(data):
else:
settings = Settings()

# get encoder & channel_rate
# get encoder, channel_rate & force_encoding
encoder = settings.get_setting('encoder')
channel_rate = settings.get_setting('channel_rate')
force_encoding = settings.get_setting('force_encoding')

if s2_encode(probe_streams, probe_format, encoder, channel_rate, abspath) != [0,0,0]:
if s2_encode(probe_streams, probe_format, encoder, force_encoder, channel_rate, abspath) != [0,0,0]:
# Mark this file to be added to the pending tasks
data['add_file_to_pending_tasks'] = True
logger.debug("File '{}' should be added to task list. Probe found streams require processing.".format(abspath))
Expand Down Expand Up @@ -249,6 +262,8 @@ def on_worker_process(data):
channel_rate = settings.get_setting('channel_rate')
custom_audio = settings.get_setting('custom_audio')
custom_suffix = settings.get_setting('custom_suffix')
force_encoding = settings.get_setting('force_encoding')

if custom_suffix:
sfx = custom_suffix
else:
Expand All @@ -258,7 +273,7 @@ def on_worker_process(data):
ffmpeg_args = ['-hide_banner', '-loglevel', 'info', '-i', str(abspath), '-max_muxing_queue_size', '9999', '-strict', '-2']

# Build stream maps for audio streams to be encoded
streams_to_process = s2_encode(probe_streams, probe_format, encoder, channel_rate, abspath)
streams_to_process = s2_encode(probe_streams, probe_format, encoder, force_encoding, channel_rate, abspath)
if streams_to_process != [0,0,0]:

all_streams = [i for i in range(len(probe_streams))]
Expand Down

0 comments on commit 7094ad0

Please sign in to comment.