这篇文章主要介绍了C# 实现SDL2进行视频播放窗口截图和字幕添加,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
使用SDL2进行视频播放窗口截图和字幕添加操作
SDL API查看:https://wiki.libsdl.org/APIByCategory
视频截图
我就废话不多说了,大家还是直接看代码吧~
/// <summary>
/// SDL2截图操作类
/// </summary>
public unsafe class SDLScreenshot
{
IntPtr window;// 窗口对象
IntPtr renderer;// 播放窗口的渲染器(来自于已初始化的播放窗口渲染器)
public SDLScreenshot(IntPtr window, IntPtr renderer)
{
this.window = window;
this.renderer = renderer;
}
/// <summary>
/// 保存截图
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="path"></param>
public void SaveBMP(int width, int height,string path)
{
// 判断渲染器是否初始化
if (renderer == IntPtr.Zero)
{
Console.WriteLine("renderer is null ,please call Init() method.");
return;
}
uint Rmask=0x00FF0000, Gmask = 0x0000FF00, Bmask = 0x000000FF, Amask = 0x00000000;
// 获取图像数据
SDL.SDL_Surface* surface= (SDL.SDL_Surface*)SDL.SDL_CreateRGBSurface(0, width, height, 32, Rmask, Gmask, Bmask, Amask);
//设置纹理的数据
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = width;
destrect.h = height;
// 读取并渲染图像数据
SDL.SDL_RenderReadPixels(renderer, ref destrect, SDL.SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface->pitch);
//保存图片
int i = SDL.SDL_SaveBMP((IntPtr)surface, path);
if (i != 0)
{
Console.WriteLine("screenshot failed." + SDL.SDL_GetError());
}
SDL.SDL_FreeSurface((IntPtr)surface);
//SDL.SDL_RenderClear(renderer);
//SDL.SDL_DestroyRenderer(renderer);
}
/// <summary>
/// 加载截图
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="path"></param>
public void LoadBMP(int width, int height, string path)
{
// 判断渲染器是否初始化
if (renderer == IntPtr.Zero)
{
Console.WriteLine("renderer is null ,please call Init() method.");
return;
}
// 加载图片
IntPtr surface = SDL.SDL_LoadBMP(path);
if (surface == IntPtr.Zero)
{
Console.WriteLine("load bmp failed." + SDL.SDL_GetError());
return;
}
IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
if (texture == IntPtr.Zero)
{
Console.WriteLine("create texture failed." + SDL.SDL_GetError());
return;
}
SDL.SDL_FreeSurface(surface);
//设置纹理的数据
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = width;
destrect.h = height;
SDL.SDL_Rect srcrect = destrect;
//SDL.SDL_RenderClear(renderer);
SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
SDL.SDL_RenderPresent(renderer);
//SDL.SDL_Delay(20);
SDL.SDL_DestroyTexture(texture);
//SDL.SDL_DestroyRenderer(renderer);
//SDL.SDL_DestroyWindow(screen);
//Quit SDL
//SDL.SDL_Quit();
}
}
播放测试代码:
if (isSaveScreenshot)
{
SDLScreenshot screenshot = new SDLScreenshot(sdlVideo.window, sdlVideo.sdlrenderer);
screenshot.SaveBMP(nvVideoframe.VideoFrame->width, nvVideoframe.VideoFrame->height, "screenshot.bmp");
isSaveScreenshot = false;
}
测试效果图:
注:此处截图是直接获取的播放窗口的图像像素来实现的。
视频字幕
/// <summary>
/// SDL2字幕显示类
/// </summary>
public unsafe class SDLTTF
{
IntPtr renderer;// 播放窗口的渲染器(来自于已初始化的播放窗口渲染器)
public SDLTTF(IntPtr renderer)
{
this.renderer = renderer;
}
/// <summary>
/// 展示字幕文字
/// </summary>
/// <param name="text"></param>
public void ShowText(string ttfPath, int fontSize,string text)
{
// 初始化 ttf
if (SDL_ttf.TTF_Init() < 0)
{
Console.WriteLine("SDL_ttf.TTF_Init() failed.");
return;
}
// 是否初始化完成
int was_init = SDL_ttf.TTF_WasInit();
if (was_init == 1)
// SDL_ttf was already initialized
Console.WriteLine("SDL_ttf was already initialized");
else if (was_init == 0)
// SDL_ttf was not already initialized
Console.WriteLine("SDL_ttf was not already initialized");
// 判断是否初始化
if (renderer == IntPtr.Zero)
{
Console.WriteLine("Not initialized by SDL_ttf.TTF_Init() ,please call Init() method.");
return;
}
//如:打开ttfPath=simfang.ttf 字库,设字体为fontSize=20号
IntPtr font = SDL_ttf.TTF_OpenFont(ttfPath, fontSize);
if (font == IntPtr.Zero)
{
Console.WriteLine("open font failed." + SDL.SDL_GetError());
return;
}
// 设置文字颜色
SDL.SDL_Color color;
color.a = 255;
color.r = 255;
color.g = 255;
color.b = 255;
// 渲染文字效果
//IntPtr surface = SDL_ttf.TTF_RenderUTF8_Blended(font, text, color);
IntPtr surface = SDL_ttf.TTF_RenderUNICODE_Blended(font, text, color);
if (surface == IntPtr.Zero)
{
Console.WriteLine("show surface failed." + SDL.SDL_GetError());
}
IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
if (texture == IntPtr.Zero)
{
Console.WriteLine("create texture failed." + SDL.SDL_GetError());
}
SDL.SDL_FreeSurface(surface);
// 关闭字体
SDL_ttf.TTF_CloseFont(font);
// 停止显示
SDL_ttf.TTF_Quit();
//设置纹理的数据
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = text.Length * 20;
destrect.h = 20;
SDL.SDL_Rect srcrect = destrect;
SDL.SDL_RenderClear(renderer);
SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
SDL.SDL_RenderPresent(renderer);
SDL.SDL_DestroyTexture(texture);
SDL.SDL_DestroyRenderer(renderer);
}
}
事件测试字幕添加:
需要的引用库下载:https://www.libsdl.org/projects/SDL_ttf/
/// <summary>
/// 字幕叠加****需要添加三个dll库:SDL2_ttf.dll 、libfreetype-6.dll 、zlib1.dll
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mbtnAddFontText_Click(object sender, EventArgs e)
{
Console.WriteLine("叠加字幕...............");
sdlTTF = new SDLTTF(sdlVideo.sdlrenderer);
// 中英文都需要兼容
string text = "Hello 世界!";
// 设置一个字体库并设置字体大小和显示文字内容
sdlTTF.ShowText("simkai.ttf",12, text);
}
测试效果图:
如果是播放过程中显示字幕一定要在视频渲染完成后渲染字幕,如下面工具类的方法:
/// <summary>
/// 播放视频
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="pixels"></param>
/// <param name="pixelsSize"></param>
/// <param name="pitch"></param>
/// <returns></returns>
public int SDL_Display(int width, int height, IntPtr pixels, int pixelsSize,
int pitch)
{
lock (this)
{
while (isPause)
{
SDL.SDL_Delay(20);//延迟播放
}
#region SDL 视频数据渲染播放
//设置纹理的数据
sdlrect.x = 0;
sdlrect.y = 0;
sdlrect.w = width;
sdlrect.h = height;
SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
//SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, pixels, pitch);//此处代码导致播放窗口绿色阴影
//复制纹理信息到渲染器目标
SDL.SDL_RenderClear(sdltexture);
//SDL.SDL_Rect srcRect = sdlrect;
//SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
//字幕渲染显示-特别提醒:此处必须放置于视频渲染之后,否则字幕不会显示
if (ttfText!=null&&!ttfText.Equals(""))
{
RenderToShowTTF(ttfText);
}
//else
//{
// RenderToShowTTF( "未设置字幕内容");
/
沃梦达教程
本文标题为:C# 实现SDL2进行视频播放窗口截图和字幕添加
猜你喜欢
- Easyx实现扫雷游戏 2023-02-06
- Qt计时器使用方法详解 2023-05-30
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言qsort()函数的使用方法详解 2023-04-26
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- ubuntu下C/C++获取剩余内存 2023-09-18
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- C语言详解float类型在内存中的存储方式 2023-03-27
