ffmpeg6.1 SDK 录屏代码
时间: 2023-12-27 22:25:34 浏览: 43
根据提供的引用内容,以下是使用ffmpeg SDK进行录屏的示例代码:
```c++
#include <iostream>
#include <string>
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
int main() {
// 初始化ffmpeg
av_register_all();
// 创建AVFormatContext
AVFormatContext* formatContext = nullptr;
avformat_alloc_output_context2(&formatContext, nullptr, nullptr, "output.mp4");
// 打开视频设备
AVInputFormat* inputFormat = av_find_input_format("dshow");
AVDictionary* options = nullptr;
av_dict_set(&options, "video_size", "640x480", 0);
av_dict_set(&options, "framerate", "30", 0);
av_dict_set(&options, "pixel_format", "yuyv422", 0);
AVFormatContext* inputContext = nullptr;
avformat_open_input(&inputContext, "video=Integrated Camera", inputFormat, &options);
// 添加视频流
AVStream* videoStream = avformat_new_stream(formatContext, nullptr);
avcodec_parameters_copy(videoStream->codecpar, inputContext->streams[0]->codecpar);
videoStream->codecpar->codec_tag = 0;
// 打开输出文件
avio_open(&formatContext->pb, "output.mp4", AVIO_FLAG_WRITE);
// 写入文件头
avformat_write_header(formatContext, nullptr);
// 读取视频帧并写入输出文件
AVPacket packet;
while (av_read_frame(inputContext, &packet) >= 0) {
if (packet.stream_index == 0) {
av_interleaved_write_frame(formatContext, &packet);
}
av_packet_unref(&packet);
}
// 写入文件尾
av_write_trailer(formatContext);
// 清理资源
avformat_close_input(&inputContext);
avio_close(formatContext->pb);
avformat_free_context(formatContext);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)