主要是先获取视频的码率,因为在转换过程中需要视频的码率,然后根据命令进行转换。
转完之后的mp4用Avpro去播放具体查Avpro怎么播放黑白通道视频吧。
PlaneVideoLoadTip 是提示进度预制体,自行修改吧。
这个是FFmpeg的下载要放在StreamingAssets文件夹下:
https://download.csdn.net/download/qq_42223582/87051539
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;
///
/// 透明视频转换
///
public class PlaneVideoFormatConvertMgr : MonoBehaviour
{
private static PlaneVideoFormatConvertMgr _Instance;
public static PlaneVideoFormatConvertMgr Instance
{
get
{
if (_Instance == null)
{
GameObject obj = Instantiate(Resources.Load<GameObject>("VideoFormatConvertMgr"));
_Instance = obj.GetComponent<PlaneVideoFormatConvertMgr>();
}
return _Instance;
}
}
///
/// FFMpeg路径
///
public string mFFMpegPath;
public string mFFprobePath;
///
/// 当前转换进度
///
public float mCurrentProcessValue;
///
/// FFmpeg转换数据
///
private FFmpegVideoInfo mFFmpegVideoInfo;
///
/// 输出信息
///
public string mDebugMessage = string.Empty;
///
/// FFmpeg返回错误原因列表
///
public List<string> mFFmpegErrorList = new List<string>();
///
/// 获取视频码率
///
private bool mIsGetVideoRate = false;
///
/// 是否转换结束
///
private bool mIsConvertEnd = false;
///
/// 当前视频参数
///
public VideoChangeData mCurrentVideoChangeData = new VideoChangeData();
///
/// 当前转换所需数据
///
public ConvertVideoData mCurrentConvertVideoData = new ConvertVideoData();
///
/// FFMpeg线程
///
private Thread mRunFFmpegThread;
///
/// 转换视频进程
///
private Process mFFMpegConvertVideoProcess;
///
/// 转换结束
/// bool:是否成功
/// string:转换后的路径
///
Action<bool, string> mActionChangeEnd;
void Awake()
{
mFFMpegPath = Application.streamingAssetsPath + "/FFmpeg/bin/ffmpeg.exe";
mFFprobePath = Application.streamingAssetsPath + "/FFmpeg/bin/ffprobe.exe";
}
///
/// 开始转换视频
///
///
///
public void StartChangeVideo(VideoChangeData videoChangeData,Action<bool,string> actionChangeEnd)
{
mFFmpegErrorList.Clear();
mDebugMessage = string.Empty;
mIsGetVideoRate = false;
mActionChangeEnd = actionChangeEnd;
StartCoroutine(StartInitVideo(videoChangeData));
}
///
/// 初始化视频数据
///
///
///
IEnumerator StartInitVideo(VideoChangeData videoChangeData)
{
if (string.IsNullOrEmpty(videoChangeData.videoPath))
{
Debug.Log("mov转换视频路径为空");
yield break;
}
mCurrentVideoChangeData = videoChangeData;
PlaneVideoLoadTip.Instance.SetPanelState(true);
PlaneVideoLoadTip.Instance.SetCancelAction(() => {
mCurrentProcessValue = 0.0f;
DeleteElement();
});
GetcodeRate();
yield return new WaitUntil(()=>true == mIsGetVideoRate);
StartConvertVideo(videoChangeData);
}
///
/// 开始转换视频
///
///
private void StartConvertVideo(VideoChangeData videoChangeData)
{
mFFmpegVideoInfo = null;
if (mCurrentConvertVideoData == null)
{
mCurrentConvertVideoData = new ConvertVideoData();
}
mRunFFmpegThread = new Thread(RunFFmpegExe);
mCurrentConvertVideoData.thread = mRunFFmpegThread;
mCurrentConvertVideoData.inputMovFileName = videoChangeData.videoPath;
switch (videoChangeData.splitType)
{
case 1:
mCurrentConvertVideoData.outVideoFileName = videoChangeData.videoPath.Replace(".mov", "UD.mp4");
break;
case 2:
mCurrentConvertVideoData.outVideoFileName = videoChangeData.videoPath.Replace(".mov", "RL.mp4");
break;
}
mRunFFmpegThread.Start(mCurrentConvertVideoData);
}
///
/// FFmpeg执行转换
///
///
private void RunFFmpegExe(object obj)
{
mFFMpegConvertVideoProcess = new Process();
mFFMpegConvertVideoProcess.StartInfo.FileName = mFFMpegPath;
if (mCurrentVideoChangeData.splitType == 2)
{
//转换为左右分离的视频
//p.StartInfo.Arguments = $"-i {data.inputMovFileName} -c:v libvpx-vp9 {data.outVideoFileName}";
mFFMpegConvertVideoProcess.StartInfo.Arguments = $"-i \"{mCurrentConvertVideoData.inputMovFileName}\" -c:v libvpx-vp9 -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" -vf \"split[a], pad = iw * 2:ih[b], [a] alphaextract, [b] overlay=w\" -b {mCurrentConvertVideoData.code }k -r {mCurrentConvertVideoData.frameRate} -y \"{mCurrentConvertVideoData.outVideoFileName}\"";
}
else if (mCurrentVideoChangeData.splitType == 1)
{
//转换成上下分离的视频
//p.StartInfo.Arguments = $"-i {data.inputMovFileName} -c:v libvpx-vp9 {data.outVideoFileName}";
mFFMpegConvertVideoProcess.StartInfo.Arguments = $"-i \"{mCurrentConvertVideoData.inputMovFileName}\" -c:v libvpx-vp9 -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" -vf \"split[a], pad = iw:ih * 2[b], [a] alphaextract, [b] overlay=0:h\" -b {mCurrentConvertVideoData.code }k -r {mCurrentConvertVideoData.frameRate} -y \"{mCurrentConvertVideoData.outVideoFileName}\"";
}
mFFMpegConvertVideoProcess.StartInfo.UseShellExecute = false;
mFFMpegConvertVideoProcess.StartInfo.RedirectStandardOutput = true;
mFFMpegConvertVideoProcess.StartInfo.CreateNoWindow = true;
mFFMpegConvertVideoProcess.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
mFFMpegConvertVideoProcess.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
mFFMpegConvertVideoProcess.StartInfo.RedirectStandardError = true;
mFFMpegConvertVideoProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
mFFMpegConvertVideoProcess.Start();
mIsConvertEnd = false;
mFFMpegConvertVideoProcess.BeginErrorReadLine();//开始异步读取
mFFMpegConvertVideoProcess.WaitForExit();
mIsConvertEnd = true;
mFFMpegConvertVideoProcess.Close();
mFFMpegConvertVideoProcess = null;
mCurrentConvertVideoData.isConvertEnd = true;
mCurrentConvertVideoData.thread.Abort();
}
private void Output(object sendProcess, DataReceivedEventArgs output)
{
if (!string.IsNullOrEmpty(output.Data))
{
string log = output.Data;
GetFFMpegProcessInfo(log);
if (mFFmpegVideoInfo != null && mFFmpegVideoInfo.nowProcessFrame > 20 && mFFmpegVideoInfo.videoDuration > 0)
{
float ConcatintProcessValue = mFFmpegVideoInfo.nowProcessTime / mFFmpegVideoInfo.videoDuration;
double j = Math.Round(ConcatintProcessValue, 2);
float tempValue = (float)j * 100.0f;
mCurrentProcessValue = tempValue;
//UnityEngine.Debug.Log("当前进度:" + tempValue);
}
}
}
private void Update()
{
if (mIsConvertEnd)
{
bool mIsError = false;
if (mFFmpegErrorList.Count > 0)
{
for (int i = 0; i < mFFmpegErrorList.Count; i++)
{
Debug.Log("mIsConvertEnd:"+ mFFmpegErrorList[i]);
switch (mFFmpegErrorList[i])
{
//case "Conversion failed!":
case "Input string was not in a correct format.":
SpringWindow.Instance.SetTipPanel("视频错误,请检查视频是否按照正确格式导出!");
break;
//case "Conversion failed!":
// SpringWindow.Instance.SetTipPanel("视频格式错误,默认转为普通视频!");
// break;
case "CreateInputBuffer failed!":
case "CreateInputBuffer failed":
case "CreateBitstreamBuffer failed!":
case "CreateBitstreamBuffer failed":
SpringWindow.Instance.SetTipPanel("视频读取失败,内存不足!");
break;
case "InitializeEncoder failed!":
case "InitializeEncoder failed":
SpringWindow.Instance.SetTipPanel("编辑器出错,请重新启动编辑器!");
break;
}
}
mIsError = true;
}
//如果没有错误,转换下一个
if (!mIsError)
{
//转换完成
PlaneVideoLoadTip.Instance.SetPanelState(false);
mIsConvertEnd = false;
mActionChangeEnd(true, mCurrentConvertVideoData.outVideoFileName);
}
else
{
//转换出错
mIsConvertEnd = false;
PlaneVideoLoadTip.Instance.SetPanelState(false);
mActionChangeEnd(false,"");
mCurrentConvertVideoData.isConvertEnd = false;
CloseGameObject();
Destroy(gameObject);
}
mCurrentProcessValue = 0.0f;
}
else
{
if (mCurrentProcessValue != 0.0f)
{
PlaneVideoLoadTip.Instance.SetProess(mCurrentProcessValue);
if (mCurrentProcessValue == 100)
{
mCurrentProcessValue = 0.0f;
}
}
}
}
private void DeleteElement()
{
Destroy(Instance.gameObject);
CloseGameObject();
}
#region 获取FFMpage转换数据
///
/// 获取控制进度
///
///
private void GetFFMpegProcessInfo(string output)
{
try
{
if (string.IsNullOrEmpty(output))
{
return;
}
if (mFFmpegVideoInfo == null)
{
mFFmpegVideoInfo = new FFmpegVideoInfo();
}
//UnityEngine.Debug.Log(output);
mDebugMessage = output;
//获取视频详细信息
if (output.Contains("Duration"))
{
// Duration: 00:00:57.30, start: 0.000000, bitrate: 107512 kb/s
var time = output.Split(',')[0].Remove(0, 11);
var data = time.Split(':');
mFFmpegVideoInfo.videoDuration = ((int.Parse(data[0].Trim()) * 60.0f * 60.0f) + (int.Parse(data[1].Trim()) * 60.0f) + float.Parse(data[2].Trim())) * 1000;
}
else if (output.Contains("Stream #0:0(und):Audio:"))
{
}
else if (output.Contains("Stream #0:0(und):"))
{
var video_info_group = output.Split(',');
if (video_info_group.Length > 7)
{
mFFmpegVideoInfo.resolution = video_info_group[2];
mFFmpegVideoInfo.bitRate = video_info_group[3];
mFFmpegVideoInfo.frameRate = int.Parse(video_info_group[4].Replace("fps", "").Trim());
mFFmpegVideoInfo.tbn = video_info_group[5].Replace("tbn", "");
mFFmpegVideoInfo.tbc = video_info_group[6].Replace("(default)", "");
mFFmpegVideoInfo.nowProcessTime = 0;
mFFmpegVideoInfo.nowProcessFrame = 0;
if (output.Contains("hevc"))
{
mFFmpegVideoInfo.videoType = "h265";
}
else if (output.Contains("h264"))
{
mFFmpegVideoInfo.videoType = "h264";
}
}
}
else if (output.Contains("fps") && output.Contains("bitrate") && output.Contains("time"))
{
int len = output.IndexOf("bitrate");
int start = output.IndexOf("time");
if (len - start - 5 > 0)
{
string time = output.Substring(start + 5, len - start - 5).Trim();
var data = time.Split(':');
//UnityEngine.Debug.Log("时间为" + time);
mFFmpegVideoInfo.nowProcessTime = ((int.Parse(data[0].Trim()) * 60.0f * 60.0f) + (int.Parse(data[1].Trim()) * 60.0f) + float.Parse(data[2].Trim())) * 1000;
}
len = output.IndexOf("fps");
// start = output.IndexOf("frame");
if (len - 6 > 0)
{
string Frame = output.Substring(6, len - 6).Trim();
//UnityEngine.Debug.Log("Frame" + Frame.ToString());
mFFmpegVideoInfo.nowProcessFrame = int.Parse(Frame);
}
}
//错误信息检测,内存不足
else if (output.Contains("CreateInputBuffer failed"))
{
string[] str = output.Split(':');
string reason = str[1];
mFFmpegErrorList.Add(reason);
}
//编译器出错
else if (output.Contains("InitializeEncoder failed"))
{
string[] str = output.Split(':');
string reason = str[1] + str[2];
mFFmpegErrorList.Add(reason);
}
//CreateBitstreamBuffer failed: out of memory (10):
else if (output.Contains("CreateBitstreamBuffer failed"))
{
string[] str = output.Split(':');
string reason = str[1];
mFFmpegErrorList.Add(reason);
}
else if (output.Contains("Conversion failed!"))
{
mFFmpegErrorList.Add(output);
}
else if (output.Contains("Error reinitializing filters!"))
{
mFFmpegErrorList.Add(output);
}
else if (output.Contains("Failed to inject frame into filter network"))
{
string[] str = output.Split(':');
string reason;
if (str.Length > 3)
{
reason = str[2];
}
else
{
reason = str[1];
}
mFFmpegErrorList.Add(reason);
}
else
{
}
}
catch (System.Exception e)
{
mIsConvertEnd = true;
mFFmpegErrorList.Add(e.Message.ToString());
Debug.Log("错误原因" + output);
Debug.Log("错误内容" + e.Message.ToString());
//PlaneVideoLoadTip.Instance.SetPanelState(false);
}
}
#endregion
#region 获取视频码率
public void GetcodeRate()
{
mRunFFmpegThread = new Thread(RunFFprobeEXE);
mRunFFmpegThread.Start();
}
private void RunFFprobeEXE()
{
Process p = new Process();
p.StartInfo.FileName = mFFprobePath;
p.StartInfo.Arguments = $" -show_format -of json -v quiet \"{ mCurrentVideoChangeData.videoPath}\"";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
string a = "";
a += p.StandardOutput.ReadToEnd();
mIsGetVideoRate = false;
p.WaitForExit();
JsonData Js = JsonMapper.ToObject(a);
string JsStr = Js["format"]["bit_rate"].ToString();
mCurrentConvertVideoData.code = int.Parse(JsStr) / 1024;
mIsGetVideoRate = true;
p.Close();
mRunFFmpegThread.Abort();
}
#endregion
///
/// 关闭
///
private void CloseGameObject()
{
mFFmpegErrorList.Clear();
if (mRunFFmpegThread != null)
{
mRunFFmpegThread.Abort();
mRunFFmpegThread = null;
}
if (mCurrentConvertVideoData != null)
{
if (!mCurrentConvertVideoData.isConvertEnd)
{
if (mFFMpegConvertVideoProcess != null)
{
mFFMpegConvertVideoProcess.Kill();
mFFMpegConvertVideoProcess.Close();
}
if (!string.IsNullOrEmpty(mCurrentConvertVideoData.outVideoFileName) && File.Exists(mCurrentConvertVideoData.outVideoFileName))
{
//if (!string.IsNullOrEmpty(mCurrentVideoChangeData.elementID))
//{
// ElementBehaviourMgr.Instance.DeleteElement(mCurrentVideoChangeData.elementID);
//}
//某种情况下会报错,没复现出来先加一个。
try
{
File.Delete(mCurrentConvertVideoData.outVideoFileName);
}
catch (Exception e)
{
UnityEngine.Debug.Log(e.Message);
}
}
}
if (mCurrentConvertVideoData.thread != null)
{
mCurrentConvertVideoData.thread.Abort();
mCurrentConvertVideoData.thread = null;
}
mCurrentConvertVideoData = null;
}
mCurrentProcessValue = 0.0f;
}
private void OnDestroy()
{
CloseGameObject();
}
}
///
/// 数据转换参数
///
[Serializable]
public class VideoChangeData
{
public string elementID;
///
/// 1:上下
/// 2:左右
///
public int splitType = 1;
///
/// 视频路径
///
public string videoPath = string.Empty;
}
[Serializable]
public class ConvertVideoData
{
public Thread thread;
public string inputMovFileName;
public string outVideoFileName;
public int code;
public bool isConvertEnd = false;
public int frameRate = 25;
}
[Serializable]
public class FFmpegVideoInfo
{
///
/// 码率
///
public string bitRate;
///
/// 视频类型 h264或者h265
///
public string videoType;
///
/// 视频帧率
///
public int frameRate;
///
/// 视频总时间
///
public float videoDuration;
///
/// 当前处理帧
///
public long nowProcessFrame;
///
/// 当前处理时间 毫秒(ms)
///
public float nowProcessTime;
///
/// 分辨率 宽x高
///
public string resolution;
public string tbc;
public string tbn;
}