写一个使用ffmpeg播放rtsp流,vs2019可以编译通过的播放器
时间: 2023-07-25 16:42:06 浏览: 158
好的,以下是一个使用FFmpeg播放RTSP流的简单示例程序(仅供参考):
```c++
#include <iostream>
#include <string>
#include <Windows.h>
#include "ffmpeg.h"
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
using namespace std;
int main()
{
av_register_all();
avformat_network_init();
AVFormatContext* formatCtx = NULL;
AVCodecContext* codecCtx = NULL;
AVCodec* codec = NULL;
AVPacket packet;
AVFrame* frame = NULL;
int videoStreamIndex = -1;
const char* rtspUrl = "rtsp://xxx.xxx.xxx.xxx:554/xxx";
if (avformat_open_input(&formatCtx, rtspUrl, NULL, NULL) != 0)
{
cerr << "Failed to open input file" << endl;
system("pause");
return -1;
}
if (avformat_find_stream_info(formatCtx, NULL) < 0)
{
cerr << "Failed to retrieve stream information" << endl;
system("pause");
return -1;
}
for (unsigned int i = 0; i < formatCtx->nb_streams; i++)
{
if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1)
{
cerr << "Failed to find video stream" << endl;
system("pause");
return -1;
}
codec = avcodec_find_decoder(formatCtx->streams[videoStreamIndex]->codecpar->codec_id);
if (codec == NULL)
{
cerr << "Failed to find codec" << endl;
system("pause");
return -1;
}
codecCtx = avcodec_alloc_context3(codec);
if (codecCtx == NULL)
{
cerr << "Failed to allocate codec context" << endl;
system("pause");
return -1;
}
if (avcodec_parameters_to_context(codecCtx, formatCtx->streams[videoStreamIndex]->codecpar) < 0)
{
cerr << "Failed to copy codec parameters to context" << endl;
system("pause");
return -1;
}
if (avcodec_open2(codecCtx, codec, NULL) < 0)
{
cerr << "Failed to open codec" << endl;
system("pause");
return -1;
}
frame = av_frame_alloc();
if (frame == NULL)
{
cerr << "Failed to allocate frame" << endl;
system("pause");
return -1;
}
cout << "Starting playback..." << endl;
AVPixelFormat dstFormat = AV_PIX_FMT_BGR24;
int dstWidth = codecCtx->width;
int dstHeight = codecCtx->height;
int numBytes = av_image_get_buffer_size(dstFormat, dstWidth, dstHeight, 1);
uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
SwsContext* swsCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt, dstWidth, dstHeight, dstFormat, SWS_BILINEAR, NULL, NULL, NULL);
while (av_read_frame(formatCtx, &packet) >= 0)
{
if (packet.stream_index == videoStreamIndex)
{
if (avcodec_send_packet(codecCtx, &packet) < 0)
{
cerr << "Error while sending a packet to the decoder" << endl;
break;
}
while (avcodec_receive_frame(codecCtx, frame) == 0)
{
sws_scale(swsCtx, frame->data, frame->linesize, 0, codecCtx->height, &buffer, &dstWidth, &dstHeight);
// TODO: 将 buffer 中的数据渲染到屏幕上
av_frame_unref(frame);
}
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
av_free(buffer);
avcodec_close(codecCtx);
avformat_close_input(&formatCtx);
avformat_network_deinit();
cout << "Playback finished" << endl;
system("pause");
return 0;
}
```
注意,在使用之前需要先下载、编译并链接FFmpeg库,这里不再赘述。此外,还需要自行完成视频数据的渲染部分。
阅读全文