コード例 #1
0
ファイル: video.py プロジェクト: esernaalonso/dev
def encode_for_streaming(video_file, scales=["full"]):
    """ Encodes the given video using ffmpeg for streaming purposes.

        Args:
            scales (list of strings, optional): Sizes to encode. Allowed values - "full", "half", "third", "quarter"
    """
    allowed_scales = {"full": 1.0, "threequarters": 0.75, "twothirds":(2.0/3.0), "half": 0.5, "third": (1.0/3.0), "quarter": 0.25}

    if video_file:
        video_size = get_video_size(video_file)
        if video_size:
            for scale in scales:
                if scale in allowed_scales:
                    new_width = int(video_size["width"]*allowed_scales[scale])
                    new_height = int(video_size["height"]*allowed_scales[scale])

                    new_width = new_width if new_width % 2 == 0 else new_width + 1
                    new_height = new_height if new_height % 2 == 0 else new_height + 1

                    new_video_file = "%s_%s.mp4" % (os.path.splitext(video_file)[0], scale)
                    ffmpeg_options = ["-i", video_file, "-c:a", "aac", "-strict", "-2", "-b:a", "128k", "-c:v", "libx264", "-profile:v", "baseline", "-filter:v", ("scale=%s:%s" % (new_width, new_height)), new_video_file]

                    result = ffmpeg.run(ffmpeg_options, exe_name="ffmpeg.exe")

                    logger.info(("Encoded video -> %s" % new_video_file), level=0)
                    logger.info(("Result: %s" % result), level=1)
        else:
            logger.error(("Error getting the original video size -> %s" % video_file), level=0)
コード例 #2
0
ファイル: pack.py プロジェクト: esernaalonso/dev
def pack_module(source_file, pack_folder=None, custom_name=None, remove_previous=True, compiled=True, standalone=True, **kwargs):
    """Packs a python file and all depedencies in and independent module folder.

    Args:
        source_file (string): Path to the file to use as root.
        pack_folder (string): The path to the folder to use as pack folder.
        custom_name (string, optional): Custom name for the packed module folder structure.
        remove_previous (bool, optional): Indicates if it has to remove the previous pack folder.
        compiled (bool, optional): Indicates if it has to compile the .py files into .pyc.
        standalone (bool, optional): Indicates if it has to be executable standalone or needs python installed.
        **kwargs: Extra arguments like custom dependencies, etc.
    """

    # Initial info.
    logger.info("Searching -> %s" % source_file)
    logger.info("Pack Folder Given -> %s" % pack_folder)

    # If source file exists, can start the packaging.
    if os.path.exists(source_file):
        logger.info(("File Exists -> %s" % source_file))

        # This will be the module name. Uses the given file name as module name if a custom name is not provided
        module_name = os.path.basename(os.path.splitext(source_file)[0]) if not custom_name else custom_name

        # pack_installer_folder = os.path.join(pack_folder, module_name)
        pack_module_folder = os.path.join(pack_folder, module_name)

        # Creates the installer
        # The real pack folder is a new folder with the name of the module in the provided pack folder.
        # pack_installer(source_file, pack_folder=pack_installer_folder, level=1, remove_previous=remove_previous)

        # Packs the file. Creates one more level folder to contain the actual application.
        # pack_module_folder = os.path.join(pack_folder, module_name, module_name)
        # pack_module_folder = os.path.join(pack_folder, module_name)
        pack_file(source_file, pack_folder=pack_module_folder, level=1, remove_previous=remove_previous, standalone=standalone, compiled=compiled)

        # Compiles the .py into .pyc and deletes the .py files if requested.
        if compiled:
            compileall.compile_dir(pack_module_folder, force=True)
            py_files = io.get_files(pack_module_folder, extensions=[".py"])
            for py_file in py_files:
                os.remove(py_file)

        # Creates the python dist in case the mode is standalone:
        if standalone:
            pack_dist(pack_folder=pack_module_folder, level=1, remove_previous=remove_previous)

        logger.info("Packaging Finished -> %s" % source_file)

    else:
        logger.error(("Source File must be provided -> %s" % source_file))
        return None
