首页 » 软件优化 » 音视频编程技术录屏软件开发之屏幕录像(抓取图像屏幕桌面采集)

音视频编程技术录屏软件开发之屏幕录像(抓取图像屏幕桌面采集)

南宫静远 2024-10-31 16:11:43 0

扫一扫用手机浏览

文章目录 [+]

在Windows系统使用libavdevice抓取屏幕数据有两种方法:gdigrab和dshow。

1. gdigrab

gdigrab是FFmpeg专门用于抓取Windows桌面的设备。
非常适合用于屏幕录制。
它通过不同的输入URL支持两种方式的抓取:

音视频编程技术录屏软件开发之屏幕录像(抓取图像屏幕桌面采集) 软件优化
(图片来自网络侵删)

(1)“desktop”:抓取整张桌面。
或者抓取桌面中的一个特定的区域。

(2)“title={窗口名称}”:抓取屏幕中特定的一个窗口(目前中文窗口还有乱码问题)。

gdigrab另外还支持一些参数,用于设定抓屏的位置:

offset_x:抓屏起始点横坐标。

offset_y:抓屏起始点纵坐标。

video_size:抓屏的大小。

framerate:抓屏的帧率。

参考的代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//Use gdigrab

AVDictionary options = NULL;

//Set some options

//grabbing frame rate

//av_dict_set(&options,"framerate","5",0);

//The distance from the left edge of the screen or desktop

//av_dict_set(&options,"offset_x","20",0);

//The distance from the top edge of the screen or desktop

//av_dict_set(&options,"offset_y","40",0);

//Video frame size. The default is to capture the full screen

//av_dict_set(&options,"video_size","640x480",0);

AVInputFormat ifmt=av_find_input_format( "gdigrab" );

if (avformat_open_input(&pFormatCtx, "desktop" ,ifmt,&options)!=0){

printf ("Couldn't open input stream.

");

return -1;

}

2. dshow

使用dshow抓屏需要安装抓屏软件:screen-capture-recorder

软件地址:http://sourceforge.net/projects/screencapturer/

下载软件安装完成后,可以指定dshow的输入设备为“screen-capture-recorder”即可。
有关dshow设备的使用方法在上一篇文章中已经有详细叙述,这里不再重复。
参考的代码如下:

1

2

3

4

5

6

7

8

9

10

11

//Use dshow //

//这里需要先安装 screen-capture-recorder 才能使用dshow采集屏幕

//screen-capture-recorder

//Website: http://sourceforge.net/projects/screencapturer/

//

AVInputFormat ifmt=av_find_input_format( "dshow" );

if (avformat_open_input(&pFormatCtx, "video=screen-capture-recorder" ,ifmt,NULL)!=0){

printf ("Couldn't open input stream.

");

return -1;

}

gdigrab支持的参数多一些,可以单独获取某个窗口的图像,也能直接获取某个部分的图像。

而dshow只能截取整个屏幕(至少目前为止我没找到其他方法),并且dshow还需要安装screen-capture-recorder这个软件。

然而这里,我们却是选择使用dshow来采集桌面而不是gdigrab。

原因是dshow采集的效率比gdigrab高,至少我是这么觉得。

看了上面的代码,我想大家也都知道了接下来要怎么办了吧。

是的,和上次我们打开摄像头相比,唯一的不同恐怕就是个名字了。

因此,代码就不说了,直接上完整工程。

我们还是一样,把他转换成yuv420然后保存到文件,用yuvplayer来播放。

我的桌面是1920x1080的,保存了100张图片发现大小达到了296M。

yuv420图像是原始的图像数据,每张图像的大小都是 1920x1080x3/2(为什么是这个值后面再讲)≈ 2.96M 所以100张图像就是296M了。

大的惊人吧!
所以这就是我们在第一节中讲的为什么图像要编码了。

————————————————

版权声明:本文为CSDN博主「雲天之巔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq214517703/article/details/52693408

标签:

相关文章