Create a Console App in Visual Studio on Windows

This topic describes the steps needed to configure a C++ Console App in Visual Studio. These steps have been verified to work with Visual Studio 2022 Community Edition, on Windows 11.

The code snippets used in this article are from the simple_converter Windows sample. This sample takes H.264/AVC video and AAC audio in MP4 container as input and converts them to H.265/HEVC video and AAC audio in MP4 container.

Create the Visual Studio project

  1. Create a C++, Console App in Visual Studio. Name the project simple-converter. Check Place solution and project in the same directory.

  2. Download the 64 bit version of AVBlocks Core (Windows) that matches your machine. Check $env:PROCESSOR_ARCHITECTURE: AMD64 means x64, ARM64 means ARM64. The file you need will have a name similar to the following - the version number may differ:

    Architecture

    File

    x64 (AMD64)

    avblocks-v3.4.0-demo.1-windows.zip

    ARM64

    avblocks-v3.4.0-demo.1-windows-arm64.zip

    You can also download it with PowerShell from the Visual Studio solution directory:

    $BaseUrl = "https://github.com/avblocks/avblocks-core/releases/download"
    $Version = "v3.4.0-demo.1"
    
    # "windows" for x64 (AMD64), "windows-arm64" for ARM64
    $Platform = "windows"
    
    Invoke-WebRequest `
        -Uri "${BaseUrl}/${Version}/avblocks-${Version}-${Platform}.zip" `
        -OutFile "avblocks-${Version}-${Platform}.zip"
    
  3. Extract the ZIP archive in a location of your choice, then copy the include and lib directories to the avblocks subdirectory of the Visual Studio solution directory. The Visual Studio solution is the directory that contains the simple-converter.sln solution file. AVBlocks Plus also needs the header file avb++.h. Download it from the avblocks-plus repository and save it in the avblocks/include/primo/avblocks directory.

    If you downloaded the archive to the Visual Studio solution directory with PowerShell, you can extract it directly into the avblocks subdirectory:

    Expand-Archive `
        -Path "avblocks-${Version}-${Platform}.zip" `
        -DestinationPath "./avblocks" -Force
    

    You should end up with a directory structure similar to the following:

    simple-converter
    ├───avblocks
    │   ├───include
    │   └───lib
    ├───simple-converter.cpp
    ├───simple-converter.sln
    └───simple-converter.vcxproj
    
  4. Replace the contents of simple-converter.cpp with this code:

    #include <primo/avblocks/avb++.h>
    
    #include <cstdio>
    #include <iostream>
    
    // link with AVBlocks64.lib
    #if defined(_M_ARM64)
    #pragma comment(lib, "./avblocks/lib/arm64/AVBlocks64.lib")
    #else
    #pragma comment(lib, "./avblocks/lib/x64/AVBlocks64.lib")
    #endif
    
    using namespace primo::codecs;
    using namespace primo::avblocks::modern;
    
    int wmain()
    {
        TLibrary library;
    
        const wchar_t* inputFile  = L"Wildlife_h264_aac.mp4";
        const wchar_t* outputFile = L"Wildlife_h265_aac.mp4";
    
        // transcoder will fail if output exists (by design)
        _wremove(outputFile);
    
        try {
            // Probe the input file and create the input socket from the probe
            auto input = TMediaSocketW(TMediaInfoW().file(inputFile).open());
    
            // Demo mode is allowed when using the demo version of the library.
            TTranscoderW()
                .allowDemoMode(true)
                .addInput(input)
                // The output starts as a copy of the input
                .addOutput(
                    input.clone()
                        .file(outputFile)
                        .videoInfoRef(0, [](TVideoStreamInfo& video) {
                            // Change the output video stream to H.265/HEVC
                            // Input is H.264/AVC at 700 kbps
                            // With H.265/HEVC we can use lower bitrate, e.g. 500 kbps
                            video.streamType(StreamType::H265)
                                 .streamSubType(StreamSubType::HEVC_Annex_B)
                                 .bitrate(500'000);
                        })
                )
                .transcode();
    
            std::wcout << L"Output: " << outputFile << std::endl;
            return 0;
    
        } catch (const TAVBlocksException& ex) {
            std::wcerr << L"AVBlocks error: " << ex.what() << std::endl;
            return 1;
        }
    }
    
  5. In Visual Studio, select Build | Configuration Manager from the menu, then select the platform that matches the AVBlocks build you downloaded (x64 or ARM64) from the solution platforms. If it is not in the list, choose <New...> and create it from the existing platform.

  6. In Visual Studio, select Project | simple-converter Properties from the menu, then C++ | General, then add ./avblocks/include to Additional Include Directories.

  7. Build the project (Ctrl + Shift + B).

  8. Copy the file AVBlocks64.dll from avblocks/lib/x64 to x64/Debug (on ARM64: from avblocks/lib/arm64 to ARM64/Debug).

Run the application

  1. Download the Wildlife_h264_aac.mp4 sample file and save it in the Visual Studio solution directory (next to the simple-converter.sln solution file):

    Invoke-WebRequest `
        -Uri https://archive.org/download/WildlifeSampleVideo/Wildlife.mp4 `
        -OutFile Wildlife_h264_aac.mp4
    
  2. Copy the file AVBlocks64.dll from avblocks/lib/x64 to x64/Debug (on ARM64: from avblocks/lib/arm64 to ARM64/Debug).

  3. Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file Wildlife_h265_aac.mp4 will be in the solution directory.

Troubleshooting

  • You may get The program can't start because AVBlocks64.dll is missing from your computer. Try reinstalling the program to fix this problem. or a similar message. To fix that, copy the file AVBlocks64.dll from avblocks/lib/x64 to x64/Debug (on ARM64: from avblocks/lib/arm64 to ARM64/Debug).

  • TTranscoder::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.