コード例 #3
0
ファイル: pack.py プロジェクト: esernaalonso/dev
def create_init_file(source_folder, **kwargs):
    """Creates a __init__.py file in the given folder.

    Args:
        source_folder (string): The folder where to create a __init__.py file in.
        **kwargs: Extra arguments to have some more options.
            level (int): Indicates the level of depth in the logs.

    Returns:
        string: Returns the path to the created __init__.py file. None if not created
    """
    # Gets the logs level values from the kwargs
    level = 0
    if "level" in kwargs: level = kwargs["level"]

    if source_folder:
        if os.path.exists(source_folder):
            init_file_path = os.path.join(source_folder, "__init__.py")
            if not os.path.exists(init_file_path):
                with open(init_file_path, 'w') as fout:
                    fout.write("")

            if os.path.exists(init_file_path):
                logger.info(("__init__.py file created -> %s" % init_file_path), level=level)
                return init_file_path
            else:
                logger.error(("__init__.py file NOT CREATED -> %s" % init_file_path), level=level)
                return None
        else:
            logger.error(("Source Folder does not exist -> %s" % source_folder), level=level)
            return None
    else:
        logger.error(("Source Folder must be provided -> %s" % source_folder), level=level)
        return None
コード例 #4
0
ファイル: pack.py プロジェクト: esernaalonso/dev
def setup_file_pack_folder(source_file, remove_previous=False, **kwargs):
    """Creates a pack folder to include all pack files.

    Args:
        source_file (string): The path to the file that will be in the folder to use as pack folder.
        remove_previous (bool, optional): Indicates if it has to remove the previous pack folder.
        **kwargs: Extra arguments to have some more options.
            level (int): Indicates the level of depth in the logs.

    Returns:
        string: The pack folder path if created, None if not.
    """

    # Gets the logs level values from the kwargs
    level = 0
    if "level" in kwargs: level = kwargs["level"]

    if source_file:
        pack_folder = os.path.dirname(source_file)

        # if a clean pack is needed, the old one is deleted.
        if os.path.exists(pack_folder) and remove_previous:
            logger.info(("Removing Current Package Folder -> %s" % pack_folder), level=level)
            shutil.rmtree(pack_folder)

        # if the pack_folder doesn't exist, is created.
        if not os.path.exists(pack_folder):
            logger.info(("Creating Package Folder-> %s" % pack_folder), level=level)
            os.makedirs(pack_folder)

        # If the pack_folder is created OK, creates the lib folder.
        if os.path.exists(pack_folder):
            logger.info(("Package Folder Created -> %s" % pack_folder), level=level)

            # Also creates an __init__ file
            pack_folder_init = create_init_file(pack_folder, level=level+1)
            if not pack_folder_init:
                logger.error(("__init__.py file in Pack Folder NOT CREATED in -> %s" % pack_folder), level=level)
                return None

            # Finally returns the pack folder created.
            return pack_folder
        else:
            logger.error(("Pack Folder NOT CREATED -> %s" % pack_folder), level=level)
            return None
    else:
        logger.error(("Pack Folder source file must be provided -> %s" % source_file), level=level)
        return None
コード例 #5
0
ファイル: pack.py プロジェクト: esernaalonso/dev
def pack_file(source_file, pack_folder=None, recursive=True, **kwargs):
    """Summary

    Args:
        source_file (string): Path of the file to pack.
        pack_folder (string, optional): Destination Pack Folder.
        recursive (bool, optional): Indicates if pack all dependencies. Default True.
        **kwargs: Extra arguments to have some more options.
            level (int): Indicates the level of depth in the logs.
            packaging_mode (string): Indicates the packaging mode for the file. To do special operations.
            packaging_type (string): Indicates the type of packaging for the file.
            remove_previous (bool): Indicates if removes previous packaging.

    Returns:
        string: The path to the created file. None if not created.
    """

    # Some configurations
    remove_previous_packaging_modes = ["root"]
    explorable_packaging_types = ["lib"]
    explorable_file_types = [".py"]
    packable_import_types = ["custom_module"]

    # Gets the logs level values from the kwargs
    level = 0
    if "level" in kwargs: level = kwargs["level"]

    packaging_mode = "root"
    if "packaging_mode" in kwargs: packaging_mode = kwargs["packaging_mode"]

    packaging_type = "lib"
    if "packaging_type" in kwargs: packaging_type = kwargs["packaging_type"]

    remove_previous = False
    if "remove_previous" in kwargs: remove_previous = kwargs["remove_previous"]

    compiled = True
    if "compiled" in kwargs: compiled = kwargs["compiled"]

    standalone = True
    if "standalone" in kwargs: standalone = kwargs["standalone"]

    # If source file does not exist, cannot start the packaging.
    if not os.path.exists(source_file):
        logger.error(("File to Pack does not exist -> %s" % source_file), level=level)
        return None

    # Shows info about the file to package.
    logger.info(("Packaging File -> %s" % source_file), level=level)

    # This should be the destination path folder
    # If is the root one, needs to add the subfolder with tool name.
    # If not, the packaging type should be used as subfolder name. If already is in that level, does not add it again.
    package_sub_folder = ""
    if packaging_mode == "root":
        # package_sub_folder = os.path.basename(os.path.splitext(source_file)[0])
        package_sub_folder = ""
    elif os.path.basename(os.path.normpath(pack_folder)) != packaging_type:
        package_sub_folder = packaging_type
        if not package_sub_folder.startswith("lib") and os.path.basename(os.path.normpath(pack_folder)) != "lib":
            package_sub_folder = os.path.join("lib", package_sub_folder)
    dest_folder = os.path.join(pack_folder, package_sub_folder)
    logger.info(("Destination Folder -> %s" % dest_folder), level=level)

    # This should be the destination path file
    dest_file = os.path.join(dest_folder, os.path.basename(source_file))
    logger.info(("Destination File -> %s" % dest_file), level=level)

    # If has to remove the previous and it exists, mark it
    remove_previous_folder = remove_previous and packaging_mode in remove_previous_packaging_modes

    # If dest pack folder does not exist, cannot start the packaging. Tries to create it.
    setup_file_pack_folder(dest_file, remove_previous=remove_previous_folder, level=level+1)

    # Gets the destination file type.
    file_type = os.path.splitext(dest_file)[1]
    logger.info(("File Type -> %s" % dest_file), level=level)

    # Prints the type of packaging.
    logger.info(("Packaging Type -> %s" % packaging_type), level=level)
    shutil.copyfile(source_file, dest_file)

    if os.path.exists(dest_file):
        logger.info(("File Packaged -> %s" % dest_file), level=level)

        # If the packaging_mode is root, need to create the execute.bat
        if packaging_mode == "root":
            execute_bat_file = os.path.join(os.path.dirname(dest_file), "execute.bat")
            create_execute_bat(execute_bat_file, os.path.basename(dest_file), compiled=compiled, standalone=standalone, remove_previous=True, level=level+1)

        # In this case is explorable. Can contain imports, ui dependencies, etc.
        if packaging_type in explorable_packaging_types and file_type in explorable_file_types:
            logger.info(("File can be recursive explored -> %s" % source_file), level=level)

            logger.info(("Searching source file imports -> %s" % source_file), level=level)
            imports_info = inspector.get_file_imports_info(source_file)

            # If it has imports info, packages the imports as libraries
            if imports_info:
                import_relative_path = "lib" if packaging_mode == "root" else ""

                for import_info in imports_info:
                    if import_info["type"] in packable_import_types:
                        # Packages the import source as a library
                        logger.info(("Packaging lib file -> %s" % import_info["path"]), level=level)
                        pack_file(import_info["path"], pack_folder=dest_folder, level=level+1, packaging_type="lib", packaging_mode="regular")

                        # After packaging the lib, must replace in dest_file the import for the new one.
                        import_statement = inspector.build_import_statement(import_info, force_relative=True, relative_path=import_relative_path)
                        logger.info(("Replacing import -> %s <- with -> %s" % (import_info["source"], import_statement)), level=level)
                        io.replace_line_in_file(dest_file, import_info["source"], import_statement, keep_old_commented=True)

            # Search dependencies like ui files and package them.
            logger.info(("Searching source UI dependencies -> %s" % source_file), level=level)
            ui_search_folder = os.path.dirname(source_file)
            ui_files = inspector.get_file_ui_dependencies(source_file)

            # If it has ui files, packages the files as uis
            if ui_files:
                for ui_file in ui_files:
                    # Prints the type of packaging.
                    logger.info(("Packaging UI dependency -> %s" % ui_file), level=level)
                    pack_file(ui_file, pack_folder=dest_folder, level=level+1, packaging_type="uis", packaging_mode="regular")

            # Search dependencies like png and jpg files and package them.
            logger.info(("Searching source Image dependencies -> %s" % source_file), level=level)
            image_search_folder = os.path.dirname(source_file)
            image_files = inspector.get_file_image_dependencies(source_file)

            # If it has image files, packages the files as images
            if image_files:
                for image_file in image_files:
                    # Prints the type of packaging.
                    logger.info(("Packaging Image dependency -> %s" % image_file), level=level)
                    pack_file(image_file, pack_folder=dest_folder, level=level+1, packaging_type="images", packaging_mode="regular")

            # Search dependencies like qss files and package them.
            logger.info(("Searching source QSS dependencies -> %s" % source_file), level=level)
            qss_search_folder = os.path.dirname(source_file)
            qss_files = inspector.get_file_qss_dependencies(source_file)

            # If it has qss files, packages the files as themes
            if qss_files:
                for qss_file in qss_files:
                    # Prints the qss dependency.
                    logger.info(("Packaging QSS dependency -> %s" % qss_file), level=level)
                    # pack_file(qss_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "styles"), packaging_mode="regular")
                    pack_file(qss_file, pack_folder=dest_folder, level=level+1, packaging_type="styles", packaging_mode="regular")

            # Search dependencies like font .ttf files and package them.
            logger.info(("Searching source font TTF dependencies -> %s" % source_file), level=level)
            font_search_folder = os.path.dirname(source_file)
            font_files = inspector.get_file_font_dependencies(source_file)

            # If it has ttf files, packages the files as fonts
            if font_files:
                for font_file in font_files:
                    # Prints the font dependency.
                    logger.info(("Packaging TTF dependency -> %s" % font_file), level=level)
                    # pack_file(font_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "fonts"), packaging_mode="regular")
                    pack_file(font_file, pack_folder=dest_folder, level=level+1, packaging_type="fonts", packaging_mode="regular")

            # Search dependencies like qss files and package them.
            logger.info(("Searching source EXE dependencies -> %s" % source_file), level=level)
            exe_search_folder = os.path.dirname(source_file)
            exe_files = inspector.get_file_exe_dependencies(source_file)

            # If it has exe files, packages the files as exes
            if exe_files:
                for exe_file in exe_files:
                    # Prints the exe dependency.
                    logger.info(("Packaging EXE dependency -> %s" % exe_file), level=level)
                    # pack_file(exe_file, pack_folder=dest_folder, level=level+1, packaging_type=os.path.join("lib", "exes"), packaging_mode="regular")
                    pack_file(exe_file, pack_folder=dest_folder, level=level+1, packaging_type="exes", packaging_mode="regular")

        else:
            logger.info(("Packaging/File Type non explorable. Direct Copy to -> %s" % dest_file), level=level)

        # Finally returns the packaged file path.
        return dest_file
    else:
        logger.error(("File could not be packaged -> %s" % dest_file), level=level)
        return None