Developing with Visual Studio Code¶
This topic describes the steps needed to setup Visual Studio Code for AVBlocks Python development on macOS. These steps have been verified to work on macOS Sequoia 15.6.
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).
xcode-select --install
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 Cmd + 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 Cmd + 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/arm64"
}
}
]
}
Add AVBlocks Core¶
Follow the Installation steps to download AVBlocks Core for macOS 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/arm64
The native library file you need is libAVBlocks.dylib.
Update for AVBlocks Python¶
Replace the contents of
main.pyand rename it tosimple_converter.py, or create a new filesimple_converter.pywith 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¶
Download the
Wildlife.mp4HD movie from the Internet Archive and save it in the project directory asWildlife_h264_aac.mp4:curl -L -o Wildlife_h264_aac.mp4 https://archive.org/download/WildlifeSampleVideo/Wildlife.mp4
Run the application from the terminal or press
F5in Visual Studio Code. Wait a few seconds for the Transcoder to finish. The converted fileWildlife_h265_aac.mp4will 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 theAVBLOCKS_CORE_PATHenvironment variable to the directory that containslibAVBlocks.dylib.Transcoder.openmay fail if there is already a fileWildlife_h265_aac.mp4in the project directory. DeleteWildlife_h265_aac.mp4to 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 matchingx64orarm64build.