Ejemplo n.º 1
0
    def __init__(self, converter_config: ConverterConfig):
        """

        :param converter_config:
        :raise:
        """
        super().__init__()

        print(f"version: {__version__.__version__}")

        # Validate python version
        Validation.python_version(
            MediaObserver.PY_VERSION_MIN,
            f"Python version required >= {MediaObserver.PY_VERSION_MIN}")

        self.__converter_config = converter_config

        # Load loggers
        self.__init_loggers(converter_config.log_filename)
        # Check permissions
        self.__check_permissions()

        self.__observer = Observer(timeout=converter_config.media_in_timeout)
        self.__event_handler = MediaEventHandler(
            converter_config.general_processes)
Ejemplo n.º 2
0
    def start(self) -> None:
        # Construct configuration file
        if len(sys.argv) > 1:
            config_file = sys.argv[1]
        else:
            config_file = f"{Common.get_proj_root_path()}/res/conf/conf.ini"

        try:
            Validation.is_file_readable(
                config_file,
                f"Error on '{config_file}': configuration file *must* exists and be readable"
            )

            converter_config = ConverterConfig.get_instance()
            converter_config.load_from(config_file)

            self.__media_observer = MediaObserver(converter_config)
            atexit.register(self.stop)
            # Blocking call
            self.__media_observer.start()
        except SyntaxError:
            print(
                f"Syntax error in configuration file.\n"
                f"Make sure to follow JSON format while defining formats, sources, destinations and rules."
            )
        except Exception as e:
            print(e)
Ejemplo n.º 3
0
    def __init__(self, ffmpeg: str = FFMPEG_BIN, ffprobe: str = FFPROBE_BIN):
        super().__init__()

        Validation.is_installed(ffmpeg, f"Wrong path for {FFmpeg.FFMPEG_BIN}")
        Validation.is_installed(ffprobe,
                                f"Wrong path for {FFmpeg.FFPROBE_BIN}")

        FFmpeg.__LOG = LogManager.get_instance().get(
            LogManager.Logger.CONVERTER)
        self.__converter = Converter(ffmpeg, ffprobe)
Ejemplo n.º 4
0
    def on_error(self,
                 media_info: MediaInfo = None,
                 exception: Exception = None) -> None:
        super().on_error(media_info, exception)
        FFmpeg.__LOG.warning(f"[CONVERSION ERROR] '{media_info.filein}'")
        FFmpeg.__LOG.debug("[CONVERSION ERROR]", exc_info=True)

        try:
            Validation.is_file(media_info.fileout)
            Path(media_info.fileout).unlink()
            FFmpeg.__LOG.debug(
                f"Removed half parsed file '{media_info.fileout}'")
        except FileNotFoundError:
            pass
Ejemplo n.º 5
0
    def __submit(self, filename) -> None:
        try:
            Validation.is_file(filename)
            Validation.has_extension(filename)
        except ValidationException.MissingExtensionError:
            FileEventHandler.__LOG.debug(
                f"[SKIPPING] file '{filename}': no extension")
            return
        except FileNotFoundError:
            FileEventHandler.__LOG.debug(
                f"[SKIPPING] file '{filename}': no regular file")
            return

        self.__executor.submit(self.__dispatcher.execute, File(filename))
    def on_created(self, event: FileSystemEvent) -> None:
        super().on_created(event)
        try:
            Validation.is_file(event.src_path)
        except FileNotFoundError:
            MediaEventHandler.__LOG.debug(
                f"[SKIPPING] '{event.src_path}': not a file")
            return

        MediaEventHandler.__LOG.debug(f"[CREATED] '{event.src_path}'")

        fileout = self.__get_fileout_from(
            event.src_path, MediaEventHandler._CONFIG.media_out_folder,
            MediaEventHandler._CONFIG.media_out_format.get('format'))
        converter_object = MediaInfo(
            event.src_path,
            MediaEventHandler._CONFIG.media_in_converted_folder, fileout,
            MediaEventHandler._CONFIG.media_out_format)
        self.__executor.submit(self.__converter.execute, converter_object)
Ejemplo n.º 7
0
    def convert(self, media_info: MediaInfo) -> None:
        """
        This method uses ffmpeg and ffprobe tool, which MUST BE installed on the system
        Visit http://ffmpeg.org/ or https://ffbinaries.com/downloads to download the tool
        :param media_info: object which incapsulate infformation for strategy
        """
        FFmpeg.__LOG.debug(f"[CONVERTING] '{media_info.filein}'")

        probe = self.__converter.probe(media_info.filein)
        FFmpeg.__LOG.debug(f"[PROBING] '{media_info.filein}': {probe}")
        Validation.not_none(
            probe,
            f"Probing failed: '{media_info.filein}' is not a valid media file")

        FFmpeg.__LOG.info(f"[CONVERSION STARTED] '{media_info.filein}'")
        conversion = self.__converter.convert(media_info.filein,
                                              media_info.fileout,
                                              media_info.out_format)

        for _ in conversion:
            pass
Ejemplo n.º 8
0
    def __check_permissions(self):
        """
        Check permissions on directories before performing the operations

        :raise ValueError if input directory is equal to output directory
        :raise NotADirectoryError
        :raise PermissionError
        :raise LinksError
        """
        media_in_folder = self.__converter_config.media_in_folder
        media_out_folder = self.__converter_config.media_out_folder
        media_in_converted_folder = self.__converter_config.media_in_converted_folder

        Validation.is_dir(media_in_folder,
                          f"Missing input directory '{media_in_folder}'")
        Validation.can_read(media_in_folder,
                            f"Missing read permission on '{media_in_folder}'")
        Validation.can_write(
            media_in_folder,
            f"Missing write permission on '{media_in_folder}'")

        try:
            Validation.is_dir_writeable(
                media_out_folder,
                f"Directory '{media_out_folder}' *must* be writable")
        except NotADirectoryError:
            parent_directory = str(Path(media_out_folder).parent)
            Validation.can_write(
                parent_directory,
                f"Missing write permission on '{parent_directory}'")
            MediaObserver.__LOG.info(
                f"Creating missing destination directory for converted files '{media_out_folder}'"
            )
            # create if not exists
            Path(media_out_folder).mkdir(parents=True, exist_ok=True)

        Validation.are_symlinks(
            media_in_folder, media_out_folder,
            f"Input ('{media_in_folder}') and output ('{media_out_folder}') directory can not be the same (or symlinks)"
        )

        try:
            Validation.is_empty(media_in_converted_folder)
            Validation.is_dir_writeable(
                media_in_converted_folder,
                f"Directory '{media_in_converted_folder}' *must* be writable")
            Validation.are_symlinks(
                media_in_folder, media_in_converted_folder,
                f"Input ('{media_in_folder}') and output ('{media_in_converted_folder}') directory can not be the same (or symlinks)"
            )
        except ValueError:
            pass
        except NotADirectoryError:
            parent_directory = str(Path(media_in_converted_folder).parent)
            Validation.can_write(
                parent_directory,
                f"Missing write permission on '{parent_directory}'")
            MediaObserver.__LOG.info(
                f"Creating missing destination directory for original files converted '{media_in_converted_folder}'"
            )
            # create if not exists
            Path(media_in_converted_folder).mkdir(parents=True, exist_ok=True)

            Validation.are_symlinks(
                media_in_folder, media_in_converted_folder,
                f"Input ('{media_in_folder}') and output ('{media_in_converted_folder}') directory can not be the same (or symlinks)"
            )