关键词搜索

源码搜索 ×
×

C# 使用SDL2进行视频播放窗口截图和字幕添加

发布2018-03-06浏览4880次

详情内容

使用SDL2进行视频播放窗口截图和字幕添加

SDL API查看:https://wiki.libsdl.org/APIByCategory

视频截图

  1.     /// <summary>
  2.     /// SDL2截图操作类
  3.     /// </summary>
  4.     public unsafe class SDLScreenshot
  5.     {
  6.         IntPtr window;// 窗口对象
  7.         IntPtr renderer;// 播放窗口的渲染器(来自于已初始化的播放窗口渲染器)
  8.         public SDLScreenshot(IntPtr window, IntPtr renderer)
  9.         {
  10.             this.window = window;
  11.             this.renderer = renderer;
  12.         }
  13.         /// <summary>
  14.         /// 保存截图
  15.         /// </summary>
  16.         /// <param name="width"></param>
  17.         /// <param name="height"></param>
  18.         /// <param name="path"></param>
  19.         public void SaveBMP(int width, int height,string path)
  20.         {
  21.             // 判断渲染器是否初始化
  22.             if (renderer == IntPtr.Zero)
  23.             {
  24.                 Console.WriteLine("renderer is null ,please call Init() method.");
  25.                 return;
  26.             }
  27.             uint Rmask=0x00FF0000, Gmask = 0x0000FF00, Bmask = 0x000000FF, Amask = 0x00000000;
  28.             // 获取图像数据
  29.             SDL.SDL_Surface* surface= (SDL.SDL_Surface*)SDL.SDL_CreateRGBSurface(0, width, height, 32, Rmask, Gmask, Bmask, Amask);
  30.             //设置纹理的数据
  31.             SDL.SDL_Rect destrect;
  32.             destrect.x = 0;
  33.             destrect.y = 0;
  34.             destrect.w = width;
  35.             destrect.h = height;
  36.             // 读取并渲染图像数据
  37.             SDL.SDL_RenderReadPixels(renderer, ref destrect, SDL.SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface->pitch);
  38.             //保存图片
  39.             int i = SDL.SDL_SaveBMP((IntPtr)surface, path);
  40.             if (i != 0)
  41.             {
  42.                 Console.WriteLine("screenshot failed." + SDL.SDL_GetError());
  43.             }
  44.             SDL.SDL_FreeSurface((IntPtr)surface);
  45.             //SDL.SDL_RenderClear(renderer);
  46.             //SDL.SDL_DestroyRenderer(renderer);
  47.         }
  48.         /// <summary>
  49.         /// 加载截图
  50.         /// </summary>
  51.         /// <param name="width"></param>
  52.         /// <param name="height"></param>
  53.         /// <param name="path"></param>
  54.         public void LoadBMP(int width, int height, string path)
  55.         {
  56.             // 判断渲染器是否初始化
  57.             if (renderer == IntPtr.Zero)
  58.             {
  59.                 Console.WriteLine("renderer is null ,please call Init() method.");
  60.                 return;
  61.             }
  62.             // 加载图片
  63.             IntPtr surface = SDL.SDL_LoadBMP(path);
  64.             if (surface == IntPtr.Zero)
  65.             {
  66.                 Console.WriteLine("load bmp failed." + SDL.SDL_GetError());
  67.                 return;
  68.             }
  69.             IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
  70.             if (texture == IntPtr.Zero)
  71.             {
  72.                 Console.WriteLine("create texture failed." + SDL.SDL_GetError());
  73.                 return;
  74.             }
  75.             SDL.SDL_FreeSurface(surface);
  76.             //设置纹理的数据
  77.             SDL.SDL_Rect destrect;
  78.             destrect.x = 0;
  79.             destrect.y = 0;
  80.             destrect.w = width;
  81.             destrect.h = height;
  82.             SDL.SDL_Rect srcrect = destrect;
  83.             //SDL.SDL_RenderClear(renderer);
  84.             SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
  85.             SDL.SDL_RenderPresent(renderer);
  86.             //SDL.SDL_Delay(20);
  87.             SDL.SDL_DestroyTexture(texture);
  88.             //SDL.SDL_DestroyRenderer(renderer);
  89.             //SDL.SDL_DestroyWindow(screen);
  90.             //Quit SDL    
  91.             //SDL.SDL_Quit();
  92.         }
  93.     }

播放测试代码:

  1. if (isSaveScreenshot)
  2. {
  3. SDLScreenshot screenshot = new SDLScreenshot(sdlVideo.window, sdlVideo.sdlrenderer);
  4. screenshot.SaveBMP(nvVideoframe.VideoFrame->width, nvVideoframe.VideoFrame->height, "screenshot.bmp");
  5. isSaveScreenshot = false;
  6. }

测试效果图:


注:此处截图是直接获取的播放窗口的图像像素来实现的。

视频字幕

  1. /// <summary>
  2. /// SDL2字幕显示类
  3. /// </summary>
  4. public unsafe class SDLTTF
  5. {
  6. IntPtr renderer;// 播放窗口的渲染器(来自于已初始化的播放窗口渲染器)
  7. public SDLTTF(IntPtr renderer)
  8. {
  9. this.renderer = renderer;
  10. }
  11. /// <summary>
  12. /// 展示字幕文字
  13. /// </summary>
  14. /// <param name="text"></param>
  15. public void ShowText(string ttfPath, int fontSize,string text)
  16. {
  17. // 初始化 ttf
  18. if (SDL_ttf.TTF_Init() < 0)
  19. {
  20. Console.WriteLine("SDL_ttf.TTF_Init() failed.");
  21. return;
  22. }
  23. // 是否初始化完成
  24. int was_init = SDL_ttf.TTF_WasInit();
  25. if (was_init == 1)
  26. // SDL_ttf was already initialized
  27. Console.WriteLine("SDL_ttf was already initialized");
  28. else if (was_init == 0)
  29. // SDL_ttf was not already initialized
  30. Console.WriteLine("SDL_ttf was not already initialized");
  31. // 判断是否初始化
  32. if (renderer == IntPtr.Zero)
  33. {
  34. Console.WriteLine("Not initialized by SDL_ttf.TTF_Init() ,please call Init() method.");
  35. return;
  36. }
  37. //如:打开ttfPath=simfang.ttf 字库,设字体为fontSize=20号
  38. IntPtr font = SDL_ttf.TTF_OpenFont(ttfPath, fontSize);
  39. if (font == IntPtr.Zero)
  40. {
  41. Console.WriteLine("open font failed." + SDL.SDL_GetError());
  42. return;
  43. }
  44. // 设置文字颜色
  45. SDL.SDL_Color color;
  46. color.a = 255;
  47. color.r = 255;
  48. color.g = 255;
  49. color.b = 255;
  50. // 渲染文字效果
  51. //IntPtr surface = SDL_ttf.TTF_RenderUTF8_Blended(font, text, color);
  52. IntPtr surface = SDL_ttf.TTF_RenderUNICODE_Blended(font, text, color);
  53. if (surface == IntPtr.Zero)
  54. {
  55. Console.WriteLine("show surface failed." + SDL.SDL_GetError());
  56. }
  57. IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
  58. if (texture == IntPtr.Zero)
  59. {
  60. Console.WriteLine("create texture failed." + SDL.SDL_GetError());
  61. }
  62. SDL.SDL_FreeSurface(surface);
  63. // 关闭字体
  64. SDL_ttf.TTF_CloseFont(font);
  65. // 停止显示
  66. SDL_ttf.TTF_Quit();
  67. //设置纹理的数据
  68. SDL.SDL_Rect destrect;
  69. destrect.x = 0;
  70. destrect.y = 0;
  71. destrect.w = text.Length * 20;
  72. destrect.h = 20;
  73. SDL.SDL_Rect srcrect = destrect;
  74. SDL.SDL_RenderClear(renderer);
  75. SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
  76. SDL.SDL_RenderPresent(renderer);
  77. SDL.SDL_DestroyTexture(texture);
  78. SDL.SDL_DestroyRenderer(renderer);
  79. }
  80. }

事件测试字幕添加:

需要的引用库下载:https://www.libsdl.org/projects/SDL_ttf/

  1. /// <summary>
  2. /// 字幕叠加****需要添加三个dll库:SDL2_ttf.dll 、libfreetype-6.dll 、zlib1.dll
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void mbtnAddFontText_Click(object sender, EventArgs e)
  7. {
  8. Console.WriteLine("叠加字幕...............");
  9. sdlTTF = new SDLTTF(sdlVideo.sdlrenderer);
  10. // 中英文都需要兼容
  11. string text = "Hello 世界!";
  12. // 设置一个字体库并设置字体大小和显示文字内容
  13. sdlTTF.ShowText("simkai.ttf",12, text);
  14. }

测试效果图:


如果是播放过程中显示字幕一定要在视频渲染完成后渲染字幕,如下面工具类的方法:

  1. /// <summary>
  2. /// 播放视频
  3. /// </summary>
  4. /// <param name="width"></param>
  5. /// <param name="height"></param>
  6. /// <param name="pixels"></param>
  7. /// <param name="pixelsSize"></param>
  8. /// <param name="pitch"></param>
  9. /// <returns></returns>
  10. public int SDL_Display(int width, int height, IntPtr pixels, int pixelsSize,
  11. int pitch)
  12. {
  13. lock (this)
  14. {
  15. while (isPause)
  16. {
  17. SDL.SDL_Delay(20);//延迟播放
  18. }
  19. #region SDL 视频数据渲染播放
  20. //设置纹理的数据
  21. sdlrect.x = 0;
  22. sdlrect.y = 0;
  23. sdlrect.w = width;
  24. sdlrect.h = height;
  25. SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
  26. //SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, pixels, pitch);//此处代码导致播放窗口绿色阴影
  27. //复制纹理信息到渲染器目标
  28. SDL.SDL_RenderClear(sdltexture);
  29. //SDL.SDL_Rect srcRect = sdlrect;
  30. //SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
  31. SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
  32. //字幕渲染显示-特别提醒:此处必须放置于视频渲染之后,否则字幕不会显示
  33. if (ttfText!=null&&!ttfText.Equals(""))
  34. {
  35. RenderToShowTTF(ttfText);
  36. }
  37. //else
  38. //{
  39. // RenderToShowTTF( "未设置字幕内容");
  40. //}
  41. //视频渲染显示
  42. SDL.SDL_RenderPresent(sdlrenderer);
  43. //SDL.SDL_Delay(40);
  44. //SDL.SDL_PollEvent(out sdlevent);
  45. //switch (sdlevent.type)
  46. //{
  47. // case SDL.SDL_EventType.SDL_QUIT:
  48. // SDL.SDL_Quit();
  49. // return -1;
  50. // default:
  51. // break;
  52. //}
  53. return 0;
  54. }
  55. //SDL.SDL_RenderClear(sdlrenderer);
  56. //SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
  57. //SDL.SDL_RenderPresent(sdlrenderer);
  58. Delay 40ms
  59. //SDL.SDL_Delay(40);
  60. #endregion
  61. //#region SDL 视频数据渲染播放
  62. //设置纹理的数据
  63. sdlrect.x = 0;
  64. sdlrect.y = 0;
  65. sdlrect.w = width;
  66. sdlrect.h = height;
  67. SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
  68. //复制纹理信息到渲染器目标
  69. SDL.SDL_Rect srcRect = sdlrect;
  70. SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
  71. //视频渲染显示
  72. SDL.SDL_RenderPresent(sdlrenderer);
  73. //SDL.SDL_Delay(40);
  74. SDL.SDL_PollEvent(out sdlevent);
  75. switch (sdlevent.type)
  76. {
  77. case SDL.SDL_EventType.SDL_QUIT:
  78. SDL.SDL_Quit();
  79. return -1;
  80. default:
  81. break;
  82. }
  83. return 0;
  84. //#endregion
  85. }
  86. /// <summary>
  87. /// 设置字幕显示内容
  88. /// </summary>
  89. /// <param name="ttfPath"></param>
  90. /// <param name="fontSize"></param>
  91. public void SDL_TTF_TEXT(string ttfPath, string text, int fontSize)
  92. {
  93. this.ttfPath = ttfPath;
  94. this.ttfText = text;
  95. this.ttfFontSize = fontSize;
  96. }
  97. /// <summary>
  98. /// 渲染字幕
  99. /// </summary>
  100. /// <param name="text"></param>
  101. private void RenderToShowTTF(string text)
  102. {
  103. // 初始化 ttf
  104. if (SDL_ttf.TTF_Init() < 0)
  105. {
  106. Console.WriteLine("SDL_ttf.TTF_Init() failed.");
  107. return;
  108. }
  109. // 是否初始化完成
  110. int was_init = SDL_ttf.TTF_WasInit();
  111. if (was_init == 1)
  112. // SDL_ttf was already initialized
  113. Console.WriteLine("SDL_ttf was already initialized");
  114. else if (was_init == 0)
  115. // SDL_ttf was not already initialized
  116. Console.WriteLine("SDL_ttf was not already initialized");
  117. //如:打开ttfPath=simfang.ttf 字库,设字体为fontSize=20号
  118. IntPtr font = SDL_ttf.TTF_OpenFont(ttfPath, ttfFontSize);
  119. if (font == IntPtr.Zero)
  120. {
  121. Console.WriteLine("open font failed." + SDL.SDL_GetError());
  122. return;
  123. }
  124. // 设置文字字体
  125. SDL_ttf.TTF_SetFontStyle(font, SDL_ttf.TTF_STYLE_BOLD);
  126. // 设置文字颜色
  127. SDL.SDL_Color color;
  128. color.a = 255;
  129. color.r = 255;
  130. color.g = 255;
  131. color.b = 255;
  132. // 渲染文字效果
  133. //IntPtr surface = SDL_ttf.TTF_RenderText_Blended(font, text, color);
  134. IntPtr surface = SDL_ttf.TTF_RenderUTF8_Blended(font, text, color);
  135. //IntPtr surface = SDL_ttf.TTF_RenderUNICODE_Blended(font, text, color);
  136. if (surface == IntPtr.Zero)
  137. {
  138. Console.WriteLine("show surface failed." + SDL.SDL_GetError());
  139. }
  140. IntPtr texture = SDL.SDL_CreateTextureFromSurface(sdlrenderer, surface);
  141. if (texture == IntPtr.Zero)
  142. {
  143. Console.WriteLine("create texture failed." + SDL.SDL_GetError());
  144. }
  145. SDL.SDL_FreeSurface(surface);
  146. // 关闭字体
  147. SDL_ttf.TTF_CloseFont(font);
  148. // 停止显示
  149. SDL_ttf.TTF_Quit();
  150. // 计算合适的宽度和高度
  151. int texWidth = 0;
  152. int texHeight = 0;
  153. uint format = 0;
  154. int access = 0;
  155. // 下面这行代码解决字体虚浮不清问题
  156. SDL.SDL_QueryTexture(texture, out format, out access, out texWidth, out texHeight);
  157. //设置纹理的数据
  158. SDL.SDL_Rect destrect;
  159. destrect.x = 0;
  160. destrect.y = 0;
  161. destrect.w = texWidth;
  162. destrect.h = texHeight;
  163. SDL.SDL_Rect srcrect = destrect;
  164. SDL.SDL_RenderCopy(sdlrenderer, texture, ref srcrect, ref destrect);
  165. }

效果就会好很多:

,请看这里的“中华人民共和国”

注意:常用中英文ttf字体包中包含了:times new roman,中山行书百年纪念版,calibri,Christopherhand,DejaVuSansMono,方正兰亭黑,James Fajardo,Monaco,微软雅黑,仿宋,黑体,楷体,宋体,yahei_mono,仿宋_GB2312,楷体_GB2312,迷你简行楷碑等。本文使用的是simkai.ttf。下面是部分字体文件名:

bb1550.ttf
calibri.ttf
calibrib.ttf
calibrii.ttf
calibriz.ttf
comesinhandy.ttf
DejaVuSansMono-Bold.ttf
DejaVuSansMono-BoldOblique.ttf
DejaVuSansMono-Oblique.ttf
DejaVuSansMono.ttf
DroidSansFallback.ttf
James_Fajardo.ttf
Monaco.ttf
msyh.ttf
msyhbd.ttf
simfang.ttf
simhei.ttf
simkai.ttf
simsun.ttc
times.ttf
timesbd.ttf
timesbi.ttf
timesi.ttf
yahei_mono.ttf

如果懒得下载,Windows里面有字体,在C:\Windows\Fonts目录下。

参考资料

sdl2.0 如何才能实现截图功能:http://tieba.baidu.com/p/5214857530

True type fonts with SDL_ttf  : http://www.willusher.io/sdl2%20tutorialshttps://cdn.jxasp.com:9143/image/2013/12/18/lesson-6-true-type-fonts-with-sdl_ttf

SDL TTF:    http://www.sdltutorials.com/sdl-ttf

SDL 使用SDL_ttf显示文字: http://blog.csdn.net/rankun1/article/details/51393732

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载