josephnhtam / live-streaming-server-net

A .NET implementation of RTMP live streaming server, supporting HTTP-FLV, WebSocket-FLV, HLS, Kubernetes, cloud storage services integration and more.
https://josephnhtam.github.io/live-streaming-server-net/
MIT License
74 stars 11 forks source link

Save Stream as Video #23

Closed kasirajansfm closed 2 months ago

kasirajansfm commented 2 months ago

@josephnhtam is there any way to save the entire session as mp4 video like history.?

josephnhtam commented 2 months ago

Hi @kasirajansfm

You can do something like this to create a ffmpeg process to convert the rtmp stream into mp4

private static ILiveStreamingServer CreateLiveStreamingServer()
{
    return LiveStreamingServerBuilder.Create()
        .ConfigureRtmpServer(options => options
            .AddTransmuxer()
            .AddFFmpeg(options =>
            {
                options.Name = "mp4-convertor";
                options.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
                options.FFmpegTransmuxerArguments = "-i {inputPath} -c:v libx264 -c:a aac -preset ultrafast -f mp4 {outputPath}";
                options.OutputPathResolver = (contextIdentifier, streamPath, streamArguments) =>
                {
                    var entryDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!;
                    return Task.FromResult(Path.Combine(entryDirectory, streamPath.Trim('/'), "output.mp4"));
                };
            })
        )
        .ConfigureLogging(options => options.AddConsole())
        .Build();
}

Note: if you can ensure that the input codec is h264+aac, you can skip the transcoding with -i {inputPath} -codec copy -preset ultrafast -f mp4 {outputPath} to save some cpu usage.

kasirajansfm commented 2 months ago

its working. thanks for the response. is there any way to upload the mp4 file to blob as small chunks, like 1 min video.?

josephnhtam commented 2 months ago

You can try this instead

private static ILiveStreamingServer CreateLiveStreamingServer()
{
    return LiveStreamingServerBuilder.Create()
        .ConfigureRtmpServer(options => options
            .AddTransmuxer()
            .AddFFmpeg(options =>
            {
                options.Name = "mp4-convertor";
                options.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
                options.FFmpegTransmuxerArguments = "-i {inputPath} -c:v libx264 -c:a aac -preset ultrafast -segment_time 60 -f segment {outputPath}";
                options.OutputPathResolver = (contextIdentifier, streamPath, streamArguments) =>
                {
                    var entryDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!;
                    return Task.FromResult(Path.Combine(entryDirectory, streamPath.Trim('/'), "output%d.mp4"));
                };
            })
        )
        .ConfigureLogging(options => options.AddConsole())
        .Build();
}

If you want to upload the mp4, you may need to implement ITransmuxerEventHandler and add the uploading logic. The Hls uploader is also implemented on top of ITransmuxerEventHandler.

deepx commented 2 months ago

its working. thanks for the response. is there any way to upload the mp4 file to blob as small chunks, like 1 min video.?

You can do a lot of stuff with ffmpg, for example:

ffmpeg -i input.mp4 -c copy -map 0 -segment_time 00:20:00 -f segment output%03d.mp4 or ffmpeg -i source-file.foo -ss 0 -t 600 first-10-min.m4v