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最終效果: