调用FFMPEG Device API完成Mac录屏功能。

调用FFMPEG提供的API来完成录屏功能,大致的思路是:

  1. 打开输入设备.
  2. 打开输出设备.
  3. 从输入设备读取视频流,然后经过解码->编码,写入到输出设备.
+--------------------------------------------------------------+
| +---------+ decode +------------+ |
| | Input | ----------read -------->| Output | |
| +---------+ encode +------------+ |
+--------------------------------------------------------------+

因此主要使用的API就是:

  1. avformat_open_input
  2. avcodec_find_decoder
  3. av_read_frame
  4. avcodec_send_packet/avcodec_receive_frame
  5. avcodec_send_frame/avcodec_receive_packet
  • 打开输入设备

如果使用FFmpeg提供的-list_devices 命令可以查询到当前支持的设备,其中分为两类:

  • AVFoundation video devices
  • AVFoundation audio devices

AVFoundation 是Mac特有的基于时间的多媒体处理框架。本次是演示录屏功能,因此忽略掉audio设备,只考虑video设备。在avfoundation.m文件中没有发现可以程序化读取设备的API。FFmpeg官方也说明没有程序化读取设备的方式,通用方案是解析日志来获取设备(https://trac.ffmpeg.org/wiki/DirectShow#Howtoprogrammaticallyenumeratedevices),下一篇再研究如何通过日志获取当前支持的设备,本次就直接写死设备ID。

  1. 获取指定格式的输入设备
    pAVInputFormat = av_find_input_format("avfoundation");

通过指定格式名称获取到AVInputFormat结构体。

  1. 打开设备
    value = avformat_open_input(&pAVFormatContext, "1", pAVInputFormat, &options);
if (value != 0) {
cout << "\nerror in opening input device";
exit(1);
}

"1"指代的是设备ID。 options是打开设备时输入参数,

    // 记录鼠标
value = av_dict_set(&options, "capture_cursor", "1", 0);
if (value < 0) {
cout << "\nerror in setting capture_cursor values";
exit(1);
} // 记录鼠标点击事件
value = av_dict_set(&options, "capture_mouse_clicks", "1", 0);
if (value < 0) {
cout << "\nerror in setting capture_mouse_clicks values";
exit(1);
} // 指定像素格式
value = av_dict_set(&options, "pixel_format", "yuyv422", 0);
if (value < 0) {
cout << "\nerror in setting pixel_format values";
exit(1);
}

通过value值判断设备是否正确打开。 然后获取设备视频流ID(解码数据包时需要判断是否一致),再获取输入编码器(解码时需要)。

  • 打开输出设备

假设需要将从输入设备读取的数据保存成mp4格式的文件。

将视频流保存到文件中,只需要一个合适的编码器(用于生成符合MP4容器规范的帧)既可。 获取编码器大致分为两个步骤:

  1. 构建编码器上下文(AVFormatContext)
  2. 匹配合适的编码器(AVCodec)

构建编码器:

    // 根据output_file后缀名推测合适的编码器
avformat_alloc_output_context2(&outAVFormatContext, NULL, NULL, output_file);
if (!outAVFormatContext) {
cout << "\nerror in allocating av format output context";
exit(1);
}

匹配编码器:

    output_format = av_guess_format(NULL, output_file, NULL);
if (!output_format) {
cout << "\nerror in guessing the video format. try with correct format";
exit(1);
} video_st = avformat_new_stream(outAVFormatContext, NULL);
if (!video_st) {
cout << "\nerror in creating a av format new stream";
exit(1);
}
  • 编解码

从输入设备读取的是原生的数据流,也就是经过设备编码之后的数据。 需要先将原生数据进行解码,变成程序可读的数据,在编码成输出设备可识别的数据。 所以这一步的流程是:

  1. 解码输入设备数据
  2. 转码
  3. 编码写入输出设备

通过av_read_frame从输入设备读取数据:

while (av_read_frame(pAVFormatContext, pAVPacket) >= 0) {
...
}

对读取后的数据进行拆包,找到我们所感兴趣的数据

    // 最开始没有做这种判断,出现不可预期的错误。 在官网example中找到这句判断,但还不是很清楚其意义。应该和packet封装格式有关
pAVPacket->stream_index == VideoStreamIndx

从FFmpeg 4.1开始,有了新的编解码函数。 为了长远考虑,直接使用新API。 使用avcodec_send_packet将输入设备的数据发往解码器进行解码,然后使用avcodec_receive_frame解码器接受解码之后的数据帧。代码大概是下面的样子:

            value = avcodec_send_packet(pAVCodecContext, pAVPacket);
if (value < 0) {
fprintf(stderr, "Error sending a packet for decoding\n");
exit(1);
} while(1){
value = avcodec_receive_frame(pAVCodecContext, pAVFrame);
if (value == AVERROR(EAGAIN) || value == AVERROR_EOF) {
break; } else if (value < 0) {
fprintf(stderr, "Error during decoding\n");
exit(1);
} .... do something
}

读取到数据帧后,就可以对每一帧进行转码:

    sws_scale(swsCtx_, pAVFrame->data, pAVFrame->linesize, 0, pAVCodecContext->height, outFrame->data,outFrame->linesize);

最后将转码后的帧封装成输出设备可设别的数据包格式。也就是解码的逆动作,使用avcodec_send_frame将每帧发往编码器进行编码,通过avcodec_receive_packet一直接受编码之后的数据包。处理逻辑大致是:

                value = avcodec_send_frame(outAVCodecContext, outFrame);
if (value < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
} while (value >= 0) {
value = avcodec_receive_packet(outAVCodecContext, &outPacket);
if (value == AVERROR(EAGAIN) || value == AVERROR_EOF) {
break;
} else if (value < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
} ... do something; av_packet_unref(&outPacket);
}

以后就按照这种的处理逻辑,不停的从输入设备读取数据,然后经过解码->转码->编码,最后发送到输出设备。 这样就完成了录屏功能。

上面是大致处理思路,完整源代码可以参考 (https://github.com/andy-zhangtao/ffmpeg-examples/tree/master/ScreenRecord) .

新手学习FFmpeg - 调用API完成录屏的更多相关文章

  1. 新手学习FFmpeg - 调用API完成录屏并进行H.264编码

    Screen Record H.264 目前在网络传输视频/音频流都一般会采用H.264进行编码,所以尝试调用FFMPEG API完成Mac录屏功能,同时编码为H.264格式. 在上一篇文章中,通过调 ...

  2. 新手学习FFmpeg - 调用API编写实现多次淡入淡出效果的滤镜

    前面几篇文章聊了聊FFmpeg的基础知识,我也是接触FFmpeg不久,除了时间处理之外,很多高深(滤镜)操作都没接触到.在学习时间处理的时候,都是通过在ffmpeg目前提供的avfilter基础上面修 ...

  3. 新手学习FFmpeg - 调用API完成视频的读取和输出

    在写了几个avfilter之后,原本以为对ffmpeg应该算是入门了. 结果今天想对一个视频文件进行转码操作,才发现基本的视频读取,输出都搞不定. 痛定思痛,仔细研究了一下ffmpeg提供的examp ...

  4. 新手学习FFmpeg - 调用API完成两个视频的任意合并

    本次尝试在视频A中的任意位置插入视频B. 在上一篇中,我们通过调整PTS可以实现视频的加减速.这只是对同一个视频的调转,本次我们尝试对多个视频进行合并处理. Concat如何运行 ffmpeg提供了一 ...

  5. 新手学习FFmpeg - 调用API计算关键帧渲染时间点

    通过简单的计算来,线上I帧在视频中出现的时间点. 完整代码请参考 https://andy-zhangtao.github.io/ffmpeg-examples/ 名词解释 首先需要明确以下名词概念: ...

  6. 新手学习FFmpeg - 调用API调整视频局部速率

    通过修改setpts代码实现调整视频部分的播放速率. 完整代码可参考: https://andy-zhangtao.github.io/ffmpeg-examples/ 在前面提到了PTS/DTS/T ...

  7. 新手学习FFmpeg - 通过API实现可控的Filter调用链

    虽然通过声明[x][y]avfilter=a=x:b=y;avfilter=xxx的方式可以创建一个可用的Filter调用链,并且在绝大多数场合下这种方式都是靠谱和实用的. 但如果想精细化的管理AVF ...

  8. 新手学习FFmpeg - 通过API完成filter-complex功能

    本篇尝试通过API实现Filter Graph功能. 源码请参看 https://andy-zhangtao.github.io/ffmpeg-examples/ FFmpeg提供了很多实用且强大的滤 ...

  9. android 调用 screenrecord 实现录屏

    首先要说明的是并未实现,本文讲一下自己的思路. adb 使用shell 命令 screenrecord 可录屏. 自己写了个app,通过Process p = Runtime.getRuntime() ...

随机推荐

  1. LeetCode_32

    LeetCode 32 题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效 ...

  2. 原 docker 安装使用 solr

    1.安装solr 7.5 docker solr 官网:https://hub.docker.com/_/solr/ docker pull solr:7.5.0 2.启动solr服务 docker ...

  3. <<Modern CMake>> 翻译 1. CMake 介绍

    <<Modern CMake>> 翻译 1. CMake 介绍 人们喜欢讨厌构建系统. 仅仅观看 CppCon17 上的演讲,就可以看到开发人员因为构建系统而闹笑话的例子. 这 ...

  4. jquery实现最简单的下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. python 感悟

    * 优美胜于丑陋.* 显式胜于隐式.* 简单胜于复杂.* 复杂胜于难懂.* 扁平胜于嵌套.* 稀疏胜于紧密.* 可读性应当被重视.* 尽管实用性会打败纯粹性,特例也不能凌驾于规则之上.* 不要忽略任何 ...

  6. 关于定时器Scheduled(cron)的问题

    定时器配置步骤参考:http://blog.csdn.NET/sd4000784/article/details/7745947 下面给出cron参数中各个参数的含义: CRON表达式    含义 & ...

  7. win10去除快捷方式小箭头

    切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...

  8. ASP.NET Core MVC 之局部视图(Partial Views)

    1.什么是局部视图 局部视图是在其他视图中呈现的视图.通过执行局部视图生成的HTML输出呈现在调用视图中.与视图一样,局部视图使用 .cshtml 文件扩展名.当希望在不同视图之间共享网页的可重用部分 ...

  9. Django是如何防止注入攻击-XSS攻击-CSRF攻击

    注入攻击-XSS攻击-CSRF攻击介绍请访问:https://www.cnblogs.com/hwnzy/p/11219475.html Django防止注入攻击 Django提供一个抽象的模型层来组 ...

  10. 图像反转(一些基本的灰度变换函数)基本原理及Python实现

    1. 基本原理 获取像素值在[0, L]范围内的图像的反转图像,即为负片.适用于增强图像中白色或者灰色的区域,尤其当黑色在图片中占主地位时候 $$T(r) = L-r$$ 2. 运行结果 图源自ski ...