How to Play and Record UDP URL Video in WPF C#?

346 Views Asked by At

I have to Play and Record UDP Url Video in my WPF Application, For that purpose currently I am using vlc.dotnet.wpf package

In Xaml side I have Assembly

xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"

Have Control to play Video

<wpf:VlcControl BorderBrush="White" BorderThickness="1" x:Name="vlcControl1" Grid.Column="0" Background="#FF023602" />

Code behind:

DirectoryInfo vlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
            this.vlcControl1.SourceProvider.CreatePlayer(this.vlcLibDirectory);
            vlcControl1.SourceProvider.MediaPlayer.Play(new Uri(@"udp://@127.0.0.1:5000"));

By this I'm Playing UDP Video Successfully, Now I want to record this Video to Specific Directory in my PC, Please guideme what I have to add in my code further to achieve this, Please recommend me any other tool/ Lib etc if it's not easy with vlc plugin. Thank You v Much

1

There are 1 best solutions below

1
mfkl On

There is a libvlcsharp sample that does exactly this, here: https://github.com/mfkl/libvlcsharp-samples/blob/master/RecordHLS/Program.cs

Code:

using LibVLCSharp.Shared;
using System;
using System.IO;
using System.Reflection;

namespace RecordHLS
{
    class Program
    {
        static void Main(string[] args)
        {
            // Record in a file "record.ts" located in the bin folder next to the app
            var currentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var destination = Path.Combine(currentDirectory, "record.ts");

            // Load native libvlc library
            Core.Initialize();

            using (var libvlc = new LibVLC())
            using (var mediaPlayer = new MediaPlayer(libvlc))
            {
                // Redirect log output to the console
                libvlc.Log += (sender, e) => Console.WriteLine($"[{e.Level}] {e.Module}:{e.Message}");
                mediaPlayer.EndReached += (sender, e) =>
                {
                    Console.WriteLine("Recorded file is located at " + destination);
                    Environment.Exit(0);
                };

                // Create new media with HLS link
                using (var media = new Media(libvlc, new Uri("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"),
                    // Define stream output options.
                    // In this case stream to a file with the given path and play locally the stream while streaming it.
                    ":sout=#file{dst=" + destination + "}", 
                    ":sout-keep"))
                {
                    // Start recording
                    mediaPlayer.Play(media);
                }

                Console.WriteLine($"Recording in {destination}");
                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
            }
        }
    }
}

Please migrate to libvlcsharp instead of using vlc.dotnet. vlc.dotnet is unmaintained.