C++ ffmpeg RTSP 视频推流实现, arm linux平台

环境:
FFmpeg版本:n4.2.2

下载地址(下载编译后请确认版本正确):

https://ffmpeg.org//download.html
下面地址经过第三方git加速可能存在实效性:
https://hub.fgit.cf/FFmpeg/FFmpeg/tree/n4.4.2

实现代码:


#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32
//Windows
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/mathematics.h"
#include "libavutil/time.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/time.h>
#ifdef __cplusplus
};
#endif
#endif#define CODEC_FLAG_GLOBAL_HEADER  0x00400000int main(int argc, char* argv[])
{AVOutputFormat *ofmt = NULL;//输入对应一个AVFormatContext,输出对应一个AVFormatContext//(Input AVFormatContext and Output AVFormatContext)AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;AVPacket pkt;const char *in_filename, *out_filename;int ret, i;int videoindex=-1;int frame_index=0;int64_t start_time=0;in_filename  = "test.mp4";//输入URL(Input file URL)out_filename = "rtmp://192.168.110.79:1935/live/1";//输出 URL(Output URL)[RTMP]av_register_all();//Networkavformat_network_init();//输入(Input)if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {printf( "Could not open input file.");goto end;}if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {printf( "Failed to retrieve input stream information");goto end;}for(i=0; i<ifmt_ctx->nb_streams; i++) if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){videoindex=i;break;}av_dump_format(ifmt_ctx, 0, in_filename, 0);//输出(Output)avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_filename); //RTMP//avformat_alloc_output_context2(&ofmt_ctx, NULL, "mpegts", out_filename);//UDPif (!ofmt_ctx) {printf( "Could not create output context\n");ret = AVERROR_UNKNOWN;goto end;}ofmt = ofmt_ctx->oformat;for (i = 0; i < ifmt_ctx->nb_streams; i++) {//根据输入流创建输出流(Create output AVStream according to input AVStream)AVStream *in_stream = ifmt_ctx->streams[i];AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);if (!out_stream) {printf( "Failed allocating output stream\n");ret = AVERROR_UNKNOWN;goto end;}//复制AVCodecContext的设置(Copy the settings of AVCodecContext)ret = avcodec_copy_context(out_stream->codec, in_stream->codec);if (ret < 0) {printf( "Failed to copy context from input to output stream codec context\n");goto end;}out_stream->codec->codec_tag = 0;if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;}//Dump Format------------------av_dump_format(ofmt_ctx, 0, out_filename, 1);//打开输出URL(Open output URL)if (!(ofmt->flags & AVFMT_NOFILE)) {ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);if (ret < 0) {printf( "Could not open output URL '%s'", out_filename);goto end;}}//写文件头(Write file header)ret = avformat_write_header(ofmt_ctx, NULL);if (ret < 0) {printf( "Error occurred when opening output URL\n");goto end;}start_time=av_gettime();while (1) {AVStream *in_stream, *out_stream;//获取一个AVPacket(Get an AVPacket)ret = av_read_frame(ifmt_ctx, &pkt);if (ret < 0)break;//FIX:No PTS (Example: Raw H.264)//Simple Write PTSif(pkt.pts==AV_NOPTS_VALUE){//Write PTSAVRational time_base1=ifmt_ctx->streams[videoindex]->time_base;//Duration between 2 frames (us)int64_t calc_duration=(double)AV_TIME_BASE/av_q2d(ifmt_ctx->streams[videoindex]->r_frame_rate);//Parameterspkt.pts=(double)(frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);pkt.dts=pkt.pts;pkt.duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);}//Important:Delayif(pkt.stream_index==videoindex){AVRational time_base=ifmt_ctx->streams[videoindex]->time_base;AVRational time_base_q={1,AV_TIME_BASE};int64_t pts_time = av_rescale_q(pkt.dts, time_base, time_base_q);int64_t now_time = av_gettime() - start_time;if (pts_time > now_time)av_usleep(pts_time - now_time);}in_stream  = ifmt_ctx->streams[pkt.stream_index];out_stream = ofmt_ctx->streams[pkt.stream_index];/* copy packet *///转换PTS/DTS(Convert PTS/DTS)pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);pkt.pos = -1;//Print to Screenif(pkt.stream_index==videoindex){printf("Send %8d video frames to output URL\n",frame_index);frame_index++;}//ret = av_write_frame(ofmt_ctx, &pkt);ret = av_interleaved_write_frame(ofmt_ctx, &pkt);if (ret < 0) {printf( "Error muxing packet\n");break;}av_free_packet(&pkt);}//写文件尾(Write file trailer)av_write_trailer(ofmt_ctx);
end:avformat_close_input(&ifmt_ctx);/* close output */if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))avio_close(ofmt_ctx->pb);avformat_free_context(ofmt_ctx);if (ret < 0 && ret != AVERROR_EOF) {printf( "Error occurred.\n");return -1;}return 0;
}

cmake:

cmake_minimum_required(VERSION 3.10.2)
project(w_test)SET(CMAKE_C_COMPILER /mnt/sda2/rk3588_sdk/other/rk3566_gcc_tool/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /mnt/sda2/rk3588_sdk/other/rk3566_gcc_tool/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++)
SET(CMAKE_STRIP /mnt/sda2/rk3588_sdk/other/rk3566_gcc_tool/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-strip)set(ffmpeg_libs_DIR /home/rog/my_file/other_LIB/myffmpeg/4.2.2/FFmpeg/my_lib_arm/lib)
set(ffmpeg_headers_DIR /home/rog/my_file/other_LIB/myffmpeg/4.2.2/FFmpeg/my_lib_arm/include)add_library( avcodec SHARED IMPORTED)
add_library( avfilter SHARED IMPORTED )
add_library( swresample SHARED IMPORTED )
add_library( swscale SHARED IMPORTED )
add_library( avformat SHARED IMPORTED )
add_library( avutil SHARED IMPORTED )set_target_properties( avcodec PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavcodec.so )
set_target_properties( avfilter PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavfilter.so )
set_target_properties( swresample PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libswresample.so )
set_target_properties( swscale PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libswscale.so )
set_target_properties( avformat PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavformat.so )
set_target_properties( avutil PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavutil.so )include_directories( ${ffmpeg_headers_DIR} )
link_directories(${ffmpeg_libs_DIR} )set(CMAKE_CXX_STANDARD 14)
add_executable(w_test main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} avcodec avformat avutil swresample swscale swscale avfilter )

效果:
在这里插入图片描述
参考:
最简单的基于FFmpeg的推流器(以推送RTMP为例)

ffmpeg c++代码推流RTSP/RTMP(命令行推流)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/656290.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入解析Dubbo负载均衡策略

深入解析Dubbo负载均衡策略 I. 引言 在当今日益复杂和庞大的网络环境中&#xff0c;分布式系统的设计和实现成为了现代软件架构的重要组成部分。Dubbo框架作为一种高性能、轻量级的分布式服务框架&#xff0c;为构建分布式系统提供了强大的支持。然而&#xff0c;随着系统规模…

发票pdf文件解析

借助pdfplumber 解析 效果如下&#xff1a; { 发票号码(FPHM): 24322000000011529984, 开票日期(KPRQ): 2024年01月11日, 合计(HJ): 1205.94, 购方: 91320213586657279T, 销方: 91320214MAD1N7EN36, 价税合计(JSHJ): 1218.00, 项目(XM)-1: …

【HarmonyOS应用开发】UIAbility实践第一部分(五)

一、UIAbility概述 1、UIAbility是一种包含用户界面的应用组件&#xff0c;主要用于和用户进行交互。UIAbility也是系统调度的单元&#xff0c;为应用提供窗口在其中绘制界面。 2、每一个UIAbility实例&#xff0c;都对应于一个最近任务列表中的任务。 3、一个应用可以有一个UI…

获取文件夹下所有文件路径

有时候我们会获取文件夹下所有文件的路径以及完成的名称,这时候如果有一个函数库轻松帮我得到数据就好了,还真有. cpp void getFiles(const std::string & path, std::vector<std::string> & files) { //文件句柄 long hFile 0; //文件信息&#xff0c;_fi…

信息安全考证攻略

&#x1f525;在信息安全领域&#xff0c;拥有相关的证书不仅能提升自己的专业技能&#xff0c;更能为职业生涯增添不少光彩。下面为大家盘点了一些国内外实用的信息安全证书&#xff0c;让你一睹为快&#xff01; &#x1f31f;国内证书&#xff08;认证机构&#xff1a;中国信…

数据与资源可视化——长安链运维监控实践

前言 “链上的交易总量是多少”&#xff0c;“我的链上现在有多少区块了”&#xff0c;“节点是否存活无法第一时间感知到”&#xff0c;除sdk查询链上的相关信息外&#xff0c;今天我们介绍一种新的方式实现链上数据与相关资源的可视化的监控。 简介 监控链上数据以及链上节…

[提高工作开发效率,远离996]程序员常用的工具软件推荐

前言 现如今&#xff0c;技术发展十分迅猛&#xff0c;开发者只有通过不断的学习才能跟得上时代的步伐。而为了便于学习和工作&#xff08;减少996&#xff09;&#xff0c;涌现了很多优秀的开发工具用以帮助开发者提高工作效率。现在我把我工作多年来收集实用的开发利器分享出…

蓝桥杯-常用STL(一)

常用STL &#x1f388;1.动态数组&#x1f388;2.vector的基础使用&#x1f52d;2.1引入库&#x1f52d;2.2构造一个动态数组&#x1f52d;2.3插入元素&#x1f52d;2.4获取长度并且访问元素&#x1f52d;2.5修改元素&#x1f52d;2.6删除元素&#x1f52d;2.7清空 &#x1f38…

关于监控的那些事,你有必要了解一下

监控在整个运维和产品生命周期中扮演着至关重要的角色。其目标是在应用的各个阶段&#xff0c;从程序设计、开发、部署到下线&#xff0c;实现事前预警、事中问题定位和事后问题分析的全方位服务。 一、监控的目的 监控贯穿应用的整个生命周期&#xff0c;服务对象主要包括技…

上个厕所的时间了解链路追踪基本概念

大家好&#xff0c;我是蓝胖子&#xff0c;随着微服务的普及&#xff0c;在面对日益复杂的架构和请求链路时&#xff0c;链路追踪技术就显得更加重要&#xff0c;今天我们花5分钟的时间&#xff0c;来掌握和链路追踪相关的基本概念。不会涉及到具体的技术框架和落地&#xff0c…

07.领域驱动设计:3种常见微服务架构模型的对比和分析

目录 1、概述 2、整洁架构 3、六边形架构 4、三种微服务架构模型的对比和分析 5、从三种架构模型看中台和微服务设计 5.1 中台建设要聚焦领域模型 5.2 微服务要有合理的架构分层 5.2.1 项目级微服务 5.2.2 企业级中台微服务 5.3 应用和资源的解耦与适配 6、总结 1、概…

性能测试分类及常用指标

性能测试是个综合的概述&#xff0c;性能测试指的是测试一种分类或多种分类&#xff0c;任何一具体分类&#xff0c;都是性能测试 一、性能测试常用分类 负载测试压力测试并发测试稳定性测试 性能测试分类还有其他类型比如&#xff1a;配置测试、容量测试等&#xff0c;前期…

AttributeError: ‘Plotter‘ object has no attribute ‘topicture‘

在以下网址找到自己的pytorch和cuda版本然后点击进入&#xff1a; https://nvidia-kaolin.s3.us-east-2.amazonaws.com/index.html 下载自己系统和python对应的最新版本 使用pip安装 pip install kaolin-0.14.0-cp38-cp38-linux_x86_64.whl

记录学习--vue发各种请求参数的请求

①发get请求&#xff0c;请求参数是数组&#xff0c;后端用RequestParam List<String>接收 axios.get(/user, {params: {ID: 12345,things: myThings ,}}) // 在参数后面加上&#xff08; &#xff09;&#xff0c;即可把数组变成字符串 // 请求变成 .../user?ID1234…

数据可视化 pycharts实现地理数据可视化(全球地图)

自用版 紧急整理一点可能要用的可视化代码&#xff0c;略粗糙 以后有机会再改 requirements&#xff1a; python3.6及以上pycharts1.9 数据格式为&#xff1a; 运行结果为&#xff1a; import pandas as pd from pyecharts.charts import Map, Timeline from pyecharts im…

【C/C++ 06】基数排序

基数排序是桶排序的一种&#xff0c;算法思路为&#xff1a; 利用队列进行数据收发创建一个队列数组&#xff0c;数组大小为10&#xff0c;每个元素都是一个队列&#xff0c;存储取模为1~9的数从低位到高位进行数据收发&#xff0c;完成排序适用于数据位不高的情况&#xff08…

C# IP v4转地址·地名 高德

需求: IPv4地址转地址 如&#xff1a;输入14.197.150.014&#xff0c;输出河北省石家庄市 SDK: 目前使用SDK为高德地图WebAPI 高德地图开放平台https://lbs.amap.com/ 可个人开发者使用&#xff0c;不过有配额限制。 WebAPI 免费配额调整公告https://lbs.amap.com/news/…

希尔伯特变换的在信号解调时的示例

1.希尔伯特变换的应用场景 希尔伯特变换&#xff0c;在数学上的含义是清晰的。它是一个数字移相器&#xff0c;可以把通过它的任何一个信号相移-90度。这个数学工具在信号解调时&#xff0c;会有非常有用的特性出现。可以看示例&#xff1a; 解释一下&#xff1a; 1.最上面的…

burp靶场--xss上篇【1-15】

burp靶场–xss https://portswigger.net/web-security/cross-site-scripting 1. 什么是xss: 跨站脚本 (XSS) 是一种通常出现在 Web 应用程序中的计算机安全漏洞。XSS 允许攻击者将恶意代码注入网站&#xff0c;然后在访问该网站的任何人的浏览器中执行该代码。这可能允许攻击…

视频转GIF动图实践, 支持长视频转GIF

背景 找了很多GIF动图制作的工具&#xff0c;比如将视频转成GIF, 或者将一系列图片转成GIF, 增加背景文案等等功能。很多收费或者用的一些三方库有点点卡顿&#xff0c;或者需要安装一个软件&#xff0c;所以就自己做一款纯前端页面级别的 视频转 GIF 动图工具。 最开始找到一…