Developing with Visual Studio Code

This topic describes the steps needed to setup Visual Studio Code for AVBlocks .NET development on macOS. These steps have been verified to work on macOS Ventura 13.5.2.

.NET

If you don’t have .NET installed follow the steps in the Setup .NET development environment on macOS post to configure a .NET development environment. Install the build that matches your Mac:

x64

For Intel Macs.

curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 10.0 --architecture x64 --install-dir ~/.dotnet/x64
export PATH="$HOME/.dotnet/x64:$PATH"

arm64

For Apple Silicon Macs (M1 and later).

curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 10.0 --architecture arm64 --install-dir ~/.dotnet/arm64
export PATH="$HOME/.dotnet/arm64:$PATH"

Check the architecture with dotnet --info | grep -i architecture.

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 C# Dev Kit extension.

Close Visual Studio Code.

Create the .NET project

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

# needed so that we can run the dotnet CLI
export PATH="$HOME/.dotnet:$PATH"

# We use 10.0 here but give the commands for other versions
# see: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json
dotnet new globaljson --sdk-version 10.0.102 --roll-forward latestPatch
# dotnet new globaljson --sdk-version 9.0.308 --roll-forward latestPatch
# dotnet new globaljson --sdk-version 8.0.416 --roll-forward latestPatch
# dotnet new globaljson --sdk-version 6.0.428 --roll-forward latestPatch

# create new console application and project
dotnet new console --framework net10.0 --use-program-main 

# create new solution and add the project to it
dotnet new sln
dotnet sln add simple-converter.csproj

# add .gitignore
dotnet new gitignore

Automate the build

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

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "dotnet",
            "task": "build",
            "group": "build",
            "problemMatcher": [],
            "label": "dotnet: build"
        }
    ]
}

Setup Debugging

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

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "dotnet: build",
            "program": "${workspaceFolder}/bin/Debug/net10.0/simple-converter.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        }
    ]
}

Test the build

Test the build by pressing Cmd + B. Visual Studio Code should execute the dotnet build command automatically.

Test the debugging

Set a breakpoint on the first line of static void Main(string[] args) inside Program.cs. Press F5 to launch the debugger. It should stop at the breakpoint.

Update for AVBlocks .NET

  1. Replace the contents of Program.cs with this code:

    using PrimoSoftware.AVBlocks;
    
    namespace SimpleConverter
    {
        class Program
        {
            static void Main(string[] args)
            {
                Library.Initialize();
    
                var inputInfo = new MediaInfo();
    
                inputInfo.Inputs[0].File = "AAP.m4v";
                if (inputInfo.Open())
                {
                    var inputSocket = MediaSocket.FromMediaInfo(inputInfo);
                    var outputSocket = MediaSocket.FromPreset(Preset.Video.Generic.MP4.Base_H264_AAC);
                    outputSocket.File = "AAP.mp4";
    
                    using (var transcoder = new Transcoder())
                    {
                        transcoder.Inputs.Add(inputSocket);
                        transcoder.Outputs.Add(outputSocket);
    
                        if (transcoder.Open())
                        {
                            transcoder.Run();
                            transcoder.Close();
                        }
                    }
                }
    
                Library.Shutdown();
            }
        }
    }
    

Add AVBlocks .NET

Follow the section that matches the architecture of your .NET installation (run dotnet --info and check the Architecture line).

x64

For Intel Macs, or an x64 .NET running under Rosetta.

  1. Download the Darwin version of AVBlocks for .NET (net10.0). The file you need will have a name similar to avblocks-net10.0-v3.4.0-demo.1-darwin.zip - the version number may differ.

  2. Unzip in a location of your choice, then copy the file AVBlocks.clrcore.x64.dll and libAVBlocks.dylib to the project’s directory.

  3. Add a reference to AVBlocks.clrcore.x64.dll

    Replace the contents of the simple-converter.csproj project file with this code:

    <Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net10.0</TargetFramework>
        <RootNamespace>SimpleConverter</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    
    <!-- This adds a reference to AVBlocks .NET dll-->
    <ItemGroup>
        <Reference Include="AVBlocks.clrcore.x64">
        <HintPath>AVBlocks.clrcore.x64.dll</HintPath>
        </Reference>  
    </ItemGroup>
    
    </Project>
    

arm64

For Apple Silicon Macs (M1 and later) running an arm64 .NET.

  1. Download the Darwin version of AVBlocks for .NET (net10.0). The file you need will have a name similar to avblocks-net10.0-v3.4.0-demo.1-darwin-arm64.zip - the version number may differ.

  2. Unzip in a location of your choice, then copy the file AVBlocks.clrcore.arm64.dll and libAVBlocks.dylib to the project’s directory.

  3. Add a reference to AVBlocks.clrcore.arm64.dll

    Replace the contents of the simple-converter.csproj project file with this code:

    <Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net10.0</TargetFramework>
        <RootNamespace>SimpleConverter</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    
    <!-- This adds a reference to AVBlocks .NET dll-->
    <ItemGroup>
        <Reference Include="AVBlocks.clrcore.arm64">
        <HintPath>AVBlocks.clrcore.arm64.dll</HintPath>
        </Reference>  
    </ItemGroup>
    
    </Project>
    

Run the application

  1. Download the AAP.m4v HD movie from the Internet Archive and and save it in the project directory.

  2. Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file AAP.mp4 will be in the project directory.

Troubleshooting

  • You may get a 'DllNotFoundException' exception: Unable to load DLL 'libAVBlocks.dylib': The specified module could not be found. To fix that, copy the file libAVBlocks.dylib to the project directory.

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

  • You may get a BadImageFormatException (or the library fails to load with an architecture mismatch). The AVBlocks package does not match the architecture of your .NET. Run dotnet --info, check the Architecture line, and use the matching x64 or arm64 package and AVBlocks.clrcore.<arch>.dll.