ffmpeg推流 —— RTMP推流例程
参考:
雷霄骅 - 最简单的基于FFmpeg的推流器(以推送RTMP为例)
nginx+rtmp配置,网页播放rtmp流(实测可用)
RTMP推流例程:
#include <stdio.h>
#include "libavformat/avformat.h"
#include "libavutil/time.h"
#include "libavutil/mathematics.h"#define RTMP_ADDR "rtmp://127.0.0.1:1935/live/1234"void fix_packet_pts(AVPacket *packet, AVRational timebase, int frame_index) {// 两帧之间的持续时间// r_frame_rate = (AVRational) {den , num}, 与time_base结构相反AVRational in_frame_rate = {timebase.num,timebase.den,};int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(in_frame_rate); //in_stream->r_frame_ratepacket->pts = (double)(frame_index * calc_duration) / (double)(av_q2d(timebase)*AV_TIME_BASE);packet->dts = packet->pts;packet->duration = (double)calc_duration / (double)(av_q2d(timebase)*AV_TIME_BASE);
}void send_rtmp(const char *file) {// 输入AVFormatContext *ifmt_ctx = NULL;if (avformat_open_input(&ifmt_ctx, file, 0, 0) < 0) {printf("failed to open input file\n");goto _Error;}if (avformat_find_stream_info(ifmt_ctx, 0) < 0) {printf("failed to find stream info\n");goto _Error;}int video_idx = -1; video_idx = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);if (video_idx < 0) {printf("failed to find stream_index\n");goto _Error;}av_dump_format(ifmt_ctx, 0, file, 0);// 输出AVFormatContext *ofmt_ctx = NULL;if (avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", RTMP_ADDR) < 0) {printf("failed to alloc output context\n");goto _Error;}int out_stm_idx = -1;for (int i = 0; i < ifmt_ctx->nb_streams; i++) { AVStream *in_stream = ifmt_ctx->streams[i];AVStream *out_stream = avformat_new_stream(ofmt_ctx, NULL);avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);out_stream->codecpar->codec_tag = 0;//if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {// out_stream->codecpar->flags |= CODEC_FLAG_GLOBAL_HEADER;//}}av_dump_format(ofmt_ctx, 0, RTMP_ADDR, 1);// open output urlif (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {if (avio_open(&ofmt_ctx->pb, RTMP_ADDR, AVIO_FLAG_WRITE) < 0) {printf("failed to open output url:%s\n", RTMP_ADDR);goto _Error;}}if (avformat_write_header(ofmt_ctx, NULL) < 0) {printf("failed to write header\n");goto _Error;}int frame_index = 0;int64_t start_time = av_gettime();AVPacket *packet = av_packet_alloc();while (1) {if (av_read_frame(ifmt_ctx, packet) < 0) break;// raw h264, doesn't contain pts// 如果实际推流的是flv文件,不会执行里面的fix_packet_ptsif (packet->pts == AV_NOPTS_VALUE) {fix_packet_pts(packet, ifmt_ctx->streams[video_idx]->time_base, frame_index); }// important delayif (packet->stream_index == video_idx) {AVRational timebase = ifmt_ctx->streams[video_idx]->time_base;AVRational timebase_q = {1, AV_TIME_BASE};int64_t pts_time = av_rescale_q(packet->pts, timebase, timebase_q);int64_t now_time = av_gettime() - start_time;if (pts_time > now_time) av_usleep(pts_time - now_time);}// rescale time baseav_packet_rescale_ts(packet, ifmt_ctx->streams[packet->stream_index]->time_base, \ofmt_ctx->streams[packet->stream_index]->time_base);packet->pos = -1;if (packet->stream_index == video_idx) {printf("send %6d video frames to output url\n", frame_index);frame_index++;}if (av_interleaved_write_frame(ofmt_ctx, packet) < 0) {printf("failed to write frame to url\n");break;}av_packet_unref(packet);}av_write_trailer(ofmt_ctx);_Error:if (ifmt_ctx) {avformat_close_input(&ifmt_ctx);}if (ofmt_ctx) {if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) avio_close(ofmt_ctx->pb);avformat_free_context(ofmt_ctx);}if (packet) av_packet_free(&packet);
}int main(int argc, char const* argv[])
{send_rtmp(argv[1]); return 0;
}
测试运行:
出现了一个警告,如下图。大致原因就是找不到视频的时长和文件大小信息,我们不理会也是没有问题的。
实际用ffmpeg命令推流时也会遇到这个问题,ffmpeg -re -i ../../testvideo/chinese_partner.flv -vcodec libx264 -f flv rtmp://127.0.0.1:1935/live/1234
可以添加-flvflags no_duration_filesize 参数,让ffmpeg不要抛出duration_filesize警告。
ffmpeg推流 —— RTMP推流例程相关推荐
- 利用ffmpeg实现rtmp推流直播
我们这次利用ffmpeg实现rtmp推流(最终推流地址统一为rtmp://127.0.0.1:1935/live/123) 1.首先下载ffmpeg和ffplay 官方下载链接为:FFmpeg 下载后 ...
- ffmpeg rtmp 封装发送函数_基于FFmpeg进行RTMP推流(一)
简介 开发环境 FFmpeg sdk下载 项目配置 代码流程 开发环境 vs 2017 FFmpeg sdk下载 下载地址 这里下载3.3.3 - 32bit - share和Dev 8.png Sh ...
- 随身wifi刷debian系统后用ffmpeg进行rtmp推流的一些tip(有关水印和卡顿)
跟风入了随身wifi,虽然只有1.2主频,512内存和4G存储,性能弱鸡,但9块9包邮的价格,立马让它香了起来. 实测,随身wifi的性能用ffmpeg进行rtmp推流毫无压力,cpu和内存占用都极低 ...
- python利用ffmpeg进行rtmp推流直播
思路: opencv读取视频 -> 将视频分割为帧 -> 将每一帧进行需求加工后 -> 将此帧写入pipe管道 -> 利用ffmpeg进行推流直播 pipe管道: 啥是pipe ...
- 【FFmpeg】rtmp推流
参考 -参考 编码+rtmp推流 _ffmpeg_process = subprocess.Popen(('/home/ubuntu/encoder/bin/ffmpeg','-re', '-i', ...
- ffmpeg实现rtmp推流(linux环境)
一.安装ffmpeg 参考:linux安装并配置ffmpeg 二.搭建rtmp推流环境 1.下载nginx-rtmp-module nginx-rtmp-module是一个nginx的插件,让ngin ...
- linux人脸识别视频推流,RTMP推流协议视频智能分析/人脸识别/直播点播平台EasyDSS接口调用注意事项介绍...
TSINGSEE青犀视频目前推出了前端支持不同协议设备接入的视频智能分析平台,包括RTSP协议的EasyNVR.GB28181协议的EasyGBS,RTMP推流协议的EasyDSS,还有能够进行人脸识 ...
- 利用ffmpeg实现rtmp推流
https://www.jianshu.com/p/c141fc7881e7 ffmpeg 常见命令功能: 查看电脑设备: ffmpeg -list_devices true -f dshow ...
- FFmpeg入门 - rtmp推流
FFmpeg入门 - 视频播放_音视频开发老马的博客-CSDN博客介绍了怎样用ffmpeg去播放视频. 里面用于打开视频流的avformat_open_input函数除了打开本地视频之外,实际上也能打 ...
- ffmpeg摄像头Android,Android平台下使用FFmpeg进行RTMP推流(摄像头推流)
简介 今天要给大家介绍如何在Android平台下获取采集的图像,并进行编码推流.同时项目工程也是在之前的代码基础上新增功能 QQ截图20171124114855.png 打开摄像头并设置参数 具体代码 ...
最新文章
- 什么BRIEF算法?BRIEF算法详解
- “23岁本科生发14篇SCI”,文章被学校官网悄悄删了,你怎么看?
- 很久没有更新blog了,对不起每一位朋友。
- tomcat 禁用access.log
- 127.0.0.1 php,关于php -fpm 出现127.0.0.1:9000已经被占用
- symfony php 更新内容,Symfony框架中的Doctrine
- vue 计算文件hash值_vue的hash值原理,也是table切换。
- 机器学习算法基础5-决策树与随机森林
- Android 四大组件学习之Activity五
- 常用连接服务器的ssh工具
- 209最新100个面试问题大全及答案
- NVIDIA英伟达GPU显卡算力一览(包含Tesla和GeForce、TITAN及RTX系列等)
- 点赞动画,鼠标点击动画
- 《人工智能本科专业知识体系与课程设置》笔记
- 【C语言】变量和常量、数据类型、进制转换、数据存储
- 吴恩达 DeepLearning 第二课第三周题目(包括选择题)
- 高手新手都能用的140个电脑技巧 收藏
- LCD工作原理及结构
- CHIL-ORACLE-创建用户
- Linux三剑客awk之行和列
热门文章
- linux vi命令详解 修改几行,Linux系统——Vi命令讲解
- 慕课PDF下载扩展--再也不用担心慕课不给PDF了
- 2022最新Nodejs下载安装配置步骤(保姆级教程)
- 飞桨2.0 PaddleDetection:瓶装酒瑕疵检测迁移学习教程
- Turbo码相关学习
- 格拉布斯离群值检验——理论与 Python 实现
- bdfg的matlab仿真模型,无刷双馈风力发电机变速恒频控制研究
- python秒网课_利用python完成大学刷课(从0到完成的思路)
- 推荐收藏:50个最佳机器学习公共数据集
- Altium Designer,打开PCB文件,提示:File type not recognised