使用FFmpeg拉rtmp流

发布时间:2018-08-26 17:44:22
ffmpeg 

ffmpeg API文档:https://ffmpeg.org/doxygen/3.4/index.html

Command+Shift+G打开自定义路径

//
//  main.c
//  pull_rtmp
//
//  Created by zhangyoulun on 2018/8/26.
//  Copyright © 2018年 zhangyoulun. All rights reserved.
//

#include <stdio.h>

#include <libavformat/avformat.h>

#define NAL_SEI 6

int main(int argc, const char * argv[]) {
    char* url = "http://flv.playlives.360.v.btime.com/live_btime/btv_sn_20170706_s1.flv?time=1537539422&sign=dcc085e92c2aa72ea012d7533f6deb26";
    
    //vars
    AVFormatContext *pFormatCtx;
    AVPacket *packet;
    
    //Initialize libavformat and register all the muxers, demuxers and protocols.
    av_register_all();
    //Do global initialization of network components.
    avformat_network_init();
    
    pFormatCtx = avformat_alloc_context();
    if(avformat_open_input(&pFormatCtx,url,NULL,NULL)!=0){
        printf("Couldn't open input stream.\n");
        return -1;
    }
    
    //Read packets of a media file to get stream information.
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){
        printf("Couldn't find stream information.\n");
        return -1;
    }
    
    int video_stream_index = -1;
    for(int i=0;i<pFormatCtx->nb_streams;i++){
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
            video_stream_index = i;
            break;
        }
    }
    printf("video stream index: %d\n", video_stream_index);
    
    packet = (AVPacket*)av_malloc(sizeof(AVPacket));
    
    
    while(av_read_frame(pFormatCtx, packet)>=0){
        if(packet->stream_index==video_stream_index){
//            printf("pts: %d\n", packet->pts);
            printf("dts: %d\n", packet->dts);
            
            //SEI
            uint8_t *pDataTemp = packet->data;
            pDataTemp += 4;
            if ((*pDataTemp++ & 0x1f) == NAL_SEI) {
                int nPayLoadType = 0;
                while (*pDataTemp == 0xFF) {
                    pDataTemp ++;
                    nPayLoadType += 255;
                }
                nPayLoadType += *pDataTemp++;
                
                int nPayLoadSize = 0;
                while (*pDataTemp == 0xFF) {
                    pDataTemp ++;
                    nPayLoadSize += 255;
                }
                nPayLoadSize += *pDataTemp++;
                
                if (nPayLoadType == 100) {
                    for(int i=0;i<nPayLoadSize;i++){
                        printf("%c", pDataTemp[i]);
                    }
                    printf("\n");
                }
            }
        }
        
        av_free_packet(packet);
    }
    
    
    //close
    avformat_close_input(&pFormatCtx);
    
    return 0;
}
原文地址:http://zhyoulun.com/post/pull-rtmp-url-by-using-ffmpeg-lib
转载请注明出处