Skip to content

Custom FFmpeg Process

This guide will provide an example to use FFmpeg to store the RTMP stream as an MP4 archive.

Add the required packages

dotnet add package LiveStreamingServerNet
dotnet add package LiveStreamingServerNet.StreamProcessor

Configure Your Live Streaming Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using LiveStreamingServerNet;
using LiveStreamingServerNet.Networking;
using LiveStreamingServerNet.StreamProcessor.FFmpeg.Contracts;
using LiveStreamingServerNet.StreamProcessor.Installer;
using LiveStreamingServerNet.StreamProcessor.Utilities;
using Microsoft.Extensions.Logging;
using System.Net;

using var liveStreamingServer = LiveStreamingServerBuilder.Create()
    .ConfigureRtmpServer(options => options
        .AddStreamProcessor()
        .AddFFmpeg(options =>
        {
            options.Name = "mp4-archive";
            options.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
            options.FFmpegArguments = "-i {inputPath} -c:v libx264 -c:a aac -preset ultrafast -f mp4 {outputPath}";
            options.OutputPathResolver = new Mp4OutputPathResolver(Path.Combine(Directory.GetCurrentDirectory(), "mp4-archive"));
        })
    )
    .ConfigureLogging(options => options.AddConsole().SetMinimumLevel(LogLevel.Debug))
    .Build();

await liveStreamingServer.RunAsync(new ServerEndPoint(new IPEndPoint(IPAddress.Any, 1935), false));

public class Mp4OutputPathResolver : IFFmpegOutputPathResolver
{
    private readonly string _outputDir;

    public Mp4OutputPathResolver(string outputDir)
    {
        _outputDir = outputDir;
    }

    public ValueTask<string> ResolveOutputPath(IServiceProvider services, Guid contextIdentifier, string streamPath, IReadOnlyDictionary<string, string> streamArguments)
    {
        return ValueTask.FromResult(Path.Combine(_outputDir, streamPath.Trim('/'), "output.mp4"));
    }
}

Whenever a stream is published, a FFmpeg process with the argument -i {inputPath} -c:v libx264 -c:a aac -preset ultrafast -f mp4 {outputPath} will be automatically created. This process converts the RTMP stream to MP4.

Note that {inputPath} and {outputPath} are placeholders which will be replaced by the actual paths internally.