ffmpeg 解码rtsp
时间: 2023-09-03 16:06:57 浏览: 197
ffmpeg_rtsp_mpp:ffmpeg 拉取rtsp h264流, 使用mpp解码, 目前在firefly 板子上跑通了
5星 · 资源好评率100%
可以使用 ffmpeg 库来解码 RTSP 视频流。下面是一个简单的 C++ 代码示例,使用 ffmpeg 库解码 RTSP 视频流:
```cpp
#include <iostream>
#include <thread>
#include <chrono>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(int argc, char *argv[]) {
AVFormatContext *pFormatCtx = nullptr;
AVCodecContext *pCodecCtxOrig = nullptr;
AVCodecContext *pCodecCtx = nullptr;
AVCodec *pCodec = nullptr;
AVFrame *pFrame = nullptr;
AVPacket *packet = nullptr;
// RTSP URL
const char *url = "rtsp://example.com/stream";
// Register all formats and codecs
av_register_all();
avformat_network_init();
// Open RTSP stream
if (avformat_open_input(&pFormatCtx, url, nullptr, nullptr) != 0) {
std::cerr << "Could not open input stream" << std::endl;
return -1;
}
// Retrieve stream information
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
std::cerr << "Could not find stream information" << std::endl;
return -1;
}
// Find the first video stream
int videoStreamIndex = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
// Check if video stream was found
if (videoStreamIndex == -1) {
std::cerr << "Could not find video stream" << std::endl;
return -1;
}
// Get a pointer to the codec context for the video stream
pCodecCtxOrig = pFormatCtx->streams[videoStreamIndex]->codec;
// Find the decoder for the video stream codec
pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
if (!pCodec) {
std::cerr << "Unsupported codec" << std::endl;
return -1;
}
// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
std::cerr << "Could not copy codec context" << std::endl;
return -1;
}
// Open codec
if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) {
std::cerr << "Could not open codec" << std::endl;
return -1;
}
// Allocate video frame
pFrame = av_frame_alloc();
// Allocate packet
packet = av_packet_alloc();
// Read frames from the stream
while (av_read_frame(pFormatCtx, packet) >= 0) {
// Check if packet belongs to video stream
if (packet->stream_index == videoStreamIndex) {
// Send packet to decoder
avcodec_send_packet(pCodecCtx, packet);
// Receive decoded frame
int ret = avcodec_receive_frame(pCodecCtx, pFrame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
// Not enough data yet, or end of file
continue;
} else if (ret < 0) {
std::cerr << "Error decoding frame" << std::endl;
break;
}
// Convert frame to RGB
SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!sws_ctx) {
std::cerr << "Could not initialize the conversion context" << std::endl;
break;
}
AVFrame *rgbFrame = av_frame_alloc();
if (!rgbFrame) {
std::cerr << "Could not allocate RGB frame" << std::endl;
break;
}
av_image_alloc(rgbFrame->data, rgbFrame->linesize, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, 1);
sws_scale(sws_ctx, (const uint8_t *const *) pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
rgbFrame->data, rgbFrame->linesize);
// Display frame
std::cout << "Frame width: " << pCodecCtx->width << ", height: " << pCodecCtx->height << std::endl;
// Free RGB frame
av_freep(&rgbFrame->data[0]);
av_frame_free(&rgbFrame);
sws_freeContext(sws_ctx);
}
// Free packet
av_packet_unref(packet);
// Sleep for a short time to simulate real-time playback
std::this_thread::sleep_for(std::chrono::milliseconds(33));
}
// Free resources
av_packet_free(&packet);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
avformat_network_deinit();
return 0;
}
```
这个代码示例使用了 ffmpeg 库中的 AVFormatContext、AVCodecContext、AVCodec、AVFrame 和 AVPacket 等数据类型,以及相关的函数来打开 RTSP 视频流,解码视频帧,并展示出来。这个代码示例中,我们将解码出来的视频帧转换为 RGB 格式,并输出帧的宽度和高度。你可以根据自己的需求来修改这个代码示例。
阅读全文