如何在WPF C#中播放和录制UDP URL视频?

6yoyoihd  于 2023-03-24  发布在  C#
关注(0)|答案(1)|浏览(150)

我必须在我的WPF应用程序中播放和录制UDP URL视频,为此目的,目前我正在使用vlc.dotnet.wpf
在Xaml端,我有Assembly

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

控制播放视频

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

代码隐藏:

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"));

通过这个我正在播放UDP视频成功,现在我想记录这个视频到我的PC中的特定目录,请指导我我必须在我的代码中添加进一步实现这一点,请推荐我任何其他工具/库等,如果它不容易与VLC插件.谢谢你v很多

yhuiod9q

yhuiod9q1#

这里有一个libvlcsharp示例可以做到这一点:https://github.com/mfkl/libvlcsharp-samples/blob/master/RecordHLS/Program.cs
代码:

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();
            }
        }
    }
}

请迁移到libvlcsharp而不是使用vlc.dotnet.vlc.dotnet未维护。

相关问题