Create a C# Console App in Visual Studio for Mac¶
This topic describes the steps needed to configure a C# console application in Visual Studio for Mac. These steps have been verified to work with Visual Studio for Mac 2022, on macOS Ventura 13.5.2.
Create the Visual Studio project¶
Create a C# console application in Visual Studio.
Choose
.NET 10.0for Target FrameworkName the project
simple-converter.Leave
Put project file in a subfolderunchecked
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.
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.Unzip in a location of your choice, then copy the file
AVBlocks.clrcore.x64.dllandlibAVBlocks.dylibto the project’s directory.Switch to the project in Visual Studio and add a reference to
AVBlocks.clrcore.x64.dll. To do this right click thesimple-converterproject, selectAdd | Reference | .NET Assembly | Browsethen selectAVBlocks.clrcore.x64.dll.Add the
libAVBlocks.dylibto your project simply as a file, open file properties, and set the ‘Copy to Output Directory’ to ‘Copy if newer’, so it is copied to the output directory automatically. TheAVBlocks.clrcore.x64.dllneedslibAVBlocks.dyliband will not work without it.Build the project (Cmd + K)
arm64¶
For Apple Silicon Macs (M1 and later) running an arm64 .NET.
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.Unzip in a location of your choice, then copy the file
AVBlocks.clrcore.arm64.dllandlibAVBlocks.dylibto the project’s directory.Switch to the project in Visual Studio and add a reference to
AVBlocks.clrcore.arm64.dll. To do this right click thesimple-converterproject, selectAdd | Reference | .NET Assembly | Browsethen selectAVBlocks.clrcore.arm64.dll.Add the
libAVBlocks.dylibto your project simply as a file, open file properties, and set the ‘Copy to Output Directory’ to ‘Copy if newer’, so it is copied to the output directory automatically. TheAVBlocks.clrcore.arm64.dllneedslibAVBlocks.dyliband will not work without it.Build the project (Cmd + K)
Run the application¶
Download the
AAP.m4vHD movie from the Internet Archive and and save it inbin/Debug/net10.0under the project’s directory.Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file
AAP.mp4will be in thebin/Debug/net10.0directory.
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 filelibAVBlocks.dylibtobin/Debug/net10.0under the project’s directory.Transcoder.Open may fail if there is already a file
AAP.mp4in thebin/Debug/net10.0directory. DeleteAAP.mp4to 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. Rundotnet --info, check theArchitectureline, and use the matchingx64orarm64package andAVBlocks.clrcore.<arch>.dll.