资源网官网,淄博seo费用,wordpress 安装主体,百度关键词购买比如#xff0c;原来我们要用ffmpeg录一段RTSP视频流转成MP4#xff0c;我们有两种方案#xff1a;
方案一#xff1a;可以使用以下命令将rtsp流分段存储为mp4文件 ffmpeg -i rtsp://example.com/stream -vcodec copy -acodec aac -f segment -segment_time 3600 -reset_t…比如原来我们要用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库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.hextern 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.exeEasyAVFilter_Release释放句柄EasyAVFilter_SetCallback设置回调函数和自定义指针回调过程中的各种媒体信息/连接信息/转码进度EasyAVFilter_AddInput添加输入参数(源地址)EasyAVFilter_AddFilter添加中间参数,如:转码兼容ffmpeg命令所有参数例如-vcodec copy -acodec aacEasyAVFilter_SetOutput设置输出参数(目标地址)EasyAVFilter_GetFilters获取所有参数review参数输入是否正确EasyAVFilter_Start开始工作EasyAVFilter_Stop停止工作
详细信息可以直接看https://www.easydarwin.org/tools/153.html具体用法和场景后续逐步介绍