简体   繁体   English

如何使用 LibVlcSharp 在 vi​​deoView 中删除视频黑带?

[英]How to remove video black bands in videoView with LibVlcSharp?

when I play a video with video, it presents black bands in the top and bottom part of the video, like in image in the URL https://imgur.com/a/JiUv8rt .当我播放带有视频的视频时,它会在视频的顶部和底部显示黑色条纹,就像 URL https://imgur.com/a/JiUv8rt中的图像一样。 I'd like to remove the bands and display just the video in an absolute layout.我想删除乐队并以绝对布局仅显示视频。 how can I reach my goal?我怎样才能达到我的目标?

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
             xmlns:shared="clr-namespace:LibVLCSharp.Forms.Shared;assembly=LibVLCSharp.Forms"
             x:Class="App.Pages.WebcamVideoPopUpPage"
             BackgroundColor="Transparent">
    <pages:PopupPage.Animation>
        <animations:ScaleAnimation 
            PositionIn="Center"
            PositionOut="Center"
            ScaleIn="1.2"
            ScaleOut="0.8"
            DurationIn="400"
            DurationOut="300"
            EasingIn="SinOut"
            EasingOut="SinIn"
            HasBackgroundAnimation="True"/>
    </pages:PopupPage.Animation>

    <AbsoluteLayout x:Name="AbsoluteLayoutWebcam"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">


        <shared:VideoView x:Name="VideoViewWebcam"
                          AbsoluteLayout.LayoutFlags="All"
                          AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
                          MediaPlayer ="{Binding MediaPlayer}"
                          MediaPlayerChanged ="VideoView_MediaPlayerChanged"/>

        <Label x:Name="DescriptionWebcam"
               AbsoluteLayout.LayoutFlags="All" 
               AbsoluteLayout.LayoutBounds="0, 0, 1, .2"
               HorizontalOptions="Fill" 
               VerticalOptions="Fill" 
               Text="{Binding InfoWebcam}"
               FontSize="Large"
               TextColor="White"/>

    </AbsoluteLayout>
</pages:PopupPage>

UPDATE I update at latest pre-release as @mtz suggested me, and I modified my code in the following way:更新我按照@mtz 的建议在最新的预发布版本中进行更新,并按以下方式修改了我的代码:

public partial class WebcamVideoPopUpPage : PopupPage
{
    public WebcamVideoPopUpPage()
    {
        var vm = App.Locator.WebCamVideoVM;
        this.BindingContext = vm;
        InitializeComponent();
        MediaPlayerWebcam.VideoView.MediaPlayerChanged += VideoView_MediaPlayerChanged;
        MediaPlayerWebcam.MediaPlayer.AspectRatio = "FitScreen";
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Messenger.Default.Send(new OnApperingVideoMessage());
    }

    private void VideoView_MediaPlayerChanged(object sender, LibVLCSharp.Shared.MediaPlayerChangedEventArgs e)
    {
        Messenger.Default.Send(new OnVideoViewInitializedMessage());
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MediaPlayerWebcam.MediaPlayer.Stop();
        MediaPlayerWebcam.MediaPlayer = null;

    }

}

My xaml:我的xml:

<AbsoluteLayout x:Name="AbsoluteLayoutWebcam"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand">


    <shared:MediaPlayerElement x:Name="MediaPlayerWebcam"
                      AbsoluteLayout.LayoutFlags="All"
                      AbsoluteLayout.LayoutBounds="0, 0, 1, .4"
                      MediaPlayer ="{Binding MediaPlayer}"/>

    <Label x:Name="DescriptionWebcam"
           AbsoluteLayout.LayoutFlags="All" 
           AbsoluteLayout.LayoutBounds="0, 0, 1, .2"
           HorizontalOptions="Fill" 
           VerticalOptions="Fill" 
           Text="{Binding InfoWebcam}"
           FontSize="Large"
           TextColor="White"/>

</AbsoluteLayout>

My viewModel:我的视图模型:

public class WebcamVideoViewModel : BaseViewModel
{
    private LibVLC LibVLC { get; set; }

    private bool IsLoaded { get; set; }
    private bool IsVideoViewInitialized { get; set; }

    private Media Media { get; set; }

    private MediaPlayer _mediaPlayer;
    public MediaPlayer MediaPlayer
    {
        get { return _mediaPlayer; }
        set
        {
            _mediaPlayer = value;
            OnPropertyChanged();
        }
    }

    private string _InfoWebcam { get; set; }

    public string InfoWebcam
    {
        get { return _InfoWebcam; }

        set
        {
            _InfoWebcam = value;
            OnPropertyChanged();
        }
    }
    public WebcamVideoViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeFactory) : base(navigationService, apiAutostradeFactory)
    {
        Messenger.Default.Register<InfoWebcamVideoMessage>(this, OnReceivedInfoWebcam);
        Messenger.Default.Register<OnApperingVideoMessage>(this, OnAppearing);
        Messenger.Default.Register<OnVideoViewInitializedMessage>(this, OnVideoViewInitialized);
        Task.Run(Initialize);
    }

    private void Initialize()
    {
        Core.Initialize();

        LibVLC = new LibVLC();
        MediaPlayer = new MediaPlayer(LibVLC);

    }

    private async void OnReceivedInfoWebcam(InfoWebcamVideoMessage msg)
    {
        var response = await ApiManager.GetVideoWebcam(msg.Mpr, msg.Uuid);
        if (response.IsSuccessStatusCode)
        {
            InfoWebcam = msg.T_Description_webcam;
            var stream = await response.Content.ReadAsStreamAsync();
            Media = new Media(LibVLC, stream);

            Play();
        }
    }

    public void OnAppearing(OnApperingVideoMessage msg)
    {
        IsLoaded = true;

    }

    public void OnVideoViewInitialized(OnVideoViewInitializedMessage msg)
    {
        IsVideoViewInitialized = true;
    }

    private void Play()
    {
        if (IsLoaded && IsVideoViewInitialized)
        {
            MediaPlayer.Play(Media);

        }
    }

}

Now i'm closer to solution because videoView is resizable with the button present at bootm, but I'd like to get at start the fill AspectRatio and I don't want anything expect the video(In other words, I'd want to remove seek bar and resize video button).现在我更接近解决方案了,因为 videoView 可以通过 bootm 上的按钮调整大小,但我想开始填充 AspectRatio 并且我不想要任何除了视频的东西(换句话说,我想要删除搜索栏并调整视频按钮大小)。 Another problem is that after I close mediaPlayer, and I open again a new video, my app crashes.另一个问题是,在我关闭 mediaPlayer 并再次打开一个新视频后,我的应用程序崩溃了。 Any advice?有什么建议吗?

You need to change the aspect ratio to "fill the screen".您需要更改纵横比以“填充屏幕”。

See how to here: https://github.com/videolan/libvlcsharp/blob/91b8f06ee1bedd9b3219a4e9ade0a9c44f59fda8/LibVLCSharp.Forms/Shared/PlaybackControls.xaml.cs#L926 or use the latest pre-release LibVLCSharp.Forms package that contains the MediaPlayerElement which has this feature built-in (soon in stable version).在此处查看操作方法: https : //github.com/videolan/libvlcsharp/blob/91b8f06ee1bedd9b3219a4e9ade0a9c44f59fda8/LibVLCSharp.Forms/Shared/PlaybackControls.xaml.cs#L926或使用最新的预发布版 MediaFormsSharp 包,其中包含 LibElementlayerPLC。内置此功能(即将推出稳定版)。

What you just trying is to stretch the video.你只是想拉伸视频。 But remember it will effect video quality .但请记住,它会影响视频质量
To remain simple, you can try this working and tested code:为了保持简单,您可以尝试使用以下有效且经过测试的代码:

MediaPlayerWebcam.MediaPlayer.AspectRatio = $"{MediaPlayerWebcam.Width.ToString()}:{MediaPlayerWebcam.Height.ToString()}";
MediaPlayerWebcam.Scale = 0;

