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

比如,原来我们要用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代码,有门槛,要研究,用大模型生成一段代码看看:

cpp 复制代码
#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,具体用法和场景,后续逐步介绍;

相关推荐
superconvert12 小时前
主流流媒体的综合性能大 PK ( smart_rtmpd, srs, zlm, nginx rtmp )
websocket·ffmpeg·webrtc·hevc·rtmp·h264·hls·dash·rtsp·srt·flv
cuijiecheng201818 小时前
音视频入门基础:AAC专题(8)——FFmpeg源码中计算AAC裸流AVStream的time_base的实现
ffmpeg·音视频·aac
0点51 胜18 小时前
[ffmpeg] 视频格式转换
ffmpeg
Jerry 二河小鱼1 天前
在Linux中安装FFmpeg
linux·运维·服务器·ffmpeg
0点51 胜2 天前
[ffmpeg] 音视频编码
ffmpeg·音视频
0点51 胜2 天前
[ffmpeg]音频格式转换
开发语言·c++·ffmpeg
PlumCarefree3 天前
基于鸿蒙API10的RTSP播放器(五:拖动底部视频滑轨实现跳转)
华为·ffmpeg·音视频
LuckyInn3 天前
从安装ffmpeg开始,把一个视频按照每秒30帧fps剪切为图片
ffmpeg·音视频
cuijiecheng20183 天前
FFmpeg源码:skip_bits、skip_bits1、show_bits函数分析
ffmpeg
__Destiny__3 天前
视频格式转为mp4(使用ffmpeg)
ffmpeg·视频编解码