ffmpeg rtsp拉流

 

#include <stdio.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
    //#include "libavfilter/avfilter.h"
#include "libavutil/avutil.h"
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/opt.h"
#include "libavcodec/avfft.h"
#include "libavutil/imgutils.h"
}

int ffmpeg_rtsp_pull();
int main(int argc, char* argv[])
{
    ffmpeg_rtsp_pull();
    getchar();
    return 0;
}

int ffmpeg_rtsp_pull()
{
    AVFormatContext* format_ctx = avformat_alloc_context();
    const char* url = "rtsp://username:password@ip:port/cam/realmonitor?channel=1&subtype=0";
    int ret = -1;
    AVDictionary* format_opts = NULL;
    av_dict_set(&format_opts, "stimeout", "2000000", 0); //设置链接超时时间(us)
    av_dict_set(&format_opts, "rtsp_transport", "tcp", 0); //设置推流的方式,默认udp。
    ret = avformat_open_input(&format_ctx, url, nullptr, &format_opts);
    if (ret != 0) {
        fprintf(stderr, "fail to open url: %s, return value: %d\n", url, ret);
        return -1;
    }
    ret = avformat_find_stream_info(format_ctx, nullptr);
    if (ret < 0) {
        fprintf(stderr, "fail to get stream information: %d\n", ret);
        return -1;
    }

    // audio/video stream index
    int video_stream_index = -1;
    int audio_stream_index = -1;
    fprintf(stdout, "Number of elements in AVFormatContext.streams: %d\n", format_ctx->nb_streams);
    for (int i = 0; i < format_ctx->nb_streams; ++i) {
        const AVStream* stream = format_ctx->streams[i];
        fprintf(stdout, "type of the encoded data: %d\n", stream->codecpar->codec_id);
        if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream_index = i;
            fprintf(stdout, "dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",
                stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);
        }
        else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            audio_stream_index = i;
            fprintf(stdout, "audio sample format: %d\n", stream->codecpar->format);
        }
    }

    if (video_stream_index == -1) {
        fprintf(stderr, "no video stream\n");
        return -1;
    }

    if (audio_stream_index == -1) {
        fprintf(stderr, "no audio stream\n");
    }

    int cnt = 0;
    AVPacket pkt;
    FILE* fp = NULL;
    remove("./testtest.h264");
    fp = fopen("./testtest.h264", "wb+");
    while (1) {
        if (++cnt > 200) break;
        ret = av_read_frame(format_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "error or end of file: %d\n", ret);
            continue;
        }
        if (pkt.stream_index == video_stream_index) {
            printf("is frame type:%d\n", pkt.flags & AV_PKT_FLAG_KEY);
            fwrite(pkt.data, 1, pkt.size, fp);
        }
        av_packet_unref(&pkt);
    }
    if (fp != NULL)
    {
        fclose(fp);
    }
    av_packet_unref(&pkt);
    avformat_free_context(format_ctx);
    printf("pull stream end\n");
    return 0;
}

 

posted @ 2022-12-03 10:08  一夜梦想  阅读(327)  评论(0编辑  收藏  举报