Developing with Visual Studio Code

This topic describes the steps needed to setup Visual Studio Code for AVBlocks Python development on Ubuntu. These steps have been verified to work on Ubuntu 22.04.3 LTS.

Python

AVBlocks for Python requires Python 3.12 or higher. We use uv to install and manage Python and the project’s virtual environment. The Python you use must match the architecture of the AVBlocks Core build you download (x64 or arm64).

sudo apt-get install libjpeg8 libtiff5 libpng16-16 libtbb2
curl -LsSf https://astral.sh/uv/install.sh | sh

Visual Studio Code

Download and install from Visual Studio Code site.

Open Visual Studio Code and press Ctrl + Shift + P. Select Shell Command: Install 'code' command in PATH.

Also install the Python extension.

Close Visual Studio Code.

Create the Python project

mkdir -p ~/avblocks/py/simple-converter
cd ~/avblocks/py/simple-converter

# pin the Python version and create the project
uv init --no-workspace --python 3.12

# create the virtual environment
uv venv

# add the AVBlocks Python package
uv add git+https://github.com/avblocks/avblocks-py.git --tag 3.4.0.2

Open the project in Visual Studio Code:

code .

Select the interpreter

Press Ctrl + Shift + P and select Python: Select Interpreter. Choose the interpreter from the project’s .venv directory.

Setup Debugging

Add the following Visual Studio Code specific file to the .vscode subdir:

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: simple_converter.py",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/simple_converter.py",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "env": {
                "AVBLOCKS_CORE_PATH": "${workspaceFolder}/sdk/lib/x64"
            }
        }
    ]
}

Add AVBlocks Core

Follow the Installation steps to download AVBlocks Core for Ubuntu into the sdk directory of the project, then point AVBLOCKS_CORE_PATH to the library directory that matches your machine (use arm64 in place of x64 on ARM64 machines):

export AVBLOCKS_CORE_PATH=$PWD/sdk/lib/x64

The native library file you need is libAVBlocks64.so.

Update for AVBlocks Python

  1. Replace the contents of main.py and rename it to simple_converter.py, or create a new file simple_converter.py with this code:

    import sys
    
    from avblocks import (
        Library, MediaInfo, MediaSocket, Transcoder,
        StreamType, StreamSubType
    )
    
    
    def main():
        Library.initialize()
    
        input_file = "Wildlife_h264_aac.mp4"
        output_file = "Wildlife_h265_aac.mp4"
    
        with MediaInfo() as input_info:
            input_info.inputs[0].file = input_file
    
            if input_info.open():
                input_socket = MediaSocket.from_media_info(input_info)
    
                # Start with same output as the input, which is MP4 / H.264 + AAC
                output_socket = input_socket.clone()
    
                # Change the video stream type to H.265 (HEVC)
                out_video_stream = output_socket.pins[0].stream_info
                out_video_stream.stream_type = StreamType.H265
                out_video_stream.stream_sub_type = StreamSubType.HEVC_Annex_B
                out_video_stream.bitrate = 500000
    
                output_socket.file = output_file
    
                with Transcoder() as transcoder:
                    transcoder.inputs.add(input_socket)
                    transcoder.outputs.add(output_socket)
    
                    # Allow demo mode when using the demo version of the library
                    transcoder.allow_demo_mode = True
    
                    if transcoder.open():
                        transcoder.run()
                        transcoder.close()
                    else:
                        print(f"transcoder.open() failed: {transcoder.error.message}", file=sys.stderr)
            else:
                print(f"input_info.open() failed: {input_info.error.message}", file=sys.stderr)
    
        Library.shutdown()
        return 0
    
    
    if __name__ == '__main__':
        sys.exit(main())
    

Run the application

  1. Download the Wildlife.mp4 HD movie from the Internet Archive and save it in the project directory as Wildlife_h264_aac.mp4:

    curl -L -o Wildlife_h264_aac.mp4 https://archive.org/download/WildlifeSampleVideo/Wildlife.mp4
    
  2. Run the application from the terminal or press F5 in Visual Studio Code. Wait a few seconds for the Transcoder to finish. The converted file Wildlife_h265_aac.mp4 will be in the project directory.

    uv run python simple_converter.py
    

Troubleshooting

  • You may get an error AVBlocks library not found. Please set AVBLOCKS_CORE_PATH .... To fix that, set the AVBLOCKS_CORE_PATH environment variable to the directory that contains libAVBlocks64.so.

  • Transcoder.open may fail if there is already a file Wildlife_h265_aac.mp4 in the project directory. Delete Wildlife_h265_aac.mp4 to solve that.

  • The library may fail to load with an architecture mismatch. The AVBlocks Core build does not match the architecture of your Python. Run python -c "import platform; print(platform.machine())" and use the matching x64 or arm64 build.