首页 新闻 会员 周边

FFmpeg.AutoGen

0
悬赏园豆:5 [已解决问题] 解决于 2023-09-14 12:51

跪求FFmpeg.AutoGen 播放rtsp流并且保存本地的demo(调用API实现),保存的视频文件能用windows播放器播放

搬砖的L先生的主页 搬砖的L先生 | 初学一级 | 园豆:2
提问于:2023-08-17 15:50
< >
分享
最佳答案
0

当涉及到基于 FFmpeg 的视频流处理和保存时,你可以使用 FFmpeg.AutoGen 库来实现。以下是一个简单的示例,展示了如何使用 FFmpeg.AutoGen 来播放 RTSP 流并将其保存为本地文件,以便 Windows 播放器可以播放。

首先,确保你已经添加了 FFmpeg.AutoGen 库到你的项目中。你可以通过 NuGet 包管理器来添加这个库。

然后,你可以使用以下示例代码作为起点:

csharp

using System;
using System.IO;
using FFmpeg.AutoGen;

class Program
{
static void Main(string[] args)
{
// Replace with the RTSP stream URL
string rtspUrl = "rtsp://your_rtsp_stream_url_here";

    FFmpegBinariesHelper.RegisterFFmpegBinaries();

    ffmpeg.avformat_network_init();

    AVFormatContext* inputFormatContext = null;
    AVFormatContext* outputFormatContext = null;

    if (ffmpeg.avformat_open_input(&inputFormatContext, rtspUrl, null, null) != 0)
    {
        Console.WriteLine("Failed to open input format context.");
        return;
    }

    if (ffmpeg.avformat_find_stream_info(inputFormatContext, null) < 0)
    {
        Console.WriteLine("Failed to find stream information.");
        return;
    }

    ffmpeg.avformat_alloc_output_context2(&outputFormatContext, null, null, "output.mp4");
    if (outputFormatContext == null)
    {
        Console.WriteLine("Failed to allocate output format context.");
        return;
    }

    AVStream* inStream = inputFormatContext->streams[0];
    AVStream* outStream = ffmpeg.avformat_new_stream(outputFormatContext, null);

    ffmpeg.avcodec_parameters_copy(outStream->codecpar, inStream->codecpar);
    outStream->time_base = inStream->time_base;

    if (ffmpeg.avio_open(&outputFormatContext->pb, "output.mp4", ffmpeg.AVIO_FLAG_WRITE) < 0)
    {
        Console.WriteLine("Failed to open output file.");
        return;
    }

    if (ffmpeg.avformat_write_header(outputFormatContext, null) < 0)
    {
        Console.WriteLine("Failed to write header.");
        return;
    }

    AVPacket packet;
    ffmpeg.av_init_packet(&packet);

    while (ffmpeg.av_read_frame(inputFormatContext, &packet) == 0)
    {
        if (packet.stream_index == 0)
        {
            packet.stream_index = 0;
            ffmpeg.av_write_frame(outputFormatContext, &packet);
        }

        ffmpeg.av_packet_unref(&packet);
    }

    ffmpeg.av_write_trailer(outputFormatContext);

    ffmpeg.avformat_close_input(&inputFormatContext);

    ffmpeg.avio_close(outputFormatContext->pb);
    ffmpeg.avformat_free_context(outputFormatContext);

    ffmpeg.avformat_network_deinit();
}

}
请注意,这只是一个简单的示例,并且可能需要根据你的需求进行进一步的调整。确保替换示例中的 rtspUrl 为实际的 RTSP 流 URL,并根据需要修改输出文件名。在实际项目中,你可能还需要添加错误处理和其他细节。

此外,由于 FFmpeg 和 FFmpeg.AutoGen 是强大且复杂的库,建议在使用之前查阅相关的文档和资料,以确保你正确地使用了库中的函数和结构。

收获园豆:5
Technologyforgood | 大侠五级 |园豆:5992 | 2023-08-17 22:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册