ffmpeg 进行屏幕录制和推流获取屏幕分辨率 c++
时间: 2023-10-20 09:23:30 浏览: 54
要在C++中使用FFmpeg进行屏幕录制和推流获取屏幕分辨率,可以使用FFmpeg的C API。以下是一个简单的示例代码:
```c++
extern "C" {
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/error.h>
}
int main() {
AVFormatContext *pFormatCtx;
AVOutputFormat *pOutputFmt;
AVStream *pStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVPacket pkt;
int ret;
int width, height;
// 初始化FFmpeg
av_register_all();
avformat_network_init();
// 打开输出文件
avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, "output.mp4");
if (!pFormatCtx) {
printf("Error allocating output context\n");
return -1;
}
// 添加视频流
pOutputFmt = pFormatCtx->oformat;
pStream = avformat_new_stream(pFormatCtx, NULL);
if (!pStream) {
printf("Error creating new stream\n");
avformat_free_context(pFormatCtx);
return -1;
}
// 设置编码器参数
pCodecCtx = pStream->codec;
pCodecCtx->codec_id = pOutputFmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->width = 1920;
pCodecCtx->height = 1080;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pCodecCtx->time_base = (AVRational){1, 30};
pCodecCtx->bit_rate = 400000;
// 查找编码器
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec) {
printf("Codec not found\n");
avformat_free_context(pFormatCtx);
return -1;
}
// 打开编码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Error opening codec\n");
avformat_free_context(pFormatCtx);
return -1;
}
// 创建帧
pFrame = av_frame_alloc();
if (!pFrame) {
printf("Error allocating frame\n");
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
return -1;
}
pFrame->format = pCodecCtx->pix_fmt;
pFrame->width = pCodecCtx->width;
pFrame->height = pCodecCtx->height;
ret = av_frame_get_buffer(pFrame, 32);
if (ret < 0) {
printf("Error allocating frame buffer\n");
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
return -1;
}
// 打开屏幕录制设备
AVInputFormat *pInputFmt = av_find_input_format("gdigrab");
AVDictionary *pOptions = NULL;
av_dict_set(&pOptions, "framerate", "30", 0);
av_dict_set(&pOptions, "draw_mouse", "0", 0);
av_dict_set(&pOptions, "offset_x", "0", 0);
av_dict_set(&pOptions, "offset_y", "0", 0);
av_dict_set(&pOptions, "video_size", "1920x1080", 0);
ret = avformat_open_input(&pFormatCtx, "desktop", pInputFmt, &pOptions);
if (ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
printf("Error opening input: %s\n", errbuf);
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
return -1;
}
// 读取屏幕分辨率
width = pCodecCtx->width;
height = pCodecCtx->height;
// 开始录制
avformat_write_header(pFormatCtx, NULL);
while (1) {
AVFrame *pFrameRaw = av_frame_alloc();
ret = av_read_frame(pFormatCtx, &pkt);
if (ret < 0) {
if (ret == AVERROR_EOF) {
printf("End of file\n");
} else {
printf("Error reading frame\n");
}
break;
}
ret = avcodec_send_packet(pCodecCtx, &pkt);
if (ret < 0) {
printf("Error sending packet\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(pCodecCtx, pFrameRaw);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
if (ret < 0) {
printf("Error receiving frame\n");
break;
}
sws_scale(sws_getContext(width, height, AV_PIX_FMT_BGRA,
pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, SWS_BILINEAR, NULL, NULL, NULL),
(const uint8_t *const *)pFrameRaw->data, pFrameRaw->linesize,
0, height, pFrame->data, pFrame->linesize);
pFrame->pts = av_gettime();
ret = avcodec_send_frame(pCodecCtx, pFrame);
if (ret < 0) {
printf("Error sending frame\n");
break;
}
}
av_packet_unref(&pkt);
}
av_write_trailer(pFormatCtx);
avcodec_close(pCodecCtx);
avformat_free_context(pFormatCtx);
return 0;
}
```
这个示例代码可以录制整个桌面,并将录制的视频输出为MP4格式的文件。其中,`avformat_open_input()`函数打开gdigrab设备,`avcodec_send_frame()`函数将录制的帧发送给编码器进行压缩,`av_write_frame()`函数将压缩后的帧写入输出文件。在代码中,我们也获取了屏幕的分辨率信息。