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 a WMV media file as input and converts it to H.265/HEVC video and AAC audio in MP4 container.
Create the Visual Studio project¶
Create a C++, Console App in Visual Studio. Name the project
simple-converter. CheckPlace solution and project in the same directory.Download the 64 bit version of AVBlocks for C++ (Windows). The file you need will have a name similar to
avblocks-v3.4.0-demo.1-windows.zip- the version number may differ.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" Invoke-WebRequest ` -Uri "${BaseUrl}/${Version}/avblocks-${Version}-windows.zip" ` -OutFile "avblocks-${Version}-windows.zip"
Extract the ZIP archive in a location of your choice, then copy the
includeandlibdirectories to theavblockssubdirectory of the Visual Studio solution directory. The Visual Studio solution is the directory that contains thesimple-converter.slnsolution file.If you downloaded the archive to the Visual Studio solution directory with PowerShell, you can extract it directly into the
avblockssubdirectory:Expand-Archive ` -Path "avblocks-${Version}-windows.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
Replace the contents of
simple-converter.cppwith this code:#include <primo/avblocks/avb.h> #include <primo/platform/reference++.h> #include <primo/platform/ustring.h> #include <windows.h> // link with AVBlocks64.lib #pragma comment(lib, "./avblocks/lib/x64/AVBlocks64.lib") using namespace primo; using namespace primo::codecs; using namespace primo::avblocks; int wmain(int argc, wchar_t* argv[]) { // needed for WMV CoInitializeEx(nullptr, COINITBASE_MULTITHREADED); Library::initialize(); auto inputFile = primo::ustring(L"Wildlife.wmv"); auto outputFile = primo::ustring(L"Wildlife_h265_aac.mp4"); auto inputInfo = primo::make_ref(Library::createMediaInfo()); inputInfo->inputs()->at(0)->setFile(inputFile); if (inputInfo->open()) { auto inputSocket = primo::make_ref( Library::createMediaSocket(inputInfo.get()) ); // Start with MP4 / H.264 + AAC preset, then change video to H.265 auto outputSocket = primo::make_ref( Library::createMediaSocket(Preset::Video::Generic::MP4::Base_H264_AAC) ); // Get the output video stream info and modify it for H.265/HEVC auto outVideoStream = static_cast<VideoStreamInfo*>(outputSocket ->pins()->at(0) ->streamInfo()); outVideoStream->setStreamType(StreamType::H265); outVideoStream->setStreamSubType(StreamSubType::HEVC_Annex_B); // With H.265/HEVC we can use lower bitrate, e.g. 500 kbps outVideoStream->setBitrate(500'000); // Change the output file name outputSocket->setFile(outputFile); // Create Transcoder and configure it with // the input and output sockets auto transcoder = primo::make_ref(Library::createTranscoder()); transcoder->inputs()->add(inputSocket.get()); transcoder->outputs()->add(outputSocket.get()); // Allow demo mode for the transcoder when // using the demo version of the library transcoder->setAllowDemoMode(true); // Run the transcoder if (transcoder->open()) { transcoder->run(); transcoder->close(); } else { std::wcerr << L"transcoder->open() failed: " << primo::ustring(transcoder->error()->message()) << std::endl; } } Library::shutdown(); CoUninitialize(); return 0; }
In Visual Studio, select
Build | Configuration Managerfrom the menu, then select thex64platform to the solution platforms.In Visual Studio, select
Project | simple-converter Propertiesfrom the menu, thenC++ | General, then add./avblocks/includetoAdditional Include Directories.Build the project (Ctrl + Shift + B).
Copy the file
AVBlocks64.dllfromavblocks/lib/x64tox64/Debug.
Run the application¶
Download the
Wildlife.wmvsample file and save it in the Visual Studio solution directory (next to thesimple-converter.slnsolution file):Invoke-WebRequest ` -Uri https://archive.org/download/WildlifeSampleVideo/Wildlife.wmv ` -OutFile Wildlife.wmv
Copy the file
AVBlocks64.dllfromavblocks/lib/x64tox64/Debug.Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file
Wildlife_h265_aac.mp4will 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 fileAVBlocks64.dllfromavblocks/lib/x64tox64/Debug.transcoder->open()may fail if there is already a fileWildlife_h265_aac.mp4in the project directory. DeleteWildlife_h265_aac.mp4to solve that.