天天看點

untiy 錄制網絡攝像頭視訊并儲存到本地檔案

網絡攝像頭使用的是海康威視的,關于如何使用Ump插件播放海康威視rtsp視訊流,請參考我的這篇文章

内部有ump插件的下載下傳連結

untiy接入 海康威視網絡攝像頭

錄屏使用的插件是 AVPro movieCapture 4.6.3版, 插件和完整工程的下載下傳連結放在本文的最後

錄制攝像頭的實作思想為

1 ump通過一個在RenderTexture上繪制圖像來播放畫面

2 movieCapture支援從紋理中擷取畫面

3 我們隻需要使用Graphics.Blit()函數将一個紋理繪制到另一個紋理即可

核心腳本為以下三個

untiy 錄制網絡攝像頭視訊并儲存到本地檔案

RecoderManger是我們自己寫的,用于打通ump和AVPro movieCapture之間的連接配接

CaptureFromTexture是AVPro movieCapture中用于從紋理中錄像的腳本,挂上就行,不用做任何調整

UniversalMediaPlayer是Ump插件提供的腳本,用于拉取網絡攝像頭的視屏

Ump裡的預制體RawImage用于用于提供一個臨時的容器,ump會建立一個RenderTexture并給Rawimge的mainTexture屬性指派(面闆上顯示的屬性名為texture),RecoderManger擷取這個RenderTexture,然後将圖案繪制到CaptureFromTexture裡的_texture屬性裡,這樣就就完成了錄像

程式打開sampleScene,直接運作即可錄屏,停止運作即可儲存錄像,代碼裡有詳細注釋,這裡不再贅述

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; //錄像機
	private UniversalMediaPlayer ump;//ump,用于拉取網絡攝像頭的視訊
	//外部rawImage
	public RawImage rawImage;
	//錄像用的渲染紋理
	private RenderTexture _texture;
	/// <summary>
	/// 錄像儲存的路徑
	/// </summary>
	private string filePath = "";
	/// <summary>
	/// 錄像儲存的檔案夾名,追在在路徑之後
	/// </summary>
	public string folderName = "";
	/// <summary>
	/// 視訊降低多少分辨率,以  _textureWidth  _textureHeight 制定的寬高為基礎
	/// </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;
		}
		//設定拉取的視訊流
		ump.Path = RTSPAddress;
		ump.Play();
		//設定渲染紋理
		_texture = new RenderTexture(_textureWidth, _textureHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
        _texture.filterMode = FilterMode.Bilinear;
        _texture.Create();
        if (movieCapture)
		{
			movieCapture.SetSourceTexture(_texture);
		}
		//設定錄像機的屬性
		filePath = Application.streamingAssetsPath + "/RecoderVideo/"+folderName+"/";
		movieCapture.OutputFolderPath = filePath;//視訊的儲存路徑 路徑不存在會自動建立
		movieCapture.FilenamePrefix = fileNamePrefix;//視訊的名字的字首
		movieCapture.ResolutionDownScale = downScale;	//降低分辨率	
		//開始錄像
		movieCapture.StartCapture();		
		rawImage.gameObject.SetActive(false); // RawImage的作用是提供中轉用的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 上,此處為将網絡攝像頭的畫面繪制到錄像用的紋理上
	}
}

           

資源免積分下載下傳

AVPro movieCapture 4.6.3