新手学习FFmpeg - 调用API完成录屏
调用FFMPEG Device API完成Mac录屏功能。
调用FFMPEG提供的API来完成录屏功能,大致的思路是:
- 打开输入设备.
- 打开输出设备.
- 从输入设备读取视频流,然后经过解码->编码,写入到输出设备.
+--------------------------------------------------------------+
| +---------+ decode +------------+ |
| | Input | ----------read -------->| Output | |
| +---------+ encode +------------+ |
+--------------------------------------------------------------+
因此主要使用的API就是:
- avformat_open_input
- avcodec_find_decoder
- av_read_frame
- avcodec_send_packet/avcodec_receive_frame
- 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。
- 获取指定格式的输入设备
pAVInputFormat = av_find_input_format("avfoundation");
通过指定格式名称获取到AVInputFormat结构体。
- 打开设备
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容器规范的帧)既可。 获取编码器大致分为两个步骤:
- 构建编码器上下文(AVFormatContext)
- 匹配合适的编码器(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);
}
- 编解码
从输入设备读取的是原生的数据流,也就是经过设备编码之后的数据。 需要先将原生数据进行解码,变成程序可读的数据,在编码成输出设备可识别的数据。 所以这一步的流程是:
- 解码输入设备数据
- 转码
- 编码写入输出设备
通过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完成录屏的更多相关文章
- 新手学习FFmpeg - 调用API完成录屏并进行H.264编码
Screen Record H.264 目前在网络传输视频/音频流都一般会采用H.264进行编码,所以尝试调用FFMPEG API完成Mac录屏功能,同时编码为H.264格式. 在上一篇文章中,通过调 ...
- 新手学习FFmpeg - 调用API编写实现多次淡入淡出效果的滤镜
前面几篇文章聊了聊FFmpeg的基础知识,我也是接触FFmpeg不久,除了时间处理之外,很多高深(滤镜)操作都没接触到.在学习时间处理的时候,都是通过在ffmpeg目前提供的avfilter基础上面修 ...
- 新手学习FFmpeg - 调用API完成视频的读取和输出
在写了几个avfilter之后,原本以为对ffmpeg应该算是入门了. 结果今天想对一个视频文件进行转码操作,才发现基本的视频读取,输出都搞不定. 痛定思痛,仔细研究了一下ffmpeg提供的examp ...
- 新手学习FFmpeg - 调用API完成两个视频的任意合并
本次尝试在视频A中的任意位置插入视频B. 在上一篇中,我们通过调整PTS可以实现视频的加减速.这只是对同一个视频的调转,本次我们尝试对多个视频进行合并处理. Concat如何运行 ffmpeg提供了一 ...
- 新手学习FFmpeg - 调用API计算关键帧渲染时间点
通过简单的计算来,线上I帧在视频中出现的时间点. 完整代码请参考 https://andy-zhangtao.github.io/ffmpeg-examples/ 名词解释 首先需要明确以下名词概念: ...
- 新手学习FFmpeg - 调用API调整视频局部速率
通过修改setpts代码实现调整视频部分的播放速率. 完整代码可参考: https://andy-zhangtao.github.io/ffmpeg-examples/ 在前面提到了PTS/DTS/T ...
- 新手学习FFmpeg - 通过API实现可控的Filter调用链
虽然通过声明[x][y]avfilter=a=x:b=y;avfilter=xxx的方式可以创建一个可用的Filter调用链,并且在绝大多数场合下这种方式都是靠谱和实用的. 但如果想精细化的管理AVF ...
- 新手学习FFmpeg - 通过API完成filter-complex功能
本篇尝试通过API实现Filter Graph功能. 源码请参看 https://andy-zhangtao.github.io/ffmpeg-examples/ FFmpeg提供了很多实用且强大的滤 ...
- android 调用 screenrecord 实现录屏
首先要说明的是并未实现,本文讲一下自己的思路. adb 使用shell 命令 screenrecord 可录屏. 自己写了个app,通过Process p = Runtime.getRuntime() ...
随机推荐
- Oracle RAC运维所遇问题记录一
Oracle11gR2,版本11.2.0.4集群测试环境运行正常 主机名:rac1,rac2 hosts文件: # Public172.17.188.12 rac1172.17.188.13 rac2 ...
- 使用Mxnet基于skip-gram模型实现word2vect
1. 需求 使用skip-gram模式实现word2vect,然后在jaychou_lyrics.txt数据集上应用 jaychou_lyrics.txt数据集收录了周杰伦从第一张专辑到第十张专辑中的 ...
- zabbix 支持的主要监控方式
zabbix 支持的主要监控方式 一.zabbix支持的主要监控方式: zabbix主要Agent,Trapper,SNMP,JMX,IPMI这几种监控方式,本文章主要通过监控理论和实际操作测试等方式 ...
- python网络爬虫(14)使用Scrapy搭建爬虫框架
目的意义 爬虫框架也许能简化工作量,提高效率等.scrapy是一款方便好用,拓展方便的框架. 本文将使用scrapy框架,示例爬取自己博客中的文章内容. 说明 学习和模仿来源:https://book ...
- C#加密解密(AES)
using System; namespace Encrypt { public class AESHelper { /// <summary> /// 默认密钥-密钥的长度必须是32 / ...
- Win常用软件
本节只适合windows系统 VScode 下载 安装 双击安装 打开目录方式 右键文件夹->使用VSCode打开 命令行打开 code folder [dzlua@win10:~]$ ls a ...
- 【iOS】tableView:cellForRowAtIndexPath: 方法未调用
今天遇到这个问题, UITableView 的代理方法 tableView:cellForRowAtIndexPath: - (UITableViewCell *)tableView:(UITable ...
- 一个C++的ElasticSearch Client
ElasticSearch官方是没有提供C++的client的:因此决定自己写一个,命名为ESClient https://github.com/ATinyAnt/ESClient(手下留星 star ...
- Thinkphp5.0快速入门笔记(1)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. Composer安装和更新 C ...
- Android平台使用Ceres Solver
在Android平台上使用Ceres求解器,官方教程不明确,且编译过程遇到了很多问题. 环境 Ubuntu 18.04 源代码 https://github.com/Great-Keith/ceres ...