使用librtmp接收直播流和点播流并保存

来源:互联网 发布:淘宝睡衣买家秀 编辑:程序博客网 时间:2024/06/06 01:51
来自rtmpdump.c
rtmpdump 可使用命令
直播流
rtmpdump.exe -o savename.flv -r rtmp://127.0.0.1/live/testlive -v
点播流
rtmpdump.exe -o savename.flv -r rtmp://127.0.0.1/vod/test.flv 

直播需要加-v参数

下面是从中提取的代码,进行直播或点播流的录制

服务器使用FMS测试通过
crtmpserver需注意超时设置

#include <stdlib.h>#include <string.h>#include <math.h>#include <stdio.h>#include <signal.h>#include <stdint.h>#include "librtmp/rtmp_sys.h"#include "librtmp/log.h"#pragma comment(lib,"ws2_32.lib")#pragma comment(lib,"librtmp.lib")int InitSockets(){WORD version;WSADATA wsaData;version = MAKEWORD(1, 1);return (WSAStartup(version, &wsaData) == 0);}void CleanupSockets(){WSACleanup();}int main(){InitSockets(); RTMP rtmp={0}; RTMP_Init(&rtmp);rtmp.Link.timeout=25;//超时设置//由于crtmpserver是每个一段时间(默认8s)发送数据包,需大于发送间隔才行bool bLiveStream=true;//是否直播if (bLiveStream){RTMP_SetupURL(&rtmp,"rtmp://127.0.0.1:1935/live/testlive");//设置直播标志rtmp.Link.lFlags|=RTMP_LF_LIVE;}else{RTMP_SetupURL(&rtmp,"rtmp://127.0.0.1:1935/vod/test.flv");}RTMP_SetBufferMS(&rtmp, 3600*1000);//1hourif(!RTMP_Connect(&rtmp,NULL)){printf("Connect Server Err\n");WSACleanup();return -1;}if(!RTMP_ConnectStream(&rtmp,0)){printf("Connect stream Err\n");RTMP_Close(&rtmp);WSACleanup();return -1;}int buffsize=1024*1024*10;char*buff=(char*)malloc(buffsize);double duration=-1;int nRead;FILE*fp=fopen("aaa.flv","wb");long countbuffsize=0;//它直接输出的就是FLV文件,包括FLV头,可对流按照flv格式解析就可提前音频,视频数据while(nRead=RTMP_Read(&rtmp,buff,buffsize)){fwrite(buff,1,nRead,fp);if (!bLiveStream&&duration<0){ duration = RTMP_GetDuration(&rtmp); printf("duration:%f\n",duration);}countbuffsize+=nRead;printf("\rdownland...:%0.2fkB",countbuffsize*1.0/1024);}fclose(fp);free(buff);buff=NULL;RTMP_Close(&rtmp);WSACleanup();return 0;}


0 0
原创粉丝点击