很多时候我们需要写一个shell脚本去执行一系列操作,例如:新建文件、文件移动、重命名以及调用其他程序。尤其unity开发者,常常有需求,截取摄影机镜头、录制视频等,我们会使用ffmpeg等其他工具合成gif图片或视频等等。。
贴上示例代码:
ProcessStartInfo psi = new ProcessStartInfo()
psi.FileName = "/usr/local/bin/ffmpeg"
psi.UseShellExecute = false
psi.RedirectStandardOutput = true
psi.Arguments = " -f image2 -framerate 30 -i /Users/muyichun/Desktop/video2/shot%d.png /Users/muyichun/Desktop/video3/1.gif"
Process p = Process.Start(psi)
string strOutput = p.StandardOutput.ReadToEnd()
p.WaitForExit()
UnityEngine.Debug.Log(strOutput)
ProcessStartInfo psi = new ProcessStartInfo()
psi.FileName = "/bin/sh"
psi.UseShellExecute = false
psi.RedirectStandardOutput = true
psi.Arguments = Application.dataPath + "/test.sh" + " arg1 arg2"
Process p = Process.Start(psi)
string strOutput = p.StandardOutput.ReadToEnd()
p.WaitForExit()
UnityEngine.Debug.Log(strOutput)
test.sh文件:
echo $1
/usr/local/bin/ffmpeg -f image2 -framerate 30 -i /Users/muyichun/Desktop/video2/shot%d.png /Users/muyichun/Desktop/video3/1.gif
/usr/local/bin/ffmpeg -f image2 -framerate 30 -i /Users/muyichun/Desktop/video2/shot%d.png /Users/muyichun/Desktop/video3/2.gif
echo $2
最后:笔者在Mac Os上测试没问题,window上基本一致!