当前位置: 首页 > news >正文

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文件

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

PostgreSQL LIMIT 子句

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

误删分区后的数据拯救:双管齐下恢复策略

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

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&#xff1a;Linux&#xff1a;基础IO&#xff08;三.软硬链接、动态库和静态库、动精态库的制作和加载&#xff09; 文章目录 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封装点击列筛选行数据功能,支持筛选,搜索,排序功能

数据少的话&#xff0c;可以前端实现&#xff0c;如果多的话&#xff0c;建议还是请求接口比较合理父组件&#xff1a; <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、服务提供者 &#xff08;1&#xff09;启动类添加注解EnableDubbo &#xff08;2&#xff09;服务类添加注解DubboService &#xff08;3&#xff09;配置文件…...

昇思25天学习打卡营第12天| 基于MindNLP+MusicGen生成自己的个性化音乐

之前都是看图文类的东西&#xff0c;今天体验一点不一样的。来点听力的内容。 mindspore有音乐生成模型MusicGen&#xff0c;MusicGen支持两种生成模式&#xff1a;贪心&#xff08;greedy&#xff09;和采样&#xff08;sampling&#xff09;。在实际执行过程中&#xff0c;采…...

代理设计模式和装饰器设计模式的区别

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

[Microsoft Office]Word设置页码从第二页开始为1

目录 第一步&#xff1a;设置页码格式 第二步&#xff1a;设置“起始页码”为0 第三步&#xff1a;双击页码&#xff0c;出现“页脚”提示 第四步&#xff1a;选中“首页不同” 第一步&#xff1a;设置页码格式 第二步&#xff1a;设置“起始页码”为0 第三步&#xff1a;双…...

【C++】日期类

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

力扣热100 滑动窗口

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

三万字带你一遍跑通uer

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

Ubuntu24.04LTS基础软件下载

librewolf: deb文件link 作用&#xff1a;访问github&#xff0c;无痕浏览&#xff0c;这个速度&#xff0c;不指望了 vscodium: 从deb安装&#xff0c;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从入门到精通系列 前言&#xff1a;笔者从事于NLPLLM的对话智能机器人的相关行业&#xff0c;现在的大模型的技术日新月异&#xff0c;传统的NLP业务显然是要被淘汰的&#xff0c;那么这也是我着笔写这一系列文章的初衷。本系列将由浅到深&#xff0c;结合实际代码案例&…...

用数组手搓一个小顶堆

堆默认从数组下标为1开始存储。 const int N201000; int heap[N]; int len; 插入操作&#xff1a; 将元素插入到堆的末尾位置向上调整。 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…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

YSYX学习记录(八)

C语言&#xff0c;练习0&#xff1a; 先创建一个文件夹&#xff0c;我用的是物理机&#xff1a; 安装build-essential 练习1&#xff1a; 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件&#xff0c;随机修改或删除一部分&#xff0c;之后…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

1.3 VSCode安装与环境配置

进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件&#xff0c;然后打开终端&#xff0c;进入下载文件夹&#xff0c;键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

使用van-uploader 的UI组件,结合vue2如何实现图片上传组件的封装

以下是基于 vant-ui&#xff08;适配 Vue2 版本 &#xff09;实现截图中照片上传预览、删除功能&#xff0c;并封装成可复用组件的完整代码&#xff0c;包含样式和逻辑实现&#xff0c;可直接在 Vue2 项目中使用&#xff1a; 1. 封装的图片上传组件 ImageUploader.vue <te…...

linux 下常用变更-8

1、删除普通用户 查询用户初始UID和GIDls -l /home/ ###家目录中查看UID cat /etc/group ###此文件查看GID删除用户1.编辑文件 /etc/passwd 找到对应的行&#xff0c;YW343:x:0:0::/home/YW343:/bin/bash 2.将标红的位置修改为用户对应初始UID和GID&#xff1a; YW3…...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

JavaScript 数据类型详解

JavaScript 数据类型详解 JavaScript 数据类型分为 原始类型&#xff08;Primitive&#xff09; 和 对象类型&#xff08;Object&#xff09; 两大类&#xff0c;共 8 种&#xff08;ES11&#xff09;&#xff1a; 一、原始类型&#xff08;7种&#xff09; 1. undefined 定…...

【JavaSE】多线程基础学习笔记

多线程基础 -线程相关概念 程序&#xff08;Program&#xff09; 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序&#xff0c;比如我们使用QQ&#xff0c;就启动了一个进程&#xff0c;操作系统就会为该进程分配内存…...