/
kushpel
/
plugins
Обзор
Документация
Войти
/
kushpel
/
plugins
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
source/encoder_audio_libfdk_aac/plugin.py
327 строк
12 KB
yajrendrag
use .unmanic file to track history
14 дек 2024, 01:53
14 дек 2024, 01:53
323af77
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ plugins.__init__.py Written by: Josh.5 <jsunnex@gmail.com> Date: 23 Aug 2021, (20:38 PM) Copyright: Copyright (C) 2021 Josh Sunnex This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. """ import logging import os from unmanic.libs.unplugins.settings import PluginSettings from unmanic.libs.directoryinfo import UnmanicDirectoryInfo from encoder_audio_libfdk_aac.lib.ffmpeg import StreamMapper, Probe, Parser # Configure plugin logger logger = logging.getLogger("Unmanic.Plugin.encoder_audio_aac") class Settings(PluginSettings): settings = { "advanced": False, "force_processing": False, "max_muxing_queue_size": 2048, "main_options": "", "advanced_options": "", "custom_options": "", } def __init__(self, *args, **kwargs): super(Settings, self).__init__(*args, **kwargs) self.form_settings = { "advanced": { "label": "Write your own FFmpeg params", }, "force_processing": { "label": "process streams even if audio is already aac encoded", }, "max_muxing_queue_size": self.__set_max_muxing_queue_size_form_settings(), "main_options": self.__set_main_options_form_settings(), "advanced_options": self.__set_advanced_options_form_settings(), "custom_options": self.__set_custom_options_form_settings(), } def __set_max_muxing_queue_size_form_settings(self): values = { "label": "Max input stream packet buffer", "input_type": "slider", "slider_options": { "min": 1024, "max": 10240, }, } if self.get_setting('advanced'): values["display"] = 'hidden' return values def __set_main_options_form_settings(self): values = { "label": "Write your own custom main options", "input_type": "textarea", } if not self.get_setting('advanced'): values["display"] = 'hidden' return values def __set_advanced_options_form_settings(self): values = { "label": "Write your own custom advanced options", "input_type": "textarea", } if not self.get_setting('advanced'): values["display"] = 'hidden' return values def __set_custom_options_form_settings(self): values = { "label": "Write your own custom audio options", "input_type": "textarea", } if not self.get_setting('advanced'): values["display"] = 'hidden' return values class PluginStreamMapper(StreamMapper): def __init__(self): super(PluginStreamMapper, self).__init__(logger, ['audio']) self.codec = 'aac' self.encoder = 'libfdk_aac' self.settings = None def set_default_values(self, settings, abspath, probe): """ Configure the stream mapper with defaults :param settings: :param abspath: :param probe: :return: """ self.abspath = abspath # Set the file probe data self.set_probe(probe) # Set the input file self.set_input_file(abspath) # Configure settings self.settings = settings # Build default options of advanced mode if self.settings.get_setting('advanced'): # If any main options are provided, overwrite them main_options = settings.get_setting('main_options').split() if main_options: # Overwrite all main options self.main_options = main_options # If any advanced options are provided, overwrite them advanced_options = settings.get_setting('advanced_options').split() if advanced_options: # Overwrite all advanced options self.advanced_options = advanced_options @staticmethod def calculate_bitrate(stream_info: dict): channels = stream_info.get('channels', 2) return int(channels) * 64 def test_stream_needs_processing(self, stream_info: dict): force_processing = self.settings.get_setting('force_processing') # Ignore streams already of the required codec_name if stream_info.get('codec_name').lower() in [self.codec] and not force_processing: return False return True def custom_stream_mapping(self, stream_info: dict, stream_id: int): stream_encoding = ['-c:a:{}'.format(stream_id), self.encoder] if self.settings.get_setting('advanced'): stream_encoding += self.settings.get_setting('custom_options').split() else: # Automatically detect bitrate for this stream. if stream_info.get('channels'): # Use 64K for the bitrate per channel calculated_bitrate = self.calculate_bitrate(stream_info) channels = int(stream_info.get('channels')) if channels > 6: channels = 6 stream_encoding += [ '-ac:a:{}'.format(stream_id), '{}'.format(channels), '-b:a:{}'.format(stream_id), "{}k".format(calculated_bitrate) ] return { 'stream_mapping': ['-map', '0:a:{}'.format(stream_id)], 'stream_encoding': stream_encoding, } def encoded_audio(settings): encoder = 'libfdk_aac' return 'encoded_audio=encoder={}'.format(encoder) def file_streams_already_encoded(settings, path): directory_info = UnmanicDirectoryInfo(os.path.dirname(path)) try: streams_already_encoded = directory_info.get('encoder_audio_libfdk_aac', os.path.basename(path)) except NoSectionError as e: streams_already_encoded = '' except NoOptionError as e: streams_already_encoded = '' except Exception as e: logger.debug("Unknown exception {}.".format(e)) streams_already_encoded = '' if streams_already_encoded: logger.debug("File's streams were previously encoded with {}.".format(streams_already_encoded)) return True # Default to... return False def on_library_management_file_test(data): """ Runner function - enables additional actions during the library management file tests. The 'data' object argument includes: path - String containing the full path to the file being tested. issues - List of currently found issues for not processing the file. add_file_to_pending_tasks - Boolean, is the file currently marked to be added to the queue for processing. :param data: :return: """ # Get the path to the file abspath = data.get('path') # Get file probe probe = Probe(logger, allowed_mimetypes=['audio', 'video']) if not probe.file(abspath): # File probe failed, skip the rest of this test return data # Configure settings object (maintain compatibility with v1 plugins) if data.get('library_id'): settings = Settings(library_id=data.get('library_id')) else: settings = Settings() # Get stream mapper mapper = PluginStreamMapper() mapper.set_default_values(settings, abspath, probe) if mapper.streams_need_processing() and not file_streams_already_encoded(settings, abspath): # 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)) else: logger.debug("File '{}' does not contain streams require processing.".format(abspath)) return data def on_worker_process(data): """ Runner function - enables additional configured processing jobs during the worker stages of a task. The 'data' object argument includes: exec_command - A command that Unmanic should execute. Can be empty. command_progress_parser - A function that Unmanic can use to parse the STDOUT of the command to collect progress stats. Can be empty. file_in - The source file to be processed by the command. file_out - The destination that the command should output (may be the same as the file_in if necessary). original_file_path - The absolute path to the original file. repeat - Boolean, should this runner be executed again once completed with the same variables. :param data: :return: """ # Default to no FFMPEG command required. This prevents the FFMPEG command from running if it is not required data['exec_command'] = [] data['repeat'] = False # Get the path to the file abspath = data.get('file_in') # Get file probe probe = Probe(logger, allowed_mimetypes=['audio', 'video']) if not probe.file(abspath): # File probe failed, skip the rest of this test return data # Configure settings object (maintain compatibility with v1 plugins) settings = Settings(library_id=data.get('library_id')) # Get stream mapper mapper = PluginStreamMapper() mapper.set_default_values(settings, abspath, probe) if mapper.streams_need_processing(): # Set the input file mapper.set_input_file(abspath) # Set the output file mapper.set_output_file(data.get('file_out')) # Get generated ffmpeg args ffmpeg_args = mapper.get_ffmpeg_args() # Apply ffmpeg args to command data['exec_command'] = ['ffmpeg'] data['exec_command'] += ffmpeg_args # Set the parser parser = Parser(logger) parser.set_probe(probe) data['command_progress_parser'] = parser.parse_progress def on_postprocessor_task_results(data): """ Runner function - provides a means for additional postprocessor functions based on the task success. The 'data' object argument includes: task_processing_success - Boolean, did all task processes complete successfully. file_move_processes_success - Boolean, did all postprocessor movement tasks complete successfully. destination_files - List containing all file paths created by postprocessor file movements. source_data - Dictionary containing data pertaining to the original source file. :param data: :return: """ # We only care that the task completed successfully. # If a worker processing task was unsuccessful, dont mark the file streams as kept # TODO: Figure out a way to know if a file's streams were kept but another plugin was the # cause of the task processing failure flag if not data.get('task_processing_success'): return data # Configure settings object (maintain compatibility with v1 plugins) if data.get('library_id'): settings = Settings(library_id=data.get('library_id')) else: settings = Settings() # Loop over the destination_files list and update the directory info file for each one for destination_file in data.get('destination_files'): directory_info = UnmanicDirectoryInfo(os.path.dirname(destination_file)) directory_info.set('encoder_audio_libfdk_aac', os.path.basename(destination_file), encoded_audio(settings)) directory_info.save() logger.debug("Audio encoder of libfdk_aac being written for '{}'.".format(destination_file)) return data