基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流
这周在研究基于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中的端口号。
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
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”引用
3.2 后台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using Vlc.DotNet.Wpf; 15 using System.Reflection; 16 using System.IO; 17 using Vlc.DotNet.Core; 18 19 namespace LiveStreamTest 20 { 21 /// <summary> 22 /// MainWindow.xaml 的交互逻辑 23 /// </summary> 24 public partial class MainWindow : Window 25 { 26 private VlcVideoSourceProvider sourceProvider; 27 public MainWindow() 28 { 29 InitializeComponent(); 30 } 31 32 private void Window_Loaded(object sender, RoutedEventArgs e) 33 { 34 var currentAssembly = Assembly.GetEntryAssembly(); 35 var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName; 36 // Default installation path of VideoLAN.LibVLC.Windows 37 var libDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64")); 38 39 this.sourceProvider = new VlcVideoSourceProvider(this.Dispatcher); 40 this.sourceProvider.CreatePlayer(libDirectory/* pass your player parameters here */); 41 var mediaOptions = new[] 42 { 43 " :network-caching=2000" 44 }; 45 this.sourceProvider.MediaPlayer.Play("rtmp://127.0.0.1:1935/live/home",mediaOptions);//rtmp://114.242.105.17:1935/live/test 46 //string file =@"D:\01_soft\rtmp\nginx_1.7.11.3_Gryphon\11.mp4"; 47 //this.sourceProvider.MediaPlayer.Play(new FileInfo(file));//本地文件 48 this.sourceProvider.MediaPlayer.Log += new EventHandler<VlcMediaPlayerLogEventArgs>(MediaPlayer_Log); 49 this.sourceProvider.MediaPlayer.Manager.SetFullScreen(this.sourceProvider.MediaPlayer.Manager.CreateMediaPlayer(), true); 50 Binding bing = new Binding(); 51 bing.Source = sourceProvider; 52 bing.Path = new PropertyPath("VideoSource"); 53 img.SetBinding(Image.SourceProperty, bing); 54 } 55 56 void MediaPlayer_Log(object sender, VlcMediaPlayerLogEventArgs e) 57 { 58 string message = "libVlc : " + e.Level + e.Message + e.Module; 59 System.Diagnostics.Debug.WriteLine(message); 60 //System.Diagnostics.Trace.WriteLine(message); 61 62 } 63 64 private void imgClose_MouseDown(object sender, MouseButtonEventArgs e) 65 { 66 this.Close(); 67 Application.Current.Shutdown(); 68 69 } 70 71 private void imgMin_MouseDown(object sender, MouseButtonEventArgs e) 72 { 73 // WindowStateUtil.FullOrMin(this, WindowState.Minimized); 74 75 } 76 } 77 }
View Code
3.3前端 只需添加 image组件即可
3.4最终效果: