Dump Rtmp Stream To FLV File (从Rtmp流保存为FLV文件)
Posted doudouyoutang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Dump Rtmp Stream To FLV File (从Rtmp流保存为FLV文件)的主要内容,如果未能解决你的问题,请参考以下文章
Car-eye-http-flv-module 实现nginx-rtmp-mudule HTTP方式的FLV直播功能