这周在研究基于rtmp+nginx直播流的实现,现总结如下:

0.所需文件:

链接:https://pan.baidu.com/s/1U5gsNI8Rcl684l5gVL6swg
提取码:dli9

1.nginx部署

1.1将nginx_1.7.11.3_Gryphon.zip解压,启动nginx.bat文件移动至nginx_1.7.11.3_Gryphon解压后文件夹内,双击bat,启动nginx

1.2浏览器输入:http://localhost:1234/ 若出现如下页面,恭喜你部署成功!如未出现,可能是防火墙或者端口被占用,可修改nginx_1.7.11.3_Gryphon\conf\nginx-win-rtmp.conf中的端口号。

基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流-LMLPHP

2.FFmpeg推流至服务器

2.1推流命令示例:打开cmd,cd到nginx_1.7.11.3_Gryphon路径下 输入 ffmpeg -re -i 11.mp4 -vcodec libx264 -acodec aac -f flv rtmp://127.0.0.1:1935/live/home 并回车 。rtmp://127.0.0.1:1935/live为推流应用,home为具体流的名称,值随意,如需多个推流同时进行,只需更改home,如rtmp://127.0.0.1:1935/live/test

基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流-LMLPHP

3.WPF端集成vlc拉流

3.1  项目里先添加Vlc.DotNet.Core.dll,Vlc.DotNet.Core.Interops.dll,Vlc.DotNet.Wpf.dll,可直接用我的 .net4.0版本,亦可下载源码自己编译(https://github.com/ZeBobo5/Vlc.DotNet   需用vs2017及以上编译)并在xaml中添加xmlns:local="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"引用,将libvlc解压至wpf生成路径下,libvlc包含必须的解码库文件及插件

3.2 后台代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vlc.DotNet.Wpf;
using System.Reflection;
using System.IO;
using Vlc.DotNet.Core; namespace LiveStreamTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private VlcVideoSourceProvider sourceProvider;
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc", IntPtr.Size == ? "win-x86" : "win-x64")); this.sourceProvider = new VlcVideoSourceProvider(this.Dispatcher);
this.sourceProvider.CreatePlayer(libDirectory/* pass your player parameters here */);
var mediaOptions = new[]
{
" :network-caching=2000"
};
this.sourceProvider.MediaPlayer.Play("rtmp://127.0.0.1:1935/live/home",mediaOptions);//rtmp://114.242.105.17:1935/live/test
//string file =@"D:\01_soft\rtmp\nginx_1.7.11.3_Gryphon\11.mp4";
//this.sourceProvider.MediaPlayer.Play(new FileInfo(file));//本地文件
this.sourceProvider.MediaPlayer.Log += new EventHandler<VlcMediaPlayerLogEventArgs>(MediaPlayer_Log);
this.sourceProvider.MediaPlayer.Manager.SetFullScreen(this.sourceProvider.MediaPlayer.Manager.CreateMediaPlayer(), true);
Binding bing = new Binding();
bing.Source = sourceProvider;
bing.Path = new PropertyPath("VideoSource");
img.SetBinding(Image.SourceProperty, bing);
} void MediaPlayer_Log(object sender, VlcMediaPlayerLogEventArgs e)
{
string message = "libVlc : " + e.Level + e.Message + e.Module;
System.Diagnostics.Debug.WriteLine(message);
//System.Diagnostics.Trace.WriteLine(message); } private void imgClose_MouseDown(object sender, MouseButtonEventArgs e)
{
this.Close();
Application.Current.Shutdown(); } private void imgMin_MouseDown(object sender, MouseButtonEventArgs e)
{
// WindowStateUtil.FullOrMin(this, WindowState.Minimized); }
}
}

3.3前端 只需添加 image组件即可

3.4最终效果:

基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流-LMLPHP

05-11 17:03