.net 使用ffmpeg.exe进行音频转码

        #region 音频转换
        private int AudioIntervalTime = 100, iAudio = 0;
        private string strPath = "D:\\web\\XXXX\\voice\\";
        /// <summary>
        /// 线程委托
        /// </summary>
        private delegate void DelegateAudio();
        /// <summary>
        /// 定时器
        /// </summary>
        System.Threading.Timer timeAudio;
        /// <summary>
        /// 定义线程
        /// </summary>
        private void TimeThreadAudio()
        {
            Thread thread = new Thread(ThreadAudio);
            thread.Name = "Audio";
            thread.IsBackground = true;
            thread.Start();
        }
        /// <summary>
        /// 线程委托
        /// </summary>
        private void ThreadAudio()
        {
            DelegateAudio dp = new DelegateAudio(ThreadAudioFunction);
            dp.BeginInvoke(null, null);
        }
        /// <summary>
        /// 线程定时调整
        /// </summary>
        private void ThreadAudioFunction()
        {
            timeAudio = new System.Threading.Timer(new System.Threading.TimerCallback(AudioFunction), null, 0, 0);
        }
        /// <summary>
        /// 线程工作方法体
        /// </summary>
        /// <param name="sender"></param>
        private void AudioFunction(object sender)
        {
            timeAudio.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
            try
            {
                Dictionary<string, string> list = new RedisTool().GetAllEntriesFromHash(SysConst.str_H_Rd_AudioConvert);
                foreach (var item in list)
                {
                    string fileName = strPath + item.Key;
                    string targetFileName = strPath + item.Key.Replace("amr","mp3"); ;
                    WavConvertToAmrHelp toamr = new WavConvertToAmrHelp();
                    toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName);
                    ShowContent(string.Format("当前时间:{0};音频文件:{1};转换成功;\r\n", DateTime.Now.ToString(), item.Key), rtbAudioContent, iAudio++);
                    new RedisTool().RemoveEntryFromHash(SysConst.str_H_Rd_AudioConvert, item.Key);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Save(ex);
                ShowContent(DateTime.Now.ToString() + "\r\n" + ex.ToString() + "\r\n", rtbAudioContent, iAudio);
            }
            timeAudio.Change(AudioIntervalTime, AudioIntervalTime);
        }
        #endregion

  

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SZ.Company.Common
{
    /// <summary>
    /// 音频转换工具类
    /// </summary>
    public class WavConvertToAmrHelp
    {
        /// <summary>
        /// 将Wav音频转成Amr手机音频
        /// </summary>
        /// <param name="applicationPath">ffmeg.exe文件路径</param>
        /// <param name="fileName">WAV文件的路径(带文件名)</param>
        /// <param name="targetFilName">生成目前amr文件路径(带文件名)</param>
        public void ConvertToAmr(string applicationPath, string fileName, string targetFilName)
        {
            string c = applicationPath + @"\ffmpeg.exe -y -i " + fileName + " -ar 8000 -ab 12.2k -ac 1 " + targetFilName;
            Cmd(c);
        }

        /// <summary>
        /// 执行Cmd命令
        /// </summary>
        private void Cmd(string c)
        {
            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.Start();

                process.StandardInput.WriteLine(c);
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine("exit");

                StreamReader reader = process.StandardOutput;//截取输出流           

                process.WaitForExit();
            }
            catch
            { }
        }

        /// <summary>
        /// 获取文件的byte[]
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public byte[] GetFileByte(string fileName)
        {
            FileStream pFileStream = null;
            byte[] pReadByte = new byte[0];
            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }

        /// <summary>
        /// 将文件的byte[]生成文件
        /// </summary>
        /// <param name="pReadByte"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool writeFile(byte[] pReadByte, string fileName)
        {
            FileStream pFileStream = null;
            try
            {
                pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
                pFileStream.Write(pReadByte, 0, pReadByte.Length);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
            return true;

        }
    }     
}

 

你可能感兴趣的:(.net 使用ffmpeg.exe进行音频转码)