Beispiel #1
0
    def format_args(self, args):
        """Check arguments and format them to be compatible with `self.config`."""
        args = {key: value for key, value in args.items() if value}
        for key, value in args.items():
            if key == 'directory':
                self.directory = value
            # Encoding
            if key == 'crf':
                self.config['encoding']['crf'] = str(value)
            elif key == 'preset':
                self.config['encoding']['preset'] = value
            elif key == 'audio':
                self.config['encoding']['audio'] = value
            elif key == 'audio':
                self.config['encoding']['kbitrate-audio'] = value
            elif key == 'threads':
                self.config['encoding']['threads'] = str(value)
            elif key == 'size':
                self.config['default']['min-size'] = str(
                    humanfriendly.parse_size(value))

        # Default if no dir is specified
        if not self.directory:
            self.directory = '.'

        # Get absolute path of directory
        self.directory = os.path.abspath(self.directory)
        Logger.info(f'Searching for files in directory {self.directory}')

        # Check if path is a dir
        if not os.path.isdir(self.directory):
            Logger.warning('A valid directory needs to be specified')
            Logger.warning(self.directory)
            sys.exit(1)
Beispiel #2
0
    def validate_encoded_file(self, task):
        """Validate that the encoded file is not malformed."""
        if os.path.exists(task.temp_path):
            Logger.info("Pueue task completed:")
            Logger.info(task.origin_file)
            # Check if the duration of both movies differs.
            copy, delete = check_duration(task.origin_path,
                                          task.temp_path,
                                          seconds=1)

            # Check if the filesize of the x.265 encoded object is bigger
            # than the original.
            if copy:
                copy, delete = check_file_size(task.origin_path,
                                               task.temp_path)

            # Only copy if checks above passed
            if copy:
                # Save new path, size, sha1 and mark as encoded
                task.movie.sha1 = get_sha1(task.temp_path)
                task.movie.size = os.path.getsize(task.temp_path)
                task.movie.encoded = True
                task.movie.name = os.path.basename(task.target_path)

                self.session.add(task.movie)
                self.session.commit()

                # Get original file permissions
                stat = os.stat(task.origin_path)

                # Remove the old file and copy the new one to the proper directory.
                os.remove(task.origin_path)
                os.rename(task.temp_path, task.target_path)
                # Set original file permissions on new file.
                os.chmod(task.target_path, stat.st_mode)
                try:
                    os.chown(task.target_path, stat.st_uid, stat.st_gid)
                except PermissionError:
                    Logger.info("Failed to set ownership for {0}".format(
                        task.target_path))
                    pass
                self.processed_files += 1
                Logger.info("New encoded file is now in place")
            elif delete:
                # Mark as failed and save
                task.movie.failed = True
                self.session.add(task.movie)
                self.session.commit()

                os.remove(task.temp_path)
                Logger.warning("Didn't copy new file, see message above")
        else:
            Logger.error("Pueue task failed in some kind of way.")