FFMpeg rtmp 无压缩推送本地yuv文件 压缩推送本地yuv文件
可以借鉴的:C++使用FFmpeg实现YUV数据编码转视频文件_C 语言_脚本之家
yuv文件下载地址:YUV Sequences
无压缩的方式推送本地yuv文件 代码:
#include <stdio.h>
#include <unistd.h>
#include <iostream>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
};
using namespace std;const char* input_file = "/mnt/hgfs/shareVM/BigBuckBunny_CIF_24fps.yuv";
const char* output_rtmp_url = "rtmp://10.10.18.94:1935/live/test";int main(int argc, char *argv[]) {AVFormatContext *pFormatCtx = nullptr;AVOutputFormat *fmt = nullptr;AVStream *video_st = nullptr;AVCodecContext *pCodecCtx = nullptr;AVCodec *pCodec = nullptr;uint8_t *picture_buf = nullptr;int size;//打开视频文件FILE *in_file = fopen(input_file, "rb");if (!in_file) {cout << "can not open file!" << endl;return -1;}//[1] --注册所有ffmpeg组件avcodec_register_all();av_register_all();//[2] --初始化AVFormatContext结构体,根据文件名获取到合适的封装格式avformat_alloc_output_context2(&pFormatCtx, NULL, "flv", output_rtmp_url);fmt = pFormatCtx->oformat;//[3] --打开文件if (avio_open(&pFormatCtx->pb, output_rtmp_url, AVIO_FLAG_READ_WRITE)) {cout << "output file open fail!";return -1;}//[3]//[4] --初始化视频码流video_st = avformat_new_stream(pFormatCtx, 0);if (video_st == NULL){printf("failed allocating output stram\n");return -1;}video_st->time_base.num = 1;video_st->time_base.den = 25;//[4]//[5] --编码器Context设置参数pCodecCtx = video_st->codec;pCodecCtx->codec_id = fmt->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;pCodecCtx->width = 352;pCodecCtx->height = 288;pCodecCtx->time_base = {1, 25};pCodecCtx->framerate = {25, 1};pCodecCtx->bit_rate = 400000;pCodecCtx->gop_size = 50;//[5]//[6] --寻找编码器并打开编码器pCodec = avcodec_find_encoder(AV_CODEC_ID_FLV1);if (!pCodec){cout << "no right encoder!" << endl;return -1;}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){cout << "open encoder fail!" << endl;return -1;}//[6]//输出格式信息av_dump_format(pFormatCtx, 0, output_rtmp_url, 1);//初始化帧AVFrame *picture = av_frame_alloc();picture->width = pCodecCtx->width;picture->height = pCodecCtx->height;picture->format = pCodecCtx->pix_fmt;av_frame_get_buffer(picture, 32);size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);picture_buf = (uint8_t*)av_malloc(size);avpicture_fill((AVPicture*)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);//[7] --写头文件avformat_write_header(pFormatCtx, NULL);//[7]//[8] --循环编码每一帧AVPacket pkt; //创建已编码帧int frame_count = 0;while (!feof(in_file)) {// 读取一帧 YUV 数据uint8_t yuv_buf[pCodecCtx->width * pCodecCtx->height * 3 / 2];size_t read_size = fread(yuv_buf, 1, pCodecCtx->width * pCodecCtx->height * 3 / 2, in_file);if (read_size <= 0) {break;}av_init_packet(&pkt);pkt.data = NULL;pkt.size = 0;// 将 YUV 数据编码为 H.264memcpy(picture->data[0], yuv_buf, pCodecCtx->width * pCodecCtx->height);memcpy(picture->data[1], yuv_buf + pCodecCtx->width * pCodecCtx->height, pCodecCtx->width * pCodecCtx->height / 4);memcpy(picture->data[2], yuv_buf + pCodecCtx->width * pCodecCtx->height * 5 / 4, pCodecCtx->width * pCodecCtx->height / 4);picture->pts = frame_count;int got_picture = 0;//编码int ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture);if (ret < 0){cout << "encoder fail!" << endl;return -1;}if (!got_picture){ret = 0;break;}cout << "encoder success! " <<picture->pts<< endl;// parpare packet for muxingpkt.stream_index = video_st->index;pkt.pts = frame_count * (pCodecCtx->time_base.den) / ((pCodecCtx->time_base.num) * 25);pkt.dts = pkt.pts;av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base);pkt.pos = -1;ret = av_interleaved_write_frame(pFormatCtx, &pkt);if(ret < 0)break;av_free_packet(&pkt);frame_count++;usleep(30*1000);}//[8]//[9] --写文件尾av_write_trailer(pFormatCtx);//[9]//释放内存if (video_st){avcodec_close(video_st->codec);av_free(picture);av_free(picture_buf);}if (pFormatCtx){avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);}fclose(in_file);return 0;
}
压缩一半的方式推送本地yuv文件 代码:
#include <stdio.h>
#include <unistd.h>
#include <iostream>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
};
using namespace std;const char* input_file = "/mnt/hgfs/shareVM/BigBuckBunny_CIF_24fps.yuv";
const char* output_rtmp_url = "rtmp://10.10.18.94:1935/live/test";
const int fps = 25;int main(int argc, char *argv[]) {AVFormatContext *pFormatCtx = nullptr;AVCodecContext *pCodecCtx = nullptr;//打开视频文件FILE *in_file = fopen(input_file, "rb");if (!in_file) {cout << "can not open file!" << endl;return -1;}//[1] --注册所有ffmpeg组件avcodec_register_all();av_register_all();//[2] --初始化AVFormatContext结构体,根据文件名获取到合适的封装格式avformat_alloc_output_context2(&pFormatCtx, NULL, "flv", output_rtmp_url);AVOutputFormat *fmt = pFormatCtx->oformat;//[3] --打开文件if (avio_open(&pFormatCtx->pb, output_rtmp_url, AVIO_FLAG_READ_WRITE)) {cout << "output file open fail!";return -1;}//[3]//[4] --初始化视频码流AVStream *video_st = avformat_new_stream(pFormatCtx, 0);if (video_st == NULL){printf("failed allocating output stram\n");avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);return -1;}video_st->time_base.num = 1;video_st->time_base.den = fps;//[4]struct SwsContext* swsContext = sws_getContext(352, 288, AV_PIX_FMT_YUV420P,352 / 2, 288 / 2, AV_PIX_FMT_YUV420P,SWS_BICUBIC, NULL, NULL, NULL);if(swsContext == NULL) {printf("failed sws_getContext\n");avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);return -1;}//[5] --编码器Context设置参数pCodecCtx = video_st->codec;pCodecCtx->codec_id = fmt->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;pCodecCtx->width = 352 / 2;pCodecCtx->height = 288 / 2;pCodecCtx->time_base = {1, fps};pCodecCtx->framerate = {fps, 1};pCodecCtx->bit_rate = 400000;pCodecCtx->gop_size = 50;//[5]//[6] --寻找编码器并打开编码器AVCodec *pCodec = avcodec_find_encoder(AV_CODEC_ID_FLV1);if (!pCodec){cout << "no right encoder!" << endl;return -1;}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){cout << "open encoder fail!" << endl;avformat_free_context(pFormatCtx);return -1;}//[6]//输出格式信息av_dump_format(pFormatCtx, 0, output_rtmp_url, 1);//初始化帧AVFrame *originalFrame = av_frame_alloc();originalFrame->width = 352;originalFrame->height = 288;originalFrame->format = pCodecCtx->pix_fmt;av_frame_get_buffer(originalFrame, 32);int originalFrame_size = avpicture_get_size(pCodecCtx->pix_fmt, 352, 288);uint8_t* originalFrame_buf = (uint8_t*)av_malloc(originalFrame_size);avpicture_fill((AVPicture*)originalFrame, originalFrame_buf, pCodecCtx->pix_fmt, 352, 288);AVFrame *compressFrame = av_frame_alloc();compressFrame->width = pCodecCtx->width;compressFrame->height = pCodecCtx->height;compressFrame->format = pCodecCtx->pix_fmt;av_frame_get_buffer(compressFrame, 32);int compressFrame_size = avpicture_get_size((AVPixelFormat)compressFrame->format, compressFrame->width, compressFrame->height);uint8_t* compressFrame_buf = (uint8_t*)av_malloc(compressFrame_size);avpicture_fill((AVPicture*)compressFrame, compressFrame_buf, (AVPixelFormat)compressFrame->format, compressFrame->width, compressFrame->height);//[7] --写头文件avformat_write_header(pFormatCtx, NULL);//[7] //[8] --循环编码每一帧AVPacket pkt; //创建已编码帧int frame_count = 0;while (!feof(in_file)) {// 读取一帧 YUV 数据uint8_t yuv_buf[352 * 288 * 3 / 2];size_t read_size = fread(yuv_buf, 1, 352 * 288 * 3 / 2, in_file);if (read_size <= 0) {break;}av_init_packet(&pkt);pkt.data = NULL;pkt.size = 0;// 将 YUV 数据编码为 H.264memcpy(originalFrame->data[0], yuv_buf, 352 * 288);memcpy(originalFrame->data[1], yuv_buf + 352 * 288, 352 * 288 / 4);memcpy(originalFrame->data[2], yuv_buf + 352 * 288 * 5 / 4, 352 * 288 / 4);originalFrame->pts = compressFrame->pts = frame_count;int got_picture = 0;sws_scale(swsContext, (const uint8_t* const*)originalFrame->data, originalFrame->linesize, 0,288, compressFrame->data, compressFrame->linesize);//编码int ret = avcodec_encode_video2(pCodecCtx, &pkt, compressFrame, &got_picture);if (ret < 0){cout << "encoder fail!" << endl;return -1;}if (got_picture){cout << "encoder success! " <<compressFrame->pts<< endl;// parpare packet for muxingpkt.stream_index = video_st->index;pkt.pts = frame_count * (pCodecCtx->time_base.den) / ((pCodecCtx->time_base.num) * fps);pkt.dts = pkt.pts;av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base);pkt.pos = -1;ret = av_interleaved_write_frame(pFormatCtx, &pkt);if(ret < 0)break;}av_free_packet(&pkt);frame_count++;usleep(30 * 1000);}//[8]//[9] --写文件尾av_write_trailer(pFormatCtx);//[9]//释放内存if (video_st){avcodec_close(video_st->codec);av_free(originalFrame);av_free(originalFrame_buf);av_free(compressFrame);av_free(compressFrame_buf);}if (pFormatCtx){avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);}fclose(in_file);return 0;
}
相关文章:

FFMpeg rtmp 无压缩推送本地yuv文件 压缩推送本地yuv文件
可以借鉴的:C使用FFmpeg实现YUV数据编码转视频文件_C 语言_脚本之家 yuv文件下载地址:YUV Sequences 无压缩的方式推送本地yuv文件 代码: #include <stdio.h> #include <unistd.h> #include <iostream> extern "C&…...

PostgreSQL LIMIT 子句
PostgreSQL LIMIT 子句 PostgreSQL 是一种功能强大的开源对象关系数据库管理系统,广泛用于各种应用中。在处理大量数据时,我们通常只需要检索部分记录,而不是整个数据集。这时,LIMIT 子句就变得非常有用。本文将详细介绍 Postgre…...

误删分区后的数据拯救:双管齐下恢复策略
在数字化时代,数据的价值日益凸显,而误删分区作为常见的数据安全威胁之一,常常让用户措手不及。本文将深入探讨误删分区的现象,并为您揭示两种高效的数据恢复方案,旨在帮助您在最短时间内找回失去的数据,同…...

git 添加本地分支, clean
//以develop为源创建本地分支fromdevelop git checkout -b fromdevelop git add . git commit -m "local" git checkout -b local/dev //切换到远程分支. git checkout dev git clean_git clean -f -d-CSDN博客 git clean -f -d #删除当前目录下没有被track…...

Linux:进程间通信(一.初识进程间通信、匿名管道与命名管道、共享内存)
上次结束了基础IO:Linux:基础IO(三.软硬链接、动态库和静态库、动精态库的制作和加载) 文章目录 1.认识进程间通信2.管道2.1匿名管道2.2pipe()函数 —创建匿名管道2.3匿名管道的四种情况2.4管道的特征 3.基于管道的进程池设计4.命…...

QML-各类布局
Colunm布局 Column{id:colspacing: 30Repeater{id:repmodel: ListModel{}Button{width: 100height: 50text: "btn"index}}//开始时候移动move: Transition {NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce }}//添加时变化add:Transi…...

el-table封装点击列筛选行数据功能,支持筛选,搜索,排序功能
数据少的话,可以前端实现,如果多的话,建议还是请求接口比较合理父组件: <template> <div class"home"> <!-- <img alt"Vue logo" src"../assets/logo.png"> <HelloWorld …...

【SpringBoot3学习 | 第1篇】SpringBoot3介绍与配置文件
文章目录 前言 一. SpringBoot3介绍1.1 SpringBoot项目创建1. 创建Maven工程2. 添加依赖(springboot父工程依赖 , web启动器依赖)3. 编写启动引导类(springboot项目运行的入口)4. 编写处理器Controller5. 启动项目 1.2 项目理解1. 依赖不需要写版本原因2. 启动器(Starter)3. Sp…...

SpringBoot整合Dubbo的快速使用教程
目录 一、什么是Dubbo? 二、SpringBoot整合Dubbo 1、父工程引入依赖 2、各个Dubbo服务子模块引入依赖 3、服务提供者 (1)启动类添加注解EnableDubbo (2)服务类添加注解DubboService (3)配置文件…...

昇思25天学习打卡营第12天| 基于MindNLP+MusicGen生成自己的个性化音乐
之前都是看图文类的东西,今天体验一点不一样的。来点听力的内容。 mindspore有音乐生成模型MusicGen,MusicGen支持两种生成模式:贪心(greedy)和采样(sampling)。在实际执行过程中,采…...

代理设计模式和装饰器设计模式的区别
代理设计模式: 作用:为目标(原始对象)增加功能(额外功能,拓展功能) 三种经典应用场景: 1:给原始对象增加额外功能(spring添加事务,Mybatis通过代理实现缓存功能等等) 2:远程代理(网络通信,输出传输(RPC,D…...

[Microsoft Office]Word设置页码从第二页开始为1
目录 第一步:设置页码格式 第二步:设置“起始页码”为0 第三步:双击页码,出现“页脚”提示 第四步:选中“首页不同” 第一步:设置页码格式 第二步:设置“起始页码”为0 第三步:双…...

【C++】日期类
鼠鼠实现了一个日期类,用来练习印证前几篇博客介绍的内容!! 目录 1.日期类的定义 2.得到某年某月的天数 3.检查日期是否合法 4.(全缺省)构造函数 5.拷贝构造函数 6.析构函数 7.赋值运算符重载 8.>运算符重…...

力扣热100 滑动窗口
这里写目录标题 3. 无重复字符的最长子串438. 找到字符串中所有字母异位词 3. 无重复字符的最长子串 左右指针left和right里面的字符串一直是没有重复的 class Solution:def lengthOfLongestSubstring(self, s: str) -> int:# 左右指针leftright0ans0#初始化结果tablecolle…...

三万字带你一遍跑通uer
三万字带你一遍跑通uer 参考文档 今天给大家介绍个非常强大的项目uer,集成了许多可以做自然语言的东西,效果的话也非常好,很适合企业级的应用! 1. 先将项目uer从github拉取下来(zip或git都ok) 2. 用pycha…...

Ubuntu24.04LTS基础软件下载
librewolf: deb文件link 作用:访问github,无痕浏览,这个速度,不指望了 vscodium: 从deb安装,ubuntu sudo dpkg -i xxx.debpaste-image 插件替代 markdown wps: libreoffice: 替换USTC源 sudo nano /etc/apt/sourc…...

SQLAlchemy配置连接多个数据库
1.定义配置项 首先定义两个数据库的配置信息 # PostgreSQL database configuration DB_USERNAMEpostgres DB_PASSWORDpassord DB_HOST127.0.0.1 DB_PORT5432 DB_DATABASEtest# mysql database configuration DB_USERNAME_MYSQLroot DB_PASSWORD_MYSQLpassword DB_HOST_MYSQL127…...

NLP+LLM从入门到精通系列
NLPLLM从入门到精通系列 前言:笔者从事于NLPLLM的对话智能机器人的相关行业,现在的大模型的技术日新月异,传统的NLP业务显然是要被淘汰的,那么这也是我着笔写这一系列文章的初衷。本系列将由浅到深,结合实际代码案例&…...

用数组手搓一个小顶堆
堆默认从数组下标为1开始存储。 const int N201000; int heap[N]; int len; 插入操作: 将元素插入到堆的末尾位置向上调整。 void up(int k){while(k>1&&heap[k/2]>heap[k]){swap(heap[k],heap[k/2]);k/2;} } //len为当前存在元素长度 void Inser…...

【Linux开发】基于ALSA库实现音量调节
基于ALSA库实现音量调节 ALSA库实现音量调节1、使用alsamixer工具查看音频接口2、完整代码2.1、snd_mixer_open2.2、snd_mixer_attach、2.3、snd_mixer_selem_register2.4、snd_mixer_load2.5、snd_mixer_first_elem/snd_mixer_elem_next2.6、snd_mixer_selem_get_playback_vol…...

代理IP在未来将面临哪些挑战?
今天我们来聊聊代理IP在未来可能会面临的挑战。虽然代理IP技术目前应用广泛,但随着科技的发展和网络环境的变化,代理IP也将面临一些新的挑战。让我们一起来看看这些挑战是什么吧! 1. 更严格的网络封锁和检测 现代社会各行各业都在飞速发展&…...

FineBI在线学习资源-数据处理
FineBI在线学习资源汇总: 学习资源 视频课程 帮助文档 问答 数据处理学习文档: 相关资料: 故事背景概述-https://help.fanruan.com/finebi6.0/doc-view-1789.html 基础表处理-https://help.fanruan.com/finebi6.0/doc-view-1791.html …...

【代码随想录算法训练营第37期 第四十五天 | LeetCode198.打家劫舍、213.打家劫舍II、337.打家劫舍III】
代码随想录算法训练营第37期 第四十五天 | LeetCode198.打家劫舍、213.打家劫舍II、337.打家劫舍III 一、198.打家劫舍 解题代码C: class Solution { public:int rob(vector<int>& nums) {if (nums.size() 0) return 0;if (nums.size() 1) return num…...

Elasticsearch查询上下文和_source
查询上下文 {"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation"…...

golang实现网卡流量监控
获取当前时刻一分钟前的网卡流量排序 package mainimport ("fmt""github.com/mackerelio/go-osstat/network""log""net/http""sort""strconv""time" )var arr []map[string]int var arr2 []map[string]…...

技术分享:直播平台如何开发并接入美颜SDK
本篇文章,笔者将分享直播平台如何开发并接入美颜SDK的技术细节与步骤。 一、选择合适的美颜SDK 首先,选择一款适合的美颜SDK非常重要。市面上有很多优秀的美颜SDK供应商,选择时应考虑以下因素: 功能丰富性:支持美白…...

左耳听风_114_113_Go编程模式修饰器
你好,我是陈浩,我名多尔多house.之前呢我写过一篇文章叫做python修饰器的函数式编程。 那这种模式呢可以很轻松的把一些函数啊装配到另外一些函数上。 让你的代码呢更加简单,也可以让一些小功能性的代码复用性更高。 让代码中的函数呢可以…...

Java实习手册(小白也看得懂)
秃狼说 距离俺发布的学习路线已经六个月了,那我给小伙伴的学习周期是四五个月左右,我相信大多的小伙伴已经学习的差不多了。正好赶上暑期实习的阶段,在暑期找到实习就成为暑期的头等大事。 实习经验在校招的起到决定性的作用,所…...

Elasticsearch 分析器(Analyzer)的作用和配置
在Elasticsearch中,分析器(Analyzer)是文本处理的核心组件,它负责将输入的文本转换为可用于搜索和索引的词项(tokens)。这一过程涉及多个步骤,包括字符过滤、分词和标记过滤,共同决定…...

SpringBoot(一)创建一个简单的SpringBoot工程
Spring框架常用注解简单介绍 SpringMVC常用注解简单介绍 SpringBoot(一)创建一个简单的SpringBoot工程 SpringBoot(二)SpringBoot多环境配置 SpringBoot(三)SpringBoot整合MyBatis SpringBoot(四…...