如何使用ffmpeg进行音频文件合并

public class FolderHelper
  {
    /// 
    /// 获取某个目录下的文件夹
    /// 
    /// 
    /// 
    public string[] GetFolder(string aFolder) {
      string[] bFolders = Directory.GetDirectories(aFolder);
      if (bFolders.Length > 0)
      {
        //并非最底层文件夹
        Array.Sort(bFolders);  //文件夹排序

        return bFolders;
      }
      else
      {
        //已经是最底层文件夹
        return null;
      }
    }

    /// 
    /// 逐步深入目录,找到最底层目录
    /// 进行音频合并
    /// 
    /// 
    /// 
    public void GetLeafFolder(string aFolder,string[] aFolders,bool aIsApp) {
      if (aFolders != null)
      {
        foreach (var item in aFolders)
        {
          string[] bFoldersTmp = GetFolder(item);
          if (bFoldersTmp != null)
          {
            GetLeafFolder(aFolder,bFoldersTmp, aIsApp);
          }
          else
          {
            //末级文件夹,需要进行音频合并处理
            if (aIsApp)
            {
              AppRecord(item);
            }
            else
            {
              WxRecord(item);
            }
          }
        }
      }
      else
      {
        if (aIsApp)
        {
          AppRecord(aFolder);
        }
        else
        {
          WxRecord(aFolder);
        }
      }
    }

    /// 
    /// 微信录音合并处理
    /// 
    /// 
    public void WxRecord(string aFolder) {
      try
      {
        //判断是否已经进行过处理,对已经处理过的不再重复处理
        string bResultFile = $@"{aFolder}/record.mp3";
        if (File.Exists(bResultFile)) {
          //对已经处理过的不再重复处理
          return;
        }
        
        string ffmpegPath = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
        ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegPath);
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = true;  //不显示dos程序窗口

        StringBuilder bArguments = new StringBuilder("-i \"concat:");  //命令拼接
        //获取要处理的文件
        DirectoryInfo bDicInfo = new DirectoryInfo(aFolder);
        FileInfo[] bFiles = bDicInfo.GetFiles("*.mp3");
        //微信录音按创建时间排序
        Array.Sort(bFiles, (FileInfo x, FileInfo y) => x.CreationTime.CompareTo(y.CreationTime));
        foreach (var item in bFiles) {
          //命令拼接
          bArguments.Append(item.FullName + "|");
        }
        bArguments.Remove(bArguments.Length - 1, 1);
        bArguments.Append("\" -acodec copy " + aFolder + "/record.mp3");

        //准备执行命令,进行音频合并
        using (Process p = new Process()) {
          startInfo.Arguments = bArguments.ToString();
          p.StartInfo = startInfo;
          p.Start();
          while (!p.HasExited)
          {
            Thread.Sleep(100);
          }
        }

        //删除录音片段
        foreach (var item in bFiles)
        {
          File.Delete(item.FullName);
        }
      }
      catch (Exception e) {

      }
      finally
      {

      }
    }

    /// 
    /// App录音合并处理
    /// 
    /// 
    public void AppRecord(string aFolder) {
      try
      {
        //判断是否已经进行过处理,对已经处理过的不再重复处理
        string bResultFile = $@"{aFolder}/record.mp3";
        if (File.Exists(bResultFile))
        {
          //对已经处理过的不再重复处理
          return;
        }

        string bTmpPath = @"/Tmp/";  //临时保存目录
        if (!Directory.Exists($@"{aFolder}/Tmp"))
        {
          Directory.CreateDirectory($@"{aFolder}/Tmp");
        }
        string ffmpegPath = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
        ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegPath);
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = true;  //不显示dos程序窗口

        //0.获取需要处理的文件
        DirectoryInfo bDicInfo = new DirectoryInfo(aFolder);
        FileInfo[] bFiles = bDicInfo.GetFiles("*.mp3");
        //微信录音按创建时间排序
        Array.Sort(bFiles, (FileInfo x, FileInfo y) => x.Name.CompareTo(y.Name));
        List<string> bFilePaths = new List<string>();
        foreach (var file in bFiles) {
          bFilePaths.Add(file.FullName);
        }
        
        //string bFinalArguments = "-i \"concat: ";
        StringBuilder bFinalArguments = new StringBuilder("-i \"concat:");
        foreach (var file in bFilePaths)
        {
          string[] bFileStr = file.Split('\\');
          string bFileName = bFileStr[bFileStr.Length - 1].Split('.')[0];
          string bFileWavName = $@"{aFolder}{bTmpPath}{bFileName}.wav";
          //1.将无法识别的mp3文件先转成wav文件
          using (Process p = new Process()) {
            startInfo.Arguments = "-i " + file + " " + bFileWavName;
            p.StartInfo = startInfo;
            p.Start();
            while (!p.HasExited)
            {
              Thread.Sleep(100);
            }
          } 
          

          //2.将wav文件转换回mp3文件
          string bFileMp3Name = $@"{aFolder}{bTmpPath}{bFileName}.mp3";
          using (Process p = new Process())
          {
            startInfo.Arguments = "-i " + bFileWavName + " " + bFileMp3Name;
            p.StartInfo = startInfo;
            p.Start();
            while (!p.HasExited)
            {
              Thread.Sleep(100);
            }
          }
          
          bFinalArguments.Append(bFileMp3Name + "|");
        }
        bFinalArguments.Remove(bFinalArguments.Length - 1, 1);
        bFinalArguments.Append("\" -acodec copy " + aFolder + "/record.mp3");

        //3.进行文件合并
        using (Process p = new Process())
        {
          startInfo.Arguments = bFinalArguments.ToString();
          p.StartInfo = startInfo;
          p.Start();
          while (!p.HasExited)
          {
            Thread.Sleep(100);
          }
        }
        
        //4.删除临时文件
        DirectoryInfo di = new DirectoryInfo($@"{aFolder}/Tmp");
        di.Delete(true);

        //5.删除录音片段
        foreach (var file in bFilePaths) {
          File.Delete(file);
        }
      }
      catch (Exception e)
      {

      }
      finally
      {

      }
    }
  }

你可能感兴趣的:(C#,ffmpeg,开发语言)