接收使用FFMPEG库RTSP流 [英] Receiving RTSP stream using FFMPEG library

查看:1039
本文介绍了接收使用FFMPEG库RTSP流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用RTSP我的局域网视频流的IPCamera的。我已经能够捕捉并成功使用ffplay命令显示:

  ffplay的rtsp://管理员:123456@192.168.2.50:7070

(身份验证)

所以我想用的ffmpeg 库,实现了使用编程的C / C ++一样。我想这一定是可行的。

因此​​,让我那句两个简单的问题:


  1. 我如何接收流使用FFMPEG库中的C / C ++程序? (只是提供一些URL /教程,如谷歌没有帮助)


  2. 如何显示接收到的视频? (同样在这里,一些好的网址访问我)。



解决方案

有关RTSP流为我工作(接收帧后,我将结果保存到一个PPM文件)以下内容:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&sstream GT;为externC
{
    #包括LT&; AV codec.h>
    #包括LT&;&avformat.h GT;
    #包括LT&;&avio.h GT;
    #包括LT&;&swscale.h GT;
}无效log_callback(无效* PTR,INT级,为const char * FMT,va_list的vargs)
{
   静态字符信息[8192];
   为const char *模块= NULL;    如果(PTR)
    {
        AVClass * AVC = *(AVClass **)PTR;
        模块= avc-> ITEM_NAME(PTR);
    }
   vsnprintf_s(消息的sizeof(消息),格式化,vargs);   性病::法院LT&;< 日志:<<消息<<的std :: ENDL;
}
INT主(INT ARGC,字符** argv的){    SwsContext * img_convert_ctx;
    AVFormatContext *上下文= avformat_alloc_context();
    AV codecContext * ccontext = AV codec_alloc_context();
    INT video_stream_index;    av_register_all();
    avformat_network_init();
    // av_log_set_callback(安培; log_callback);    //打开RTSP
    如果(avformat_open_input(安培;上下文中,!RTSP://134.169.178.187:8554 / h264.3gp,NULL,NULL)= 0){
        返回EXIT_FAILURE;
    }    如果(avformat_find_stream_info(上下文,NULL)℃,){
        返回EXIT_FAILURE;
    }    //搜索视频流
    的for(int i = 0; I<&上下文GT; nb_streams;我++){
        如果(上下文>流[I] - > codeC-> codeC_TYPE == AVMEDIA_TYPE_VIDEO)
            video_stream_index = I;
    }    AVPacket包;
    av_init_packet(安培;包);    //打开输出文件。
    // AVOutputFormat * FMT = av_guess_format(NULL,test2.mp4,NULL);
    AVFormatContext * OC = avformat_alloc_context();
    // OC-GT&; oformat = FMT;
    // avio_open2(安培; OC-> PBtest.mp4,AVIO_FLAG_WRITE,NULL,NULL);    AVStream *流= NULL;
    INT CNT = 0;
    //从流开始读取数据包,并将其写入到文件
    av_read_play(上下文); //播放RTSP    AV codeC * codeC = NULL;
    codeC = AV codec_find_de codeR(codeC_ID_H264);
    如果(!codeC)出口(1);    AV codec_get_context_defaults3(ccontext,codeC);
    AV codec_copy_context(ccontext,上下文>流[video_stream_index] - > codeC);
    的std :: ofstream的MYFILE;    如果(AV codec_open(ccontext,codeC)℃下)出口(1);    img_convert_ctx = sws_getContext(ccontext->宽度,ccontext->的高度,ccontext-> pix_fmt,ccontext->宽度,ccontext->的高度,
                            PIX_FMT_RGB24,SWS_BICUBIC,NULL,NULL,NULL);    INT大小= avpicture_get_size(PIX_FMT_YUV420P,ccontext->宽度,ccontext->高度);
    uint8_t有* picture_buf =(uint8_t有*)(av_malloc(大小));
    AVFrame * PIC = AV codec_alloc_frame();
    AVFrame * picrgb = AV codec_alloc_frame();
    INT尺寸2 = avpicture_get_size(PIX_FMT_RGB24,ccontext->宽度,ccontext->高度);
    uint8_t有* picture_buf2 =(uint8_t有*)(av_malloc(size2个));
    avpicture_fill((AVPicture *)图片,picture_buf,PIX_FMT_YUV420P,ccontext->宽度,ccontext->高度);
    avpicture_fill((AVPicture *)picrgb,picture_buf2,PIX_FMT_RGB24,ccontext->宽度,ccontext->高度);    而(av_read_frame(上下文中,放大器;分组)GT; = 0&放大器;&放大器; CNT&下; 1000)
    {//读取100帧        性病::法院LT&;< 1帧:<< CNT<<的std :: ENDL;
        如果(packet.stream_index == video_stream_index){//分组是视频
            性病::法院LT&;< 2是视频<<的std :: ENDL;
            如果(流== NULL)
            {//创建文件流
                性病::法院LT&;< 3创建流<<的std :: ENDL;
                流= avformat_new_stream(OC,上下文>流[video_stream_index] - > codeC-> codeC);
                AV codec_copy_context(与于流GT; codeC,上下文>流[video_stream_index] - > codeC);
                 - 于流GT; sample_aspect_ratio =上下文>流[video_stream_index] - > codeC-> sample_aspect_ratio;
            }
            INT检查= 0;
            packet.stream_index =&于流GT; ID;
            性病::法院LT&;< 4解码<<的std :: ENDL;
            INT结果= AV codec_de code_video2(ccontext,PIC,和放大器;检查,&安培;包);
            性病::法院LT&;< 字节德codeD<<结果<< 检查<<检查<<的std :: ENDL;
            如果(CNT大于100)// CNT&下; 0)
            {
                sws_scale(img_convert_ctx,pic->的数据,pic-> LINESIZE,0,ccontext->的高度,picrgb->的数据,picrgb-> LINESIZE);
                的std :: stringstream的名称;
                命名<< 测试<< CNT<< .ppm;
                myfile.open(name.str());
                MYFILE<< P3&所述;&下; ccontext->宽度LT;< << ccontext->高度<< 255 \\ n;
                对于(INT Y = 0; Y< ccontext->高度; Y ++)
                {
                    对于(INT X = 0; X< ccontext->宽* 3; X ++)
                        MYFILE<< (中间体)(picrgb->数据[0] + Y * picrgb-> LINESIZE [0])[X]&下;&下; ;
                }
                myfile.close();
            }
            CNT ++;
        }
        av_free_packet(安培;包);
        av_init_packet(安培;包);
    }
    av_free(PIC);
    av_free(picrgb);
    av_free(picture_buf);
    av_free(picture_buf2);    av_read_pause(上下文);
    avio_close(OC-GT&; PB);
    avformat_free_context(OC);    回报(EXIT_SUCCESS);
}

I have an IPCamera on my LAN streaming video using RTSP. I have been able to capture and display it successfully using ffplay command:

ffplay rtsp://admin:123456@192.168.2.50:7070 

(with authentication)

So I would like to achieve the same using programming in C/C++ using ffmpeg library. I guess this must be possible.

So let me phrase two simple questions :

  1. How do I receive the stream in a C/C++ program using FFMPEG library? (just provide some URL/tutorial, as google was not helpful)

  2. How do I display the received video? (same here, some good URL to direct me).

解决方案

For rtsp streams the following is working for me (after receiving frames i save the result to a ppm file):

    #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>

extern "C"
{
    #include <avcodec.h>
    #include <avformat.h>
    #include <avio.h>
    #include <swscale.h>
}

void log_callback(void *ptr, int level, const char *fmt, va_list vargs)
{
   static char message[8192];
   const char *module = NULL;

    if (ptr)
    {
        AVClass *avc = *(AVClass**) ptr;
        module = avc->item_name(ptr);
    }
   vsnprintf_s(message, sizeof(message), fmt, vargs);

   std::cout << "LOG: " << message << std::endl;
}


int main(int argc, char** argv) {

    SwsContext *img_convert_ctx;
    AVFormatContext* context = avformat_alloc_context();
    AVCodecContext* ccontext = avcodec_alloc_context();
    int video_stream_index;

    av_register_all();
    avformat_network_init();
    //av_log_set_callback(&log_callback);

    //open rtsp
    if(avformat_open_input(&context, "rtsp://134.169.178.187:8554/h264.3gp",NULL,NULL) != 0){
        return EXIT_FAILURE;
    }

    if(avformat_find_stream_info(context,NULL) < 0){
        return EXIT_FAILURE;
    }

    //search video stream
    for(int i =0;i<context->nb_streams;i++){
        if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            video_stream_index = i;
    }

    AVPacket packet;
    av_init_packet(&packet);

    //open output file
    //AVOutputFormat* fmt = av_guess_format(NULL,"test2.mp4",NULL);
    AVFormatContext* oc = avformat_alloc_context();
    //oc->oformat = fmt;
    //avio_open2(&oc->pb, "test.mp4", AVIO_FLAG_WRITE,NULL,NULL);

    AVStream* stream=NULL;
    int cnt = 0;
    //start reading packets from stream and write them to file
    av_read_play(context);//play RTSP

    AVCodec *codec = NULL;
    codec = avcodec_find_decoder(CODEC_ID_H264);
    if (!codec) exit(1);

    avcodec_get_context_defaults3(ccontext, codec);
    avcodec_copy_context(ccontext,context->streams[video_stream_index]->codec);
    std::ofstream myfile;

    if (avcodec_open(ccontext, codec) < 0) exit(1);

    img_convert_ctx = sws_getContext(ccontext->width, ccontext->height, ccontext->pix_fmt, ccontext->width, ccontext->height,
                            PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);

    int size = avpicture_get_size(PIX_FMT_YUV420P, ccontext->width, ccontext->height);
    uint8_t* picture_buf = (uint8_t*)(av_malloc(size));
    AVFrame* pic = avcodec_alloc_frame();
    AVFrame* picrgb = avcodec_alloc_frame();
    int size2 = avpicture_get_size(PIX_FMT_RGB24, ccontext->width, ccontext->height);
    uint8_t* picture_buf2 = (uint8_t*)(av_malloc(size2));
    avpicture_fill((AVPicture *) pic, picture_buf, PIX_FMT_YUV420P, ccontext->width, ccontext->height);
    avpicture_fill((AVPicture *) picrgb, picture_buf2, PIX_FMT_RGB24, ccontext->width, ccontext->height);

    while(av_read_frame(context,&packet)>=0 && cnt <1000)
    {//read 100 frames

        std::cout << "1 Frame: " << cnt << std::endl;
        if(packet.stream_index == video_stream_index){//packet is video
            std::cout << "2 Is Video" << std::endl;
            if(stream == NULL)
            {//create stream in file
                std::cout << "3 create stream" << std::endl;
                stream = avformat_new_stream(oc,context->streams[video_stream_index]->codec->codec);
                avcodec_copy_context(stream->codec,context->streams[video_stream_index]->codec);
                stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio;
            }
            int check = 0;
            packet.stream_index = stream->id;
            std::cout << "4 decoding" << std::endl;
            int result = avcodec_decode_video2(ccontext, pic, &check, &packet);
            std::cout << "Bytes decoded " << result << " check " << check << std::endl;
            if(cnt > 100)//cnt < 0)
            {
                sws_scale(img_convert_ctx, pic->data, pic->linesize, 0, ccontext->height, picrgb->data, picrgb->linesize);
                std::stringstream name;
                name << "test" << cnt << ".ppm";
                myfile.open(name.str());
                myfile << "P3 " << ccontext->width << " " << ccontext->height << " 255\n";
                for(int y = 0; y < ccontext->height; y++)
                {
                    for(int x = 0; x < ccontext->width * 3; x++)
                        myfile << (int)(picrgb->data[0] + y * picrgb->linesize[0])[x] << " ";
                }
                myfile.close();
            }
            cnt++;
        }
        av_free_packet(&packet);
        av_init_packet(&packet);
    }
    av_free(pic);
    av_free(picrgb);
    av_free(picture_buf);
    av_free(picture_buf2);

    av_read_pause(context);
    avio_close(oc->pb);
    avformat_free_context(oc);

    return (EXIT_SUCCESS);
}

这篇关于接收使用FFMPEG库RTSP流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