網(wǎng)絡(luò)攝像頭使用的是??低暤?,關(guān)于如何使用Ump插件播放海康威視r(shí)tsp視頻流,請(qǐng)參考我的這篇文章
內(nèi)部有ump插件的下載鏈接
untiy接入 海康威視網(wǎng)絡(luò)攝像頭
錄屏使用的插件是 AVPro movieCapture 4.6.3版, 插件和完整工程的下載鏈接放在本文的最后
錄制攝像頭的實(shí)現(xiàn)思想為
1 ump通過(guò)一個(gè)在RenderTexture上繪制圖像來(lái)播放畫面
2 movieCapture支持從紋理中獲取畫面
3 我們只需要使用Graphics.Blit()函數(shù)將一個(gè)紋理繪制到另一個(gè)紋理即可
核心腳本為以下三個(gè)
RecoderManger是我們自己寫的,用于打通ump和AVPro movieCapture之間的連接
CaptureFromTexture是AVPro movieCapture中用于從紋理中錄像的腳本,掛上就行,不用做任何調(diào)整
UniversalMediaPlayer是Ump插件提供的腳本,用于拉取網(wǎng)絡(luò)攝像頭的視頻
Ump里的預(yù)制體RawImage用于用于提供一個(gè)臨時(shí)的容器,ump會(huì)新建一個(gè)RenderTexture并給Rawimge的mainTexture屬性賦值(面板上顯示的屬性名為texture),RecoderManger獲取這個(gè)RenderTexture,然后將圖案繪制到CaptureFromTexture里的_texture屬性里,這樣就完成了錄像
程序打開sampleScene,直接運(yùn)行即可錄屏,停止運(yùn)行即可保存錄像,代碼里有詳細(xì)注釋,這里不再贅述文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-407946.html
using RenderHeads.Media.AVProMovieCapture;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UMP;
public class RecoderManager : MonoBehaviour
{
public int _textureWidth = 1024; //視頻的寬
public int _textureHeight = 768; //視頻的高
private CaptureFromTexture movieCapture = null; //錄像機(jī)
private UniversalMediaPlayer ump;//ump,用于拉取網(wǎng)絡(luò)攝像頭的視頻
//外部rawImage
public RawImage rawImage;
//錄像用的渲染紋理
private RenderTexture _texture;
/// <summary>
/// 錄像保存的路徑
/// </summary>
private string filePath = "";
/// <summary>
/// 錄像保存的文件夾名,追在在路徑之后
/// </summary>
public string folderName = "";
/// <summary>
/// 視頻降低多少分辨率,以 _textureWidth _textureHeight 制定的寬高為基礎(chǔ)
/// </summary>
public CaptureBase.DownScale downScale = CaptureBase.DownScale.Half;
/// <summary>
/// 視頻的前綴名
/// </summary>
public string fileNamePrefix = "";
/// <summary>
/// 視頻流的地址
/// </summary>
public string RTSPAddress = "";
private void Start()
{
ump = GetComponent<UniversalMediaPlayer>();
movieCapture = GetComponent<CaptureFromTexture>();
//如果視頻流是空的,直接返回
if (string.IsNullOrEmpty(RTSPAddress))
{
print("視頻流地址為空,停止錄像");
return;
}
//設(shè)置拉取的視頻流
ump.Path = RTSPAddress;
ump.Play();
//設(shè)置渲染紋理
_texture = new RenderTexture(_textureWidth, _textureHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
_texture.filterMode = FilterMode.Bilinear;
_texture.Create();
if (movieCapture)
{
movieCapture.SetSourceTexture(_texture);
}
//設(shè)置錄像機(jī)的屬性
filePath = Application.streamingAssetsPath + "/RecoderVideo/"+folderName+"/";
movieCapture.OutputFolderPath = filePath;//視頻的保存路徑 路徑不存在會(huì)自動(dòng)創(chuàng)建
movieCapture.FilenamePrefix = fileNamePrefix;//視頻的名字的前綴
movieCapture.ResolutionDownScale = downScale; //降低分辨率
//開始錄像
movieCapture.StartCapture();
rawImage.gameObject.SetActive(false); // RawImage的作用是提供中轉(zhuǎn)用的RenderTexture,禁用即可
}
private void OnDestroy()
{
if (_texture != null)
{
RenderTexture.Destroy(_texture);
_texture = null;
}
movieCapture.StopCapture();
}
private void Update()
{
UpdateTexture();
}
private void UpdateTexture()
{
Graphics.Blit(rawImage.mainTexture, _texture); //將紋理1 繪制到 紋理2 上,此處為將網(wǎng)絡(luò)攝像頭的畫面繪制到錄像用的紋理上
}
}
資源免積分下載
AVPro movieCapture 4.6.3
完整工程文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-407946.html
到了這里,關(guān)于untiy 錄制網(wǎng)絡(luò)攝像頭視頻并保存到本地文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!