windows下 Nginx+FFmpeg實現rtsp流轉hls流,在WEB通過H5 video實現視頻播放

1.準備好相關軟件,Nginx、FFmpeg、vlc以及前臺在web中集成時用的video.js庫,可以到

https://download.csdn.net/download/jinxiumeihappy/12327803 中去下載

2.將下載後的ffmpeg和nginx這兩個文件夾直接拷貝,放置在想要放置的地方,最好不要放置在C盤,(作爲測試使用,我放置在了E盤的shipin文件夾下)

3.修改nginx配置,在nginx\conf中修改nginx.conf配置文件,添加或者修改成如下格式(在以上提供的下載連接中,nginx.conf已經更改好)

 

4.進入命令窗口,啓動nginx,具體操作如下:此時打開任務管理器,可以看到nginx已經啓動成功。

5.進入命令窗口,查看ffmepg是否正常,具體操作如下:

輸入ffmepg命令後,會打印出很多東西,則說明,ffmepg是是正常的,在這裏可以吧nginx和ffmepg配置到系統環境變量path中,就可以直接使用明明,不用進入相應的文件夾下,但是我不習慣那麼用

6.FFmpeg命令行轉流

ffmpeg -i "rtsp://admin:[email protected]" -c copy -f hls -hls_time 2.0 -hls_list_size 0 -hls_wrap 15 E:/shipin/nginx/html/hls/test.m3u8 

其中: E:/shipin/nginx/html/hls/test.m3u8 是對應nginx.conf配置中的hls訪問路徑/hls

ffmpeg 關於hls方面的指令說明:

-hls_time n: 設置每片的長度,默認值爲2。單位爲秒
-hls_list_size n:設置播放列表保存的最多條目,設置爲0會保存有所片信息,默認值爲5
-hls_wrap n:設置多少片之後開始覆蓋,如果設置爲0則不會覆蓋,默認值爲0.這個選項能夠避免在磁盤上存儲過多的片,而且能夠限制寫入磁盤的最多的片的數量
-hls_start_number n:設置播放列表中sequence number的值爲number,默認值爲0

在這裏具體的命令行轉流可以用java來實現,我建立一個java項目,以下是源碼的下載地址:

https://download.csdn.net/download/jinxiumeihappy/12328114

7.html通過h5的video標籤進行播放,此處需要一個第三方的video的js庫,具體代碼如下

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video.js 7.4.1</title>
<link href="../lib/video-7.4.1/video-js.min.css" rel="stylesheet">
<style>
body {
	background-color: #191919
}
.m {
	width: 960px;
	height: 400px;
	margin-left: auto;
	margin-right: auto;
	margin-top: 100px;
}
</style>
</head>

<body>
	<div class="m">
		<video id="my-video" class="video-js" muted controls autoplay preload="auto" width="480" height="300" data-setup="{}">
			<source src="http://localhost:5010/hls/test.m3u8" type="application/x-mpegURL">
		</video>
		<video id="my-video" class="video-js" muted controls autoplay preload="auto" width="480" height="300" data-setup="{}">
			<source src="http://localhost:5010/hls/test.m3u8" type="application/x-mpegURL">
		</video>
		<script type="text/javascript" src="../lib/video-7.4.1/video.min.js"></script>
		<script type="text/javascript">
			var myPlayer = videojs('my-video');
			videojs("my-video").ready(function() {
				var myPlayer = this;
				myPlayer.play();
			});
		</script>
	</div>
</body>
</html>

其中"http://localhost:5010/hls/test.m3u8"就是ffmepg轉流後,經nginx轉成http協議的hls格式的視頻訪問地址,添加 muted  和autoplay,可以使視頻自動播放,以下是上面頁面的展示效果

參考博客:

https://blog.csdn.net/wenqiangluyao/article/details/98594861

https://blog.csdn.net/wenqiangluyao/article/details/97897794

還有一些網上的博客,大家可自行百度

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章