【FFmpeg】RTSP、RTMP相关命令及开源项目

129 篇文章 140 订阅 ¥69.90 ¥99.00
本文介绍了如何使用FFmpeg将RTSP流转换为RTMP流,包括不同分辨率和码率的设置,以及从RTSP源拉流并推送到RTMP服务器的多个命令实例。此外,还提及了FFmpeg-RTMP的相关开源项目和参考博客资源。
摘要由CSDN通过智能技术生成

【目录】郭老二博文之:图像视频汇总

一、RTSP转RTMP

海康摄像头:抓取 rtsp 流然后转换成 rtmp 推流出去
主码流
ffmpeg -i “rtsp://[用户名]:[密码]@192.168.1.100/h265/ch1/main/av_stream” -f flv -r 25 -s 1920x1080 -an rtmp://localhost/live/a
ffmpeg -i “rtsp://[用户名]:[密码]@192.168.1.100:554/h265/ch1/main/av_stream” -f flv -r 25 -s 1920x1080 -an rtmp://localhost:1935/live/a
子码流
ffmpeg -i “rtsp://[用户名]:[密码]@192.168.1.100/h265/ch1/sub/av_stream” -f flv -r 25 -s 640x360 -an rtmp://localhost/live/a
ffmpeg -i “rtsp://[用户名]:[密码]@192.168.1.100:554/h265/ch1/sub/av_stream” -f flv -r 25 -s 640x36

ffmpeg 是一个开源的跨平台视频和音频处理工具,支持多种格式的视频和音频文件的转换、编辑、剪辑等操作。rtsprtmp 都是视频流传输协议,其中 rtsp 是实时流传输协议,rtmp 是实时消息传输协议。 ffmpeg 支持通过 rtsprtmp 协议获取视频流,并进行处理。可以使用 ffmpeg 命令行工具或者 ffmpeg 库进行开发。 例如,使用 ffmpeg 命令行工具获取 rtsp 视频流并保存为 mp4 格式的命令如下: ``` ffmpeg -i rtsp://xxx.xxx.xxx.xxx:554/live.sdp -c copy output.mp4 ``` 使用 ffmpeg 库获取 rtsp 视频流并进行处理的示例代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; AVPacket pkt; AVFrame *frame = NULL; struct SwsContext *sws_ctx = NULL; int video_stream_index = -1; int ret; av_register_all(); // 打开输入文件 if ((ret = avformat_open_input(&fmt_ctx, "rtsp://xxx.xxx.xxx.xxx:554/live.sdp", NULL, NULL)) < 0) { fprintf(stderr, "Could not open input file\n"); goto end; } // 查找视频流 if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { fprintf(stderr, "Could not find stream info\n"); goto end; } for (int i = 0; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { fprintf(stderr, "Could not find video stream\n"); goto end; } // 打开视频解码器 codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id); if (!codec) { fprintf(stderr, "Could not find decoder\n"); goto end; } codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); goto end; } if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)) < 0) { fprintf(stderr, "Could not copy codec parameters to context\n"); goto end; } if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) { fprintf(stderr, "Could not open codec\n"); goto end; } // 分配帧内存 frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate frame\n"); goto end; } // 初始化像素格式转换上下文 sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); if (!sws_ctx) { fprintf(stderr, "Could not initialize sws context\n"); goto end; } // 读取视频帧并进行处理 while (av_read_frame(fmt_ctx, &pkt) >= 0) { if (pkt.stream_index == video_stream_index) { ret = avcodec_send_packet(codec_ctx, &pkt); if (ret < 0) { fprintf(stderr, "Error sending packet to decoder\n"); goto end; } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error receiving frame from decoder\n"); goto end; } // 转换像素格式 uint8_t *data[AV_NUM_DATA_POINTERS] = {0}; int linesize[AV_NUM_DATA_POINTERS] = {0}; av_image_alloc(data, linesize, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, 1); sws_scale(sws_ctx, frame->data, frame->linesize, 0, codec_ctx->height, data, linesize); // 处理视频帧 // ... av_freep(&data[0]); } } av_packet_unref(&pkt); } end: if (frame) { av_frame_free(&frame); } if (codec_ctx) { avcodec_free_context(&codec_ctx); } if (fmt_ctx) { avformat_close_input(&fmt_ctx); } if (sws_ctx) { sws_freeContext(sws_ctx); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭老二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值