c#利用lame.exe实现mp3编码

来源:互联网 发布:js style.border 编辑:程序博客网 时间:2024/05/22 01:26

参考:VB.NET LameMP3 Shell

1.c#调用非c#编写的EXE:Process

   引用空间: System.Diagnostics;

   process.StartInfo.FileName="lame.exe";  //设置要启动的应用程序

   process.StartInfo.Arguments=" -h --abr 128  test.wav  testOut.mp3 ";//设置启动应用程序时的命令行参数

2.正则表达式匹配lame.exe输出的信息

   引用空间:System.Text.RegularExpressions;

   @"(/d+)/(/d+)/D+(/d+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d/.]+)/D+([/d:]+)"

3.事件、委托、线程

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Diagnostics ;
  6. using System.Threading ;
  7. using System.Windows.Forms;
  8. namespace CSharpShell
  9. {
  10.     public delegate void eventDone();
  11.     public delegate void eventProgress(LameProgress Progress);
  12.     public delegate void eventIgnoredLine(string Line);
  13.     public delegate void eventCanceled();
  14.     public class Shell
  15.     {
  16.         private Process _lameProcess = new Process();
  17.         private Thread _lameThread;
  18.         private int _percentDone;
  19.         private bool _isRunning = false;
  20.         private ProcessStartInfo _startInfo = new ProcessStartInfo();
  21.         private Regex _regLine=new Regex (@"(/d+)/(/d+)/D+(/d+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d/.]+)/D+([/d:]+)", RegexOptions.IgnoreCase 
  22.                 | RegexOptions.CultureInvariant 
  23.                 | RegexOptions.IgnorePatternWhitespace 
  24.                 | RegexOptions.Compiled );
  25.      
  26.         private string _inFile;
  27.         private string _outFile;
  28.         private string _options;
  29.         public event eventDone Done;
  30.         public event eventProgress Progress;
  31.         public event eventIgnoredLine IgnoredLine;
  32.         public event eventCanceled Canceled;
  33.         public Shell()
  34.         {
  35.             DefStartInfo();
  36.             _startInfo.WorkingDirectory = Application.StartupPath;
  37.         }
  38.         public Shell(string InFile, string OutFile, string LamePath, string Options)
  39.         {
  40.             DefStartInfo();
  41.             if (string.IsNullOrEmpty(LamePath))
  42.                 _startInfo.WorkingDirectory = Application.StartupPath;
  43.             _inFile = InFile;
  44.             _outFile = OutFile;
  45.             _options = Options;
  46.         }
  47.         private void DefStartInfo()
  48.         {
  49.             _startInfo.FileName = "lame.exe";
  50.             _startInfo.UseShellExecute = false;
  51.             _startInfo.RedirectStandardOutput = true;
  52.             _startInfo.RedirectStandardError = true;
  53.             _startInfo.CreateNoWindow = true;
  54.         }
  55.         public string InFile
  56.         {
  57.             get { return _inFile; }
  58.             set { _inFile = value; }
  59.         }
  60.         public string OutFile
  61.         {
  62.             get { return _outFile; }
  63.             set { _outFile = value; }
  64.         }
  65.         public string Options
  66.         {
  67.             get { return _options; }
  68.             set { _options = value; }
  69.         }
  70.         public string LamePath
  71.         {
  72.             get { return _startInfo.WorkingDirectory; }
  73.             set { _startInfo.WorkingDirectory = value; }
  74.         }
  75.         private void Reset()
  76.         {
  77.             string arguments="";
  78.             _percentDone = 0;
  79.             _lameProcess = new Process();
  80.             if (_options != "")
  81.                 arguments = _options + " ";
  82.             arguments += "/"/"" + _inFile + "/"/" ";
  83.             if (_outFile != "")
  84.                 arguments += "/"/"" + _outFile + "/"/"";
  85.             _startInfo.Arguments = arguments;
  86.             _lameProcess.StartInfo = _startInfo;
  87.         }
  88.         public bool Start()
  89.         {
  90.             if (_isRunning || _inFile == "-" || _outFile == "-")
  91.                 return false;
  92.             _isRunning = true ;
  93.             Reset();
  94.             try
  95.             {
  96.                 _lameThread = new Thread(new ThreadStart(LameReader));
  97.                 _lameThread.IsBackground = true;
  98.                 _lameThread.Name = "LameReader";
  99.                 _lameThread.Start();
  100.                 return true;
  101.             }
  102.             catch (Exception ex)
  103.             {
  104.                 return false;
  105.             }           
  106.             
  107.         }
  108.         private void LameReader()
  109.         {
  110.             Match m;
  111.             string oneLine;
  112.             try
  113.             {
  114.                 _lameProcess.Start();
  115.                 oneLine = _lameProcess.StandardError.ReadLine(); 
  116.                 while (_lameProcess .StandardError .Peek ()!=0&&oneLine !=null )
  117.                 {
  118.                     m = _regLine.Match(oneLine);
  119.                     if (m.Success)
  120.                     {
  121.                         _percentDone = Convert.ToInt32(m.Groups[3].Value);
  122.                         if (Progress != null)
  123.                             Progress(new LameProgress(m));
  124.                     }                  
  125.                     else
  126.                     {
  127.                         if (IgnoredLine != null)
  128.                             IgnoredLine(oneLine);
  129.                     }                                                                    
  130.                     oneLine = _lameProcess.StandardError.ReadLine();
  131.                 }
  132.                 _lameProcess.Close();
  133.                 _lameProcess = null;
  134.                 if(_percentDone == 100 )
  135.                 {
  136.                     if (Done != null)
  137.                         Done();
  138.                 }
  139.                 else 
  140.                 {
  141.                     if (Canceled != null)
  142.                         Canceled();
  143.                 }
  144.             }
  145.             catch (Exception ex)
  146.             {
  147.                 if (_lameProcess != null)
  148.                 {
  149.                     _lameProcess.Close();
  150.                     _lameProcess = null;
  151.                 }
  152.                 MessageBox.Show(ex.Message + "/n" + "Are you sure lame.exe is here:" + "/n" + _startInfo.WorkingDirectory);
  153.             }
  154.             finally
  155.             {
  156.                 _isRunning = false;
  157.             }
  158.         }
  159.         public void Cancel()
  160.         {
  161.             try
  162.             {
  163.                 if (_lameProcess != null)
  164.                 {
  165.                     _lameProcess.Kill();
  166.                 }
  167.             }
  168.             catch
  169.             {
  170.             }
  171.         }
  172.     }
  173.     public class LameProgress
  174.     {
  175.         private Match _m;
  176.         public int FrameCurrent
  177.         {
  178.             get { return Convert.ToInt32(_m.Groups[1].Value); }
  179.         }
  180.         public int FrameMax
  181.         {
  182.             get { return Convert.ToInt32(_m.Groups[2].Value); }
  183.         }
  184.         public int PercentDone
  185.         {
  186.             get
  187.             {
  188.                 return Convert.ToInt32(_m.Groups[3].Value);
  189.             }
  190.         }
  191.         public string CPUTime
  192.         {
  193.             get 
  194.             {
  195.                 return _m.Groups[4].Value;                
  196.             }
  197.         }
  198.         public string CPUEstimate
  199.         {
  200.             get
  201.             {
  202.                 return _m.Groups[5].Value;
  203.             }
  204.         }
  205.         public string REALTime
  206.         {
  207.             get
  208.             {
  209.                 return _m.Groups[6].Value;
  210.             }
  211.         }
  212.         public string REALEstimate
  213.         {
  214.             get 
  215.             {
  216.                 return _m.Groups[7].Value;
  217.             }
  218.         }
  219.         public Single  PlayCPU
  220.         {
  221.             get 
  222.             {                
  223.                 return Convert.ToSingle(_m.Groups[8].Value);
  224.             }
  225.         }
  226.         public string ETA
  227.         {
  228.             get
  229.             {
  230.                 return _m.Groups[9].Value;               
  231.             }
  232.         }
  233.         public LameProgress(Match m)
  234.         {
  235.             _m = m;
  236.         }
  237.     }
  238. }

 下面是一个引用Shell的实例:

界面有一个progressbar(pBar),两个button(cmdStart,cmdCancel), 一个label (lblFeedback)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace CSharpShell
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         private Shell _lameShell;
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         [STAThread]
  18.         static void Main()
  19.         {
  20.             Application.EnableVisualStyles();
  21.             Application.SetCompatibleTextRenderingDefault(false);
  22.             Application.Run(new Form1());
  23.         }
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             _lameShell = new Shell();
  27.             _lameShell.Done += new eventDone(_lameShell_Done);
  28.             _lameShell .Canceled +=new eventCanceled(_lameShell_Canceled);
  29.             _lameShell .Progress +=new eventProgress(_lameShell_Progress);         
  30.            
  31.         }
  32.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  33.         {
  34.             _lameShell.Cancel();
  35.             
  36.         }
  37.         private void cmdStart_Click(object sender, EventArgs e)
  38.         {
  39.             _lameShell.InFile = Application.StartupPath + "//test.wav";
  40.             _lameShell.OutFile = Application.StartupPath + "//testOut.mp3";
  41.             _lameShell.Options = " -h --abr 128 ";
  42.             _lameShell.Start();
  43.         }
  44.         private void cmdCancel_Click(object sender, EventArgs e)
  45.         {
  46.             _lameShell.Cancel();
  47.         }
  48.         
  49.         private void _lameShell_Done()
  50.         {
  51.             if (this.InvokeRequired)
  52.             {
  53.                 this.Invoke(new eventDone(_lameShell_Done));
  54.             }
  55.             else
  56.                 lblFeedback.Text = "Done";
  57.         }
  58.         private void _lameShell_Canceled()
  59.         {
  60.             if (this.InvokeRequired)
  61.                 this.Invoke(new eventCanceled(_lameShell_Canceled));
  62.             else
  63.             {
  64.                 lblFeedback.Text = "Canceled/Error";
  65.                 pBar.Value = 0;
  66.             }
  67.         }
  68.         
  69.         private void _lameShell_Progress(LameProgress Progress)
  70.         {
  71.             if (this.InvokeRequired)
  72.             {
  73.                 this.Invoke(new eventProgress(_lameShell_Progress), new object[] { Progress });
  74.             }
  75.             else
  76.             {
  77.                 if (pBar.Maximum != Progress.FrameMax)
  78.                 {
  79.                     pBar.Value = 0;
  80.                     pBar.Maximum = Progress.FrameMax;
  81.                 }
  82.                 else
  83.                 {
  84.                     pBar.Value = Progress.FrameCurrent;
  85.                 }
  86.                 lblFeedback.Text = Progress.PercentDone + "%" + " ETA:" + Progress.ETA;               
  87.             }            
  88.         }        
  89.     }
  90. }

 至于mp3 编码,看完上面的文章您肯定也会了,只要匹配正则就可以了.   @"/D+(/d+)/(/d+)(/d+)/D+"

原创粉丝点击