要在Linux上使用librtmp库播放视频流,您需要执行以下步骤:
安装librtmp库: 对于Debian/Ubuntu系统,使用以下命令安装:
sudo apt-get install librtmp-dev
对于CentOS/RHEL系统,使用以下命令安装:
sudo yum install librtmp-devel
编写一个简单的C程序来播放RTMP流。以下是一个示例程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rtmp.h>
int main(int argc, char *argv[]) {
RTMP *rtmp = NULL;
RTMP_ConnParams *conn_params = NULL;
RTMP_Netbuf *netbuf = NULL;
RTMP_Packet packet;
if (argc != 3) {
printf("Usage: %s <rtmp_url> <stream_key>\n", argv[0]);
return 1;
}
rtmp = RTMP_Alloc();
if (!rtmp) {
fprintf(stderr, "Failed to allocate RTMP instance.\n");
return 1;
}
conn_params = RTMP_ConnParams_Create();
if (!conn_params) {
fprintf(stderr, "Failed to create connection parameters.\n");
RTMP_Free(rtmp);
return 1;
}
RTMP_Init(rtmp);
RTMP_SetupURL(rtmp, argv[1]);
RTMP_SetConnParams(rtmp, conn_params);
if (RTMP_Connect(rtmp, NULL) != RTMP_OK) {
fprintf(stderr, "Failed to connect to RTMP server: %s\n", RTMP_GetError(rtmp));
RTMP_Free(rtmp);
RTMP_ConnParams_Free(conn_params);
return 1;
}
if (RTMP_ConnectStream(rtmp, 0) != RTMP_OK) {
fprintf(stderr, "Failed to connect to stream: %s\n", RTMP_GetError(rtmp));
RTMP_Close(rtmp);
RTMP_Free(rtmp);
RTMP_ConnParams_Free(conn_params);
return 1;
}
netbuf = RTMP_AllocBuffer(rtmp, 1024 * 1024); // Allocate 1MB buffer
if (!netbuf) {
fprintf(stderr, "Failed to allocate network buffer.\n");
RTMP_Close(rtmp);
RTMP_Free(rtmp);
RTMP_ConnParams_Free(conn_params);
return 1;
}
while (RTMP_Read(rtmp, netbuf, -1) == RTMP_OK) {
RTMP_Packet_Init(&packet);
RTMP_GetPacket(rtmp, &packet);
if (packet.m_headerType == RTMP_PacketHeaderType_Audio || packet.m_headerType == RTMP_PacketHeaderType_Video) {
printf("Playing video frame...\n");
// Process the video frame (packet.data, packet.length) here
}
RTMP_FreePacket(&packet);
}
RTMP_Close(rtmp);
RTMP_Free(rtmp);
RTMP_ConnParams_Free(conn_params);
RTMP_FreeBuffer(netbuf);
return 0;
}
编译程序: 使用以下命令编译程序:
gcc -o play_video play_video.c -lrtmp
运行程序:
使用以下命令运行程序,将<rtmp_url>
替换为您的RTMP流地址,将<stream_key>
替换为流密钥:
./play_video <rtmp_url> <stream_key>
这个示例程序将连接到指定的RTMP服务器,播放视频流,并在控制台输出“Playing video frame…”。您可以根据需要修改程序以处理视频帧。