必須先下載 ffmpeg :http://www.ffmpeg.org/
或者下載我備份的 ffmpeg:https://www.dropbox.com/s/a1fb8jg3ktqdtvg/ffmpeg.exe

下載後將 ffmpeg.exe 丟到 Unity 專案資料夾,之後專案發布後也要再丟一次到發布的資料夾

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Threading;

public class NewBehaviourScript : MonoBehaviour {

    void Start () {
        new Thread (Run).Start ();
    }

    void Run(){
        ExcuteProcess("ffmpeg.exe", "-i A.wav A.mp3", (s, e) => print(e.Data));
    }

    void ExcuteProcess(string fileName, string arg, DataReceivedEventHandler output){
        using (var p = new Process()){
            p.StartInfo.FileName = fileName; // ffmpeg 路徑 & 檔案 名稱
            p.StartInfo.Arguments = arg; // ffmpeg 的指令,更多指令請參考 ffmpeg 官網 or Google

            p.StartInfo.UseShellExecute = false; // 重新定義輸出位置
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.OutputDataReceived += output;
            p.ErrorDataReceived += output;

            p.Start(); //啟動執行緒
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit(); //等待執行緒結束
        }
    }
}

arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(1) 人氣()