zoukankan      html  css  js  c++  java
  • Dump Rtmp Stream To FLV File (从Rtmp流保存为FLV文件)

    一、准备工作

      搭建本地rtmp服务:

      https://www.cnblogs.com/doudouyoutang/p/6602430.html

      获取使用到的库,openssl 和 librtmp

      参考:

      https://www.jianshu.com/p/b38656443e71
      https://github.com/x2on/OpenSSL-for-iPhone

      也可以从我的工程中直接拿

    二、代码编写

      利用librtmp中的RTMP_Read函数,直接读取到的就是FLV流,然后写入文件,就可以正常播放了。

      

    void RtmpStreamDumper::startDump()
    {
        int readBytes = 0;
        bool bLiveStream = true;
        int bufsize = 1024*1024*10;
        long countbufsize = 0;
        char *buf  = (char*)calloc(sizeof(char), bufsize);
        char *path = (char*)calloc(sizeof(char), this->rtmp_rsource_url.size() + 1);
        strcpy(path, this->rtmp_rsource_url.c_str());
        
        RTMP_LogPrintf("Start Dump To %s", this->dump_flv_path.c_str());
    
        
        RTMP *rtmp = RTMP_Alloc();
        RTMP_Init(rtmp);
        rtmp->Link.timeout=10;
        
        if(!RTMP_SetupURL(rtmp, path))
        {
            RTMP_Log(RTMP_LOGERROR,"SetupURL Err
    ");
            RTMP_Free(rtmp);
            return;
        }
        
        if (bLiveStream){
            rtmp->Link.lFlags|=RTMP_LF_LIVE;
        }
    
        RTMP_SetBufferMS(rtmp, 3600*1000);
    
        if(!RTMP_Connect(rtmp,NULL)){
            RTMP_Log(RTMP_LOGERROR,"Connect Err
    ");
            RTMP_Free(rtmp);
            return ;
        }
        
        if(!RTMP_ConnectStream(rtmp,0)){
            RTMP_Log(RTMP_LOGERROR,"ConnectStream Err
    ");
            RTMP_Close(rtmp);
            RTMP_Free(rtmp);
            return ;
        }
        
        while((readBytes = RTMP_Read(rtmp,buf,bufsize))){
            this->dumpBytesToFlv((const unsigned char *)buf, readBytes);
            countbufsize += readBytes;
            RTMP_LogPrintf("Receive: %5dByte, Total: %5.2fkB
    ",readBytes,countbufsize*1.0/1024);
        }
    
        if(buf){
            free(buf);
        }
        
        if(rtmp){
            RTMP_Close(rtmp);
            RTMP_Free(rtmp);
            rtmp=NULL;
        }
    
    }

    三、执行效果

      

    四、已经封装为可执行文件

      

        std::cout<<"use example :RtmpDumper [rtmp_live_url] [flv_save_path(default to excute folder)]"<<std::endl;
        std::string url((argc > 1)?argv[1]:"");
        std::string path((argc > 2)?argv[2]:"");
        RtmpStreamDumper *dp = new RtmpStreamDumper(url, path);
        dp->startDump();
        return 0;
    

      

    使用方法

    RtmpDumper rtmp://localhost:1935/myapp/room
    

      

    五、代码

    https://github.com/liqiushui/RtmpDumpAsFlv.git

  • 相关阅读:
    nginx开机自启动
    解决This system is not registered with RHN
    数据库琐表
    linux启动和关闭防火墙命令
    linux端口开放
    linux常用命令
    dedecms编辑器不能复制word格式的处理方法
    dede5.7 标题长度限制修改
    网站收录地址大全
    最全的各搜索引擎、各免费收录提交网站入口大全
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10220599.html
Copyright © 2011-2022 走看看