利用 ffplay.exe 在程序中播放视频
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】雜項
[
24 瀏覽
0 評論
7 贊
6 踩
]
概要
正文
原教程 https://www.bilibili.com/video/BV1HZ4y1978a?spm_id_from=444.41.0.0
配套代码 https://github.com/3150601355/VideoWallPaperDemo/blob/main/VideoWallpaper/VideoWallpaper.cpp
使用此方法播放视频必须使用 ffplay.exe,下载地址:Builds - CODEX FFMPEG @ gyan.dev 下载 ffmpeg-git-essentials.7z,里面有好几个程序,只需要 ffplay.exe
项目目录下:
main.cpp
example.mp4
ffplay(文件夹)
|—ffplay.exe
代码:
#include <windows.h> #include <thread> #include <string> #include <direct.h> HWND hwnd; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CLOSE: DestroyWindow(hwnd); PostQuitMessage(NULL); exit(0); break; } return DefWindowProc(hwnd, msg, wParam, lParam); } // 快速新建窗口 void initwindow(int w, int h, int mode, LRESULT(_stdcall* WindowProcess)(HWND, UINT, WPARAM, LPARAM), LPCTSTR strWndTitle) { // 窗口类 WNDCLASSEX WndClassEx; MSG Msg; // 隐藏cmd if (mode == 0) ShowWindow(GetConsoleWindow(), SW_HIDE); // 填写结构体 WndClassEx.cbSize = sizeof(WNDCLASSEX); WndClassEx.style = CS_VREDRAW | CS_HREDRAW; WndClassEx.lpfnWndProc = WindowProcess; WndClassEx.cbClsExtra = 0; WndClassEx.cbWndExtra = 0; WndClassEx.hInstance = GetModuleHandle(0); WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW); WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClassEx.lpszMenuName = NULL; WndClassEx.lpszClassName = strWndTitle; WndClassEx.hIconSm = NULL; // 注册窗口类 RegisterClassEx(&WndClassEx); // 创建窗口 hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, strWndTitle, strWndTitle, WS_OVERLAPPEDWINDOW, 200, 200, w, h, NULL, NULL, GetModuleHandle(0), NULL); // 显示窗口 ShowWindow(hwnd, SW_SHOWNORMAL); // 更新窗口 UpdateWindow(hwnd); // 循环获得消息 while (GetMessage(&Msg, NULL, NULL, NULL)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } } int main() { std::thread(initwindow, 640, 480, 0, WndProc, L"mywnd").detach(); wchar_t buf[512] = { 0 }; _wgetcwd(buf, 512); std::wstring str = L" " + (std::wstring)buf; str += L"\\example.mp4 -noborder -x 444 -y 428 -loop 0"; STARTUPINFO si{ 0 }; PROCESS_INFORMATION pi{ 0 }; if (CreateProcess(L".\\ffplay\\ffplay.exe", (LPWSTR)str.c_str(), 0, 0, 0, 0, 0, 0, &si, &pi)) { Sleep(500); // 等待视频播放器启动完成 HWND hFfplay = FindWindow(L"SDL_app", 0); // 找到视频窗口 SetParent(hFfplay, hwnd); // 将视频窗口设置为 win32 窗口的子窗口 SetWindowPos(hFfplay, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE); } while (true) Sleep(100); return 0; }