1 Star 0 Fork 1

wangafeiBlogs / Mpv.NET.lib

forked from GiantappMan / Mpv.NET.lib 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Mpv.NET (lib)

Version Downloads

.NET embeddable video/media player based on mpv for WinForms and WPF

Player Features

  • Looping
  • Auto-play
  • Frame stepping
  • Asynchronous seeking
  • Change playback speed
  • Simple setup and usage
  • Logging from mpv
  • Add separate audio track
  • Playlist - Load, Next, Previous, Move, Remove or Clear
  • Optional youtube-dl support to play videos from hundreds of video sites.
    • Change the desired video quality.

Notes:

  • See here for Mpv.NET the C# media player based on mpv.
  • Very little documentation has been written for the C API wrapper. Consult client.h.
  • The entirety of the mpv C API has not yet been implemented.

If you encounter any bugs or would like to see a feature added then please open an issue. Contributions are very welcome!

Download

This package is available via NuGet.

Prerequisites

libmpv

To use the API wrapper (and player) you will need libmpv.

  1. Download libmpv from here. (latest "Dev" build)
  2. Extract "mpv-1.dll" from either the "i686" (32-bit) or "x86_64" (64-bit) folder into your project. (A "lib" folder in your project is common practice)
  3. Include the DLL in your IDE and instruct your build system to copy the DLL to output.
    • In Visual Studio this can be achieved so:
      1. In your Solution Explorer click the "Show All Files" button at the top. (make sure you have the correct project selected)
      2. You should see the DLL show up, right click on it and select "Include In Project".
      3. Right click on the DLL and select "Properties", then change the value for "Copy to Output Directory" to "Copy Always".
  4. Done!

If you wish to compile libmpv yourself, there is a guide available in the mpv repository.

youtube-dl

To enable youtube-dl functionality in the player you will need the youtube-dl executable and the ytdl hook script from mpv which allows mpv to communicate with youtube-dl.

  1. Download the "youtube-dl.exe" executable from here.
  2. Download the "ytdl_hook.lua" script from here.
  3. Copy both files to your "lib" folder you made for libmpv.
  4. Follow step #3 in the libmpv section but perform the steps on the "ytdl_hook.lua" script and "youtube-dl.exe" executable instead.
  5. Open "ytdl_hook.lua" and change line 13 to path = "lib\\youtube-dl.exe",
  6. In your code, you will need to call the EnableYouTubeDl method on an instance of MpvPlayer.
    • If you placed your "ytdl_hook.lua" script somewhere other than the "lib" folder, you will need to pass the relative (to your apps executable) or absolute path of the script to EnableYouTubeDl.
  7. Done!

If youtube-dl stops working after working fine, first try updating the executable with the latest version.

If you have any doubts or questions regarding this process, please feel free to open an issue.

Player

This player was designed to work on Windows and tested in WPF and WinForms. Not tested on other platforms.

To overlay controls over the top of the player please start with this Stack Overflow post.

If you're looking for a media player UI, I'd recommend MediaPlayerUI.NET. :)

Initialisation

MpvPlayer provides 4 constructors:

  1. MpvPlayer()
  2. MpvPlayer(string libMpvPath)
  3. MpvPlayer(IntPtr hwnd)
  4. MpvPlayer(IntPtr hwnd, string libMpvPath)

Constructors #1 and #2 let mpv create it's own window and will not embed it.

Constructors #3 and #4 allow you to specify a hwnd parameter which should be the handle to the host control where you want to embed the player.

For the constructors where libMpvPath is not included, the player attempts to load libmpv from: (in order)

  • LibMpvPath property
  • "mpv-1.dll"
  • "lib\mpv-1.dll"

WPF

Since WPF doesn't keep traditional handles to controls we will need to use a System.Windows.Forms.Control object (E.g. Panel or nearly any other WinForms control) to host the mpv instance.

First you will need to add a reference to System.Windows.Forms in your project:

  • In Visual Studio this can be achieved so:
    • Right click "References" in your project and click "Add Reference".
    • Navigate to Assemblies, find System.Windows.Forms, make sure it's ticked and click "OK".

Next, you will need to reference the System.Windows.Forms namespace in XAML:

<Window ...
        xmlns:windowsForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        ...>

Then create a WindowsFormsHost with a Panel from WinForms:

<WindowsFormsHost>
    <windowsForms:Panel x:Name="PlayerHost" />
</WindowsFormsHost>

If WindowsFormHost isn't found you will need to add a reference to WindowsFormsIntegration to your project:

  • Just as above: this can be achieved in Visual Studio by:
    • Right clicking "References" in your project and clicking "Add Reference".
    • Navigating to Assemblies, finding WindowsFormsIntegration, making sure it's ticked and clicking "OK".

In your .xaml.cs file create a field/property of type MpvPlayer and initialise it using one of the constructors outlined above.

In this example we called our Panel object PlayerHost so for the hwnd parameter in the constructor you would pass in PlayerHost.Handle like so:

player = new MpvPlayer(PlayerHost.Handle);

See Mpv.NET.WPFExample project for a basic example.

WinForms

You can use any WinForms control, just pass the Handle property to the MpvPlayer constructor and you're done! Easy.

See Mpv.NET.WinFormsExample project for a basic example.

youtube-dl settings

You have the option to set the desired quality of the media you're trying to play using youtube-dl. This can be changed by setting the YouTubeDlVideoQuality property on an instance of MpvPlayer. Note: this will take effect on the next call of Load.

API

This "API" is a wrapper around the mpv C API defined in client.h and is utilised by the player.

Mpv

Simplest way to create an instance of the mpv .NET API and load libmpv:

// Relative path to the DLL.
var dllPath = @"lib\mpv-1.dll";

using (var mpv = new Mpv(dllPath))
{
    // code
}

This will provide a friendly interface to the mpv C API and start an event loop which will raise events accordingly.

MpvFunctions

If you are looking for a more direct approach to the C API you can create an instance of MpvFunctions which allows you to execute the loaded delegates directly:

var dllPath = @"lib\mpv-1.dll";

using (var mpvFunctions = new MpvFunctions(dllPath))
{
    // code
}

Be aware, this method does not raise events and an event loop would have to be manually implemented.

MpvEventLoop

If you are looking that start an event loop you will need to create an instance of MpvFunctions, create and initialise mpv, create an instance of MpvEventLoop and start it:

var dllPath = @"lib\mpv-1.dll";

// Create an instance of MpvFunctions.
var functions = new MpvFunctions(dllPath);

// Create mpv
var handle = functions.Create();

// Initialise mpv
functions.Initialise(handle);
    
// Create an instance of MpvEventLoop, passing in a callback argument
// which will be invoked when an event comes in.
using (var eventLoop = new MpvEventLoop(callback, handle, functions))
{
    // Start the event loop.
    eventLoop.Start();
}

The code above does not contain any error checking, most of the mpv functions return an MpvError which indicates whether an error has occured.

Licensing

The libmpv C API specifically is licensed under ICS, this means that a wrapper such as this can be licensed under MIT.

The rest of libmpv is licensed under GPLv2 by default, which means that any work utilising this wrapper in conjunction with libmpv is subject to GPLv2, unless libmpv is compiled using LGPL.

In simple terms, once you use the "libmpv" files (DLL) you downloaded, your application must be licensed under GPLv2.

See here for more information.

MIT License Copyright (c) 2019 Aurel Hudec Jr Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

.NET embeddable video/media player based on mpv for WinForms and WPF 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wangafeiblogs/Mpv.NET.lib.git
git@gitee.com:wangafeiblogs/Mpv.NET.lib.git
wangafeiblogs
Mpv.NET.lib
Mpv.NET.lib
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891