是否有可实现C++ DeckLink输出的最简可运行示例?
DeckLink 输出单一纯色最简C++代码示例
前置要求
- 已安装DeckLink官方SDK,编译时需配置SDK的头文件和库路径
- 代码默认输出1080p25格式、RGB纯红色块,可通过修改宏定义调整色值、分辨率、输出帧率参数
完整代码
#include <iostream> #include <DeckLinkAPI.h> // 可自定义参数 #define kDisplayMode bmdModeHD1080p25 #define kPixelFormat bmdFormat8BitARGB #define kRed 0xFF // 红色分量 0~255 #define kGreen 0x00 // 绿色分量 0~255 #define kBlue 0x00 // 蓝色分量 0~255 #define kWidth 1920 #define kHeight 1080 int main() { // Windows平台初始化COM #if defined(_WIN32) CoInitialize(nullptr); #endif IDeckLink* deckLink = nullptr; IDeckLinkOutput* deckLinkOutput = nullptr; IDeckLinkDisplayMode* displayMode = nullptr; IDeckLinkMutableVideoFrame* frame = nullptr; // 枚举获取第一个DeckLink设备 IDeckLinkIterator* iterator = CreateDeckLinkIteratorInstance(); if (!iterator || iterator->Next(&deckLink) != S_OK) { std::cerr << "未检测到DeckLink设备" << std::endl; return 1; } iterator->Release(); // 获取设备输出接口 if (deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&deckLinkOutput) != S_OK) { std::cerr << "当前设备不支持输出功能" << std::endl; return 1; } // 配置输出模式 if (deckLinkOutput->GetDisplayMode(kDisplayMode, &displayMode) != S_OK) { std::cerr << "设备不支持指定的输出分辨率/帧率" << std::endl; return 1; } if (deckLinkOutput->EnableVideoOutput(kDisplayMode, bmdVideoOutputFlagDefault) != S_OK) { std::cerr << "启用视频输出失败" << std::endl; return 1; } // 创建视频帧对象 if (deckLinkOutput->CreateVideoFrame(kWidth, kHeight, kWidth * 4, kPixelFormat, bmdFrameFlagDefault, &frame) != S_OK) { std::cerr << "创建视频帧缓冲区失败" << std::endl; return 1; } // 填充纯色到帧缓冲区 void* buffer = nullptr; frame->GetBytes(&buffer); uint32_t* pixelBuffer = static_cast<uint32_t*>(buffer); // DeckLink 8BitARGB内存格式为BGRA小端,色值按B/G/R/A顺序拼接 uint32_t solidColor = (kBlue << 16) | (kGreen << 8) | kRed | 0xFF000000; for (int i = 0; i < kWidth * kHeight; i++) { pixelBuffer[i] = solidColor; } std::cout << "纯色输出已启动,按Ctrl+C停止运行" << std::endl; // 循环调度同一帧实现持续输出 while (true) { if (deckLinkOutput->ScheduleVideoFrame(frame, 0, displayMode->GetFrameDuration(), displayMode->GetTimeScale()) != S_OK) break; deckLinkOutput->WaitForCompletion(1, nullptr); } // 资源释放 frame->Release(); deckLinkOutput->DisableVideoOutput(); deckLinkOutput->Release(); deckLink->Release(); #if defined(_WIN32) CoUninitialize(); #endif return 0; }
编译运行说明
- Windows平台:在Visual Studio中添加DeckLink SDK的include目录,链接
DeckLinkAPI_i.c和DeckLinkAPI.lib即可编译 - Linux平台:编译命令参考
g++ solid_color_output.cpp -o solid_color_output -I<DeckLink_SDK_include路径> -lDeckLink -lpthread - macOS平台:编译命令参考
clang++ solid_color_output.cpp -o solid_color_output -F/Library/Frameworks -framework DeckLinkAPI -framework CoreVideo
内容的提问来源于stack exchange,提问作者user17477928
相关产品推荐
相关产品推荐