My scenario is a fullscreen player, I do it with these codes refer to LibVLCSharp.Forms, hope it will be helpful.我的场景是一个全屏播放器,我用这些代码来做,参考 LibVLCSharp.Forms,希望它会有所帮助。 the code deal with fullscreen(commented) or fill screen with video.代码处理全屏(评论)或用视频填充屏幕。

    public void PlayerFullScreen()
    {
        //restore
        if (_isFullScreen)
        {
            RestoreDefaultWindowInfo();
            _isFullScreen = false;
            _mediaPlayer.Scale = _originalScale;   //reset 
            _mediaPlayer.AspectRatio = _originalAspectRatio;
            //Mouse.Capture(null);
            playerBar.Visibility = Visibility.Visible;
        }
        else  // fullscreen(stretch video)
        {
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            this.Left = 0;
            this.Top = 0;
            this.Width = SystemParameters.VirtualScreenWidth;
            this.Height = SystemParameters.VirtualScreenHeight;
            //this.Topmost = true;
            _isFullScreen = true;
            _originalScale = _mediaPlayer.Scale;   // save original
            _originalAspectRatio = _mediaPlayer.AspectRatio;

            playerBar.Visibility = Visibility.Collapsed;

            MediaTrack? mediaTrack;
            try
            {
                mediaTrack = _mediaPlayer.Media?.Tracks?.FirstOrDefault(x => x.TrackType == TrackType.Video);
            }
            catch (Exception)
            {
                mediaTrack = null;
            }
            if (mediaTrack == null || !mediaTrack.HasValue)
            {
                return;
            }

            //get windows scale factor(DPI)
            PresentationSource source = PresentationSource.FromVisual(this);
            double dpiX=1.0, dpiY=1.0;
            if (source != null)
            {
                dpiX = source.CompositionTarget.TransformToDevice.M11;
                dpiY = source.CompositionTarget.TransformToDevice.M22;
            }

            var displayW = this.Width * dpiX;
            var displayH = this.Height * dpiY;
            var videoSwapped = mediaTrack.Value.Data.Video.Orientation == VideoOrientation.LeftBottom ||
                                        mediaTrack.Value.Data.Video.Orientation == VideoOrientation.RightTop;

            var videoW = mediaTrack.Value.Data.Video.Width;
            var videoH = mediaTrack.Value.Data.Video.Height;

            if (videoSwapped)
            {
                var swap = videoW;
                videoW = videoH;
                videoH = swap;
            }
            if (mediaTrack.Value.Data.Video.SarNum != mediaTrack.Value.Data.Video.SarDen)
                videoW = videoW * mediaTrack.Value.Data.Video.SarNum / mediaTrack.Value.Data.Video.SarDen;

            var ar = videoW / (float)videoH;
            var dar = displayW / (float)displayH;

            float scale;
            if (dar >= ar)
                scale = (float)displayW / videoW; /* horizontal */
            else
                scale = (float)displayH / videoH; /* vertical */

            //keep ratio of width/height, not fill the srceen
            //_mediaPlayer.Scale = scale;         // 这是保持视频宽高比的,视频不会变形
            //_mediaPlayer.AspectRatio = string.Empty;//这是保持视频宽高比的,视频不会变形

            //这是视频变形适配屏幕的情况(满屏)
            //fill all the screen by video,video is streched 
            float xscale, yscale;
            xscale = (float)(displayW / videoW);
            yscale = (float)(displayH / videoH);
            _mediaPlayer.Scale = (xscale<yscale)? xscale:yscale;
            string aspectRatio = String.Format("{0}:{1}",
                    this.Width,this.Height);
            _mediaPlayer.AspectRatio = aspectRatio;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用LibVLCSharp复制第二个视频? - How to reproduce a second video with LibVLCSharp? 如何在 LibVLCSharp.WPF.VideoView 上放置自动显示面板? - How to put an auto show panel over a LibVLCSharp.WPF.VideoView? 播放带有音频的视频产生黑屏(Vlc.DotNet 和 libvlcsharp) - Playing video yeild black screen with audio(Vlc.DotNet and libvlcsharp) 如何阻止 Grid 控件中的 VideoView 控件使用 LibVLCSharp 库打开新的 window? - How do I stop VideoView control inside a Grid control from opening a new window using LibVLCSharp library? AvaloniaUI:无法在 UserControl 中嵌入 VideoView 控件 (LibVlcSharp) - AvaloniaUI: Cannot embed VideoView control (LibVlcSharp) in a UserControl 如何使用 LibVLCSharp 将一系列图像转换为可播放的视频? - How can I transform a sequence of images into a playable video using LibVLCSharp? 使用 LibvlcSharp,如何更改视频的播放速度 - Using LibvlcSharp, how can I change the speed that a video is playing LibVLCSharp.WPF VideoView.Content(重叠)滚动 - LibVLCSharp.WPF VideoView.Content (overlay) scrolling 如何使用 libvlcsharp(c# 或 vb.net)从数据 stream 播放视频 - How to play video from a data stream using libvlcsharp (c# or vb.net) 解析从 stream 加载的 libvlcsharp 视频不起作用 - Parsing libvlcsharp video loaded from stream not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM