ffmpeg解析

1、rtsp解析

ff_ffplay.c的read_thread方法中。

 /* check if packet is in play range specified by user, then queue, otherwise discard */
        stream_start_time = ic->streams[pkt->stream_index]->start_time;
        pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
        pkt_in_play_range = ffp->duration == AV_NOPTS_VALUE ||
                (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
                av_q2d(ic->streams[pkt->stream_index]->time_base) -
                (double)(ffp->start_time != AV_NOPTS_VALUE ? ffp->start_time : 0) / 1000000
                <= ((double)ffp->duration / 1000000);
        if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
            packet_queue_put(&is->audioq, pkt);
        } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
                   && !(is->video_st && (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))) {
            _mCount++;
            ALOGI("获取帧%d = %s = %s\n", _mCount, printCurrentTime(time(0)),printCurrentTime(_mCurrentTime));
            packet_queue_put(&is->videoq, pkt);
        } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
            packet_queue_put(&is->subtitleq, pkt);
        } else {
            av_packet_unref(pkt);
        }

        ffp_statistic_l(ffp);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg是一个用于处理多媒体数据的开源库,它支持多种音视频格式的解析、转换和处理。其中,FFmpeg可以通过RTSP协议来解析实时流媒体数据。 使用FFmpeg解析RTSP流的基本步骤如下: 1. 初始化FFmpeg库和网络模块。 2. 打开RTSP流地址,获取AVFormatContext对象。 3. 找到音视频流的索引,获取AVStream对象。 4. 解码音视频流,获取AVPacket对象。 5. 对AVPacket进行解码,获取AVFrame对象。 6. 对AVFrame进行处理或者编码成其他格式。 具体来说,可以通过如下的代码来解析RTSP流: ```c // 初始化FFmpeg库和网络模块 av_register_all(); avformat_network_init(); // 打开RTSP流地址,获取AVFormatContext对象 AVFormatContext *pFormatCtx = avformat_alloc_context(); if (avformat_open_input(&pFormatCtx, "rtsp://xxx.xxx.xxx.xxx:xxxx/xxx", NULL, NULL) != 0) { printf("Could not open input stream.\n"); return -1; } // 找到音视频流的索引,获取AVStream对象 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Could not find stream information.\n"); return -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audioStream = i; } else if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; } } if (audioStream == -1 || videoStream == -1) { printf("Could not find audio or video stream.\n"); return -1; } // 解码音视频流,获取AVPacket对象 AVPacket *packet = av_packet_alloc(); AVFrame *frame = av_frame_alloc(); while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == audioStream || packet->stream_index == videoStream) { // 对AVPacket进行解码,获取AVFrame对象 int ret = avcodec_send_packet(pCodecCtx, packet); if (ret < 0) { printf("Error sending a packet for decoding.\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { printf("Error during decoding.\n"); break; } // 对AVFrame进行处理或者编码成其他格式 } } av_packet_unref(packet); } // 释放资源 av_frame_free(&frame); av_packet_free(&packet); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值