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"引用,將libvlc解壓至wpf生成路徑下,libvlc包含必須的解碼庫文件及插件
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 }
3.3前端 只需添加 image組件即可
3.4最終效果: