我正在使用FFMpeg解码RTSP视频流。在显示时间(致电cv::imshow(...)),我收到以下异常:
[swscaler @ 0d55e5c0]使用了不赞成使用的像素格式,请确保您正确设置了范围
我正在将像素格式从“ AV_PIX_FMT_YUVJ420P”转换为“ AV_PIX_FMT_YUV420P”。仍然出现上述异常。任何帮助表示赞赏;
int Decodestream()
{
av_register_all();
avdevice_register_all();
avcodec_register_all();
avformat_network_init();
const char *filenameSrc = "rtsp://192.168.1.67/gnz_media/second";
AVCodecContext *pCodecCtx;
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVCodec * pCodec;
AVFrame *pFrame, *pFrameRGB;
if(avformat_open_input(&pFormatCtx,filenameSrc,NULL,NULL) != 0)
{return -1;}
if(av_find_stream_info(pFormatCtx) < 0)
{return -1;}
av_dump_format(pFormatCtx, 0, filenameSrc, 0);
int videoStream = 1;
for(int i=0; i < pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->coder_type==AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if(videoStream == -1) return -1 ;
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec =avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{return -1;} …Run Code Online (Sandbox Code Playgroud) 通过使用以下命令行之一,可以将视频流转换为 RGB 缓冲区:
ffmpeg -i video.mp4 -frames 1 -color_range pc -f rawvideo -pix_fmt rgb24 output.rgb24
ffmpeg -i video.mp4 -frames 1 -color_range pc -f rawvideo -pix_fmt gbrp output.gbrp
Run Code Online (Sandbox Code Playgroud)
然后可以读取这些 RGB 缓冲区,例如使用 Python 和 NumPy:
import numpy as np
def load_buffer_gbrp(path, width=1920, height=1080):
"""Load a gbrp 8-bit raw buffer from a file"""
data = np.frombuffer(open(path, "rb").read(), dtype=np.uint8)
data_gbrp = data.reshape((3, height, width))
img_rgb = np.empty((height, width, 3), dtype=np.uint8)
img_rgb[..., 0] = data_gbrp[2, ...]
img_rgb[..., 1] = data_gbrp[0, ...]
img_rgb[..., 2] = data_gbrp[1, …Run Code Online (Sandbox Code Playgroud)