ffmpeg把RTSP流分段录制成MP4,如果能把ffmpeg.exe改成ffmpeg.dll用,那音视频开发的难度直接就降一个维度啊

news/2025/4/1 5:13:51/

比如,原来我们要用ffmpeg录一段RTSP视频流转成MP4,我们有两种方案:

方案一:可以使用以下命令将rtsp流分段存储为mp4文件

ffmpeg -i rtsp://example.com/stream -vcodec copy -acodec aac -f segment -segment_time 3600 -reset_timestamps 1 -strftime 1 output_%Y-%m-%d_%H-%M-%S.mp4

ffmpeg将RTSP录像成mp4

方案二:可以直接调用ffmpeg库avcodec、avfilter代码,有门槛,要研究,用大模型生成一段代码看看:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <time.h>extern "C" {#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#include <libswscale/swscale.h>
}int main(int argc, char **argv) {AVFormatContext *pFormatCtx = NULL;int videoStream;AVCodecContext *pCodecCtx = NULL;AVCodec *pCodec = NULL;AVPacket packet;FILE *videoFile = NULL;struct SwsContext *img_convert_ctx;int64_t start_time, end_time, duration;double framerate = 25.0; // 帧率,需要根据实际情况调整int i, ret, got_output;uint8_t *outbuf[1024]; // 输出缓冲区大小,需要根据实际情况调整int outbuf_size; // 输出缓冲区大小,需要根据实际情况调整start_time = (int64_t)time(NULL); // 开始时间,单位为秒videoFile = fopen("output.ts", "wb"); // 输出文件名,需要根据实际情况调整if (!videoFile) {printf("Could not open output file for writing
");return -1;}outbuf_size = av_image_get_buffer_size((AVPixelFormat)AV_PIX_FMT_YUV420P, videoStream, 1920, 1080, 1); // 获取输出缓冲区大小,需要根据实际情况调整outbuf = (uint8_t*)av_malloc(outbuf_size); // 分配输出缓冲区,需要根据实际情况调整ret = avformat_new_stream(&pFormatCtx, NULL); // 创建视频流上下文,需要根据实际情况调整if (ret < 0) {printf("Error: could not create output format context
");exit(1);} else {ret = avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar); // 设置解码器参数,需要根据实际情况调整if (ret < 0) {printf("Error: could not initialize the codec context
");exit(1);} else {ret = avcodec_open2(pCodecCtx, pCodec, NULL); // 打开解码器,需要根据实际情况调整if (ret < 0) {printf("Error: Could not open codec
");exit(1);} else {while (1) { // 循环处理视频流数据,需要根据实际情况调整ret = av_read_frame(pFormatCtx, &packet); // 读取视频帧数据,需要根据实际情况调整if (ret == 0) { // 如果读取到数据包,则进行处理,需要根据实际情况调整// ... 在这里添加你的代码来处理视频帧数据 ...} else if (ret < 0 && ret != AVERROR(EAGAIN)) { // 如果发生错误,则打印错误信息并退出程序,需要根据实际情况调整char errorbuf[1024];av_strerror(ret, errorbuf, sizeof(errorbuf));printf("%s
", errorbuf);exit(1);} else if (ret == AVERROR(EAGAIN)) { // 如果发生EAGAIN错误,则等待一段时间后继续读取数据包,需要根据实际情况调整usleep(200000); // 等待200ms,单位为微秒,需要根据实际情况调整} else if (ret == AVERROR_EOF) { // 如果发生EOF错误,则表示已经读取完所有数据包,需要根据实际情况调整break; // 结束循环,需要根据实际情况调整} else if (ret < 0 && ret != AVERROR(EAGAIN)) { // 如果发生其他错误,则打印错误信息并退出程序,需要根据实际情况调整char errorbuf[1024];av_strerror(ret, errorbuf, sizeof(errorbuf));printf("%s
", errorbuf);exit(1);} else if (ret >= 0) { // 如果成功读取到数据包,则进行处理,需要根据实际情况调整// ... 在这里添加你的代码来处理视频帧数据 ...} else { // 如果发生未知错误,则打印错误信息并退出程序,需要根据实际情况调整char errorbuf[1024];av_strerror(ret, errorbuf, sizeof(errorbuf));printf("%s
", errorbuf);exit(1);}} // while循环结束条件 ... 在这里添加你的代码 ... } } } } /* end of main() */
  • 方案一通过exec调用,但是有一个弊端就是不好控制,不能自动重连,进程管理需要不断自己维护;
  • 方案二就是开发成本较高,太复杂了,不同级别的开发写出来的效果参差不齐;

有没有一个能把方案一和方案二结合起来的方法?

有,他来了:EasyAVFilter!简单的几个接口,就能解决ffmpeg开发门槛的问题:

方法名称说明
EasyAVFilter_Create创建句柄,相当于创建了一个ffmpeg.exe
EasyAVFilter_Release释放句柄
EasyAVFilter_SetCallback设置回调函数和自定义指针,回调过程中的各种媒体信息/连接信息/转码进度
EasyAVFilter_AddInput添加输入参数(源地址)
EasyAVFilter_AddFilter添加中间参数,如:转码,兼容ffmpeg命令所有参数(例如-vcodec copy -acodec aac)
EasyAVFilter_SetOutput设置输出参数(目标地址)
EasyAVFilter_GetFilters获取所有参数(review参数输入是否正确)
EasyAVFilter_Start开始工作
EasyAVFilter_Stop停止工作

详细信息可以直接看https://www.easydarwin.org/tools/153.html,具体用法和场景,后续逐步介绍;


http://www.ppmy.cn/news/1077030.html

相关文章

算法 -汉诺塔,哈夫曼编码

有三个柱子,分别为 from、buffer、to。需要将 from 上的圆盘全部移动到 to 上,并且要保证小圆盘始终在大圆盘上。 这是一个经典的递归问题,分为三步求解: ① 将 n-1 个圆盘从 from -> buffer ② 将 1 个圆盘从 from -> to ③ 将 n-1 个圆盘从 buffer -> to 如果…

docker 容器pip、git安装异常;容器内web对外端口ping不通

1、docker 容器pip、git安装异常 错误信息&#xff1a; git clone https://github.com/vllm-project/vllm.git Cloning into ‘vllm’… fatal: unable to access ‘https://github.com/vllm-project/vllm.git/’: Failed to connect to 127.0.0.1 port 10808: Connection ref…

AI绘画的一些网址收集

1、负面tag词语收集 https://www.bilibili.com/read/cv19834742 https://y3if3fk7ce.feishu.cn/docx/VOZMdoib8oY7xVxVoYbcA8m1nld 2、如何写关键词 https://y3if3fk7ce.feishu.cn/docx/KqEMdhJigoFY8fxc9TPcwMninKf 3、关键词 https://zhuanlan.zhihu.com/p/573340345 模板词…

QT DAY 2

window.cpp #include "window.h" #include<QDebug> #include<QIcon> Window::Window(QWidget *parent) //构造函数的定义: QWidget(parent) //显性调用父类的构造函数 {//this->resize(430,330);this->resize(QSize(800,600));// this…

是否在业务中使用大语言模型?

ChatGPT取得了巨大的成功&#xff0c;在短短一个月内就获得了1亿用户&#xff0c;并激发了企业和专业人士对如何在他们的组织中利用这一工具的兴趣和好奇心。 但LLM究竟是什么&#xff0c;它们如何使你的企业受益?它只是一种炒作&#xff0c;还是会长期存在? 在这篇文章中我…

gitHooks使用教程

1. 安装所需依赖 npm install eslint prettier husky lint-staged --save-dev 2.初始化 husky npx husky-init && npm install 这将创建一个 .husky/ 目录&#xff0c;并且在其中包含一个示例的 pre-commit 文件。 3.设置 pre-commit 钩子 npx husky add .husky/…

(已解决)ModuleNotFoundError: No module named ‘braceexpand‘

问题描述 import braceexpand, yaml ModuleNotFoundError: No module named braceexpand 解决办法 pip install braceexpand 参考链接https://www.roseindia.net/answers/viewqa/pythonquestions/52026-ModuleNotFoundError-No-module-named-braceexpand.html 其他问题-1 im…

线性代数的学习和整理16:什么是各种空间(类型),向量空间,距离(类型)?

目录 1 空间相关的群&#xff0c;环&#xff0c;域&#xff0c;集合&#xff0c;空间的预备知识 1.1&#xff1a;群&#xff0c;环&#xff0c;域&#xff0c;集合&#xff0c;空间的定义&#xff08;表示不懂&#xff0c;只是做个标记&#xff09; 2 空间 2.1 各种空间概念…