c#利用lame.exe实现mp3编码
来源:互联网 发布:js style.border 编辑:程序博客网 时间:2024/05/22 01:26
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.事件、委托、线程
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Diagnostics ;
- using System.Threading ;
- using System.Windows.Forms;
- namespace CSharpShell
- {
- public delegate void eventDone();
- public delegate void eventProgress(LameProgress Progress);
- public delegate void eventIgnoredLine(string Line);
- public delegate void eventCanceled();
- public class Shell
- {
- private Process _lameProcess = new Process();
- private Thread _lameThread;
- private int _percentDone;
- private bool _isRunning = false;
- private ProcessStartInfo _startInfo = new ProcessStartInfo();
- private Regex _regLine=new Regex (@"(/d+)/(/d+)/D+(/d+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d/.]+)/D+([/d:]+)", RegexOptions.IgnoreCase
- | RegexOptions.CultureInvariant
- | RegexOptions.IgnorePatternWhitespace
- | RegexOptions.Compiled );
- private string _inFile;
- private string _outFile;
- private string _options;
- public event eventDone Done;
- public event eventProgress Progress;
- public event eventIgnoredLine IgnoredLine;
- public event eventCanceled Canceled;
- public Shell()
- {
- DefStartInfo();
- _startInfo.WorkingDirectory = Application.StartupPath;
- }
- public Shell(string InFile, string OutFile, string LamePath, string Options)
- {
- DefStartInfo();
- if (string.IsNullOrEmpty(LamePath))
- _startInfo.WorkingDirectory = Application.StartupPath;
- _inFile = InFile;
- _outFile = OutFile;
- _options = Options;
- }
- private void DefStartInfo()
- {
- _startInfo.FileName = "lame.exe";
- _startInfo.UseShellExecute = false;
- _startInfo.RedirectStandardOutput = true;
- _startInfo.RedirectStandardError = true;
- _startInfo.CreateNoWindow = true;
- }
- public string InFile
- {
- get { return _inFile; }
- set { _inFile = value; }
- }
- public string OutFile
- {
- get { return _outFile; }
- set { _outFile = value; }
- }
- public string Options
- {
- get { return _options; }
- set { _options = value; }
- }
- public string LamePath
- {
- get { return _startInfo.WorkingDirectory; }
- set { _startInfo.WorkingDirectory = value; }
- }
- private void Reset()
- {
- string arguments="";
- _percentDone = 0;
- _lameProcess = new Process();
- if (_options != "")
- arguments = _options + " ";
- arguments += "/"/"" + _inFile + "/"/" ";
- if (_outFile != "")
- arguments += "/"/"" + _outFile + "/"/"";
- _startInfo.Arguments = arguments;
- _lameProcess.StartInfo = _startInfo;
- }
- public bool Start()
- {
- if (_isRunning || _inFile == "-" || _outFile == "-")
- return false;
- _isRunning = true ;
- Reset();
- try
- {
- _lameThread = new Thread(new ThreadStart(LameReader));
- _lameThread.IsBackground = true;
- _lameThread.Name = "LameReader";
- _lameThread.Start();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- private void LameReader()
- {
- Match m;
- string oneLine;
- try
- {
- _lameProcess.Start();
- oneLine = _lameProcess.StandardError.ReadLine();
- while (_lameProcess .StandardError .Peek ()!=0&&oneLine !=null )
- {
- m = _regLine.Match(oneLine);
- if (m.Success)
- {
- _percentDone = Convert.ToInt32(m.Groups[3].Value);
- if (Progress != null)
- Progress(new LameProgress(m));
- }
- else
- {
- if (IgnoredLine != null)
- IgnoredLine(oneLine);
- }
- oneLine = _lameProcess.StandardError.ReadLine();
- }
- _lameProcess.Close();
- _lameProcess = null;
- if(_percentDone == 100 )
- {
- if (Done != null)
- Done();
- }
- else
- {
- if (Canceled != null)
- Canceled();
- }
- }
- catch (Exception ex)
- {
- if (_lameProcess != null)
- {
- _lameProcess.Close();
- _lameProcess = null;
- }
- MessageBox.Show(ex.Message + "/n" + "Are you sure lame.exe is here:" + "/n" + _startInfo.WorkingDirectory);
- }
- finally
- {
- _isRunning = false;
- }
- }
- public void Cancel()
- {
- try
- {
- if (_lameProcess != null)
- {
- _lameProcess.Kill();
- }
- }
- catch
- {
- }
- }
- }
- public class LameProgress
- {
- private Match _m;
- public int FrameCurrent
- {
- get { return Convert.ToInt32(_m.Groups[1].Value); }
- }
- public int FrameMax
- {
- get { return Convert.ToInt32(_m.Groups[2].Value); }
- }
- public int PercentDone
- {
- get
- {
- return Convert.ToInt32(_m.Groups[3].Value);
- }
- }
- public string CPUTime
- {
- get
- {
- return _m.Groups[4].Value;
- }
- }
- public string CPUEstimate
- {
- get
- {
- return _m.Groups[5].Value;
- }
- }
- public string REALTime
- {
- get
- {
- return _m.Groups[6].Value;
- }
- }
- public string REALEstimate
- {
- get
- {
- return _m.Groups[7].Value;
- }
- }
- public Single PlayCPU
- {
- get
- {
- return Convert.ToSingle(_m.Groups[8].Value);
- }
- }
- public string ETA
- {
- get
- {
- return _m.Groups[9].Value;
- }
- }
- public LameProgress(Match m)
- {
- _m = m;
- }
- }
- }
下面是一个引用Shell的实例:
界面有一个progressbar(pBar),两个button(cmdStart,cmdCancel), 一个label (lblFeedback)
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace CSharpShell
- {
- public partial class Form1 : Form
- {
- private Shell _lameShell;
- public Form1()
- {
- InitializeComponent();
- }
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- _lameShell = new Shell();
- _lameShell.Done += new eventDone(_lameShell_Done);
- _lameShell .Canceled +=new eventCanceled(_lameShell_Canceled);
- _lameShell .Progress +=new eventProgress(_lameShell_Progress);
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- _lameShell.Cancel();
- }
- private void cmdStart_Click(object sender, EventArgs e)
- {
- _lameShell.InFile = Application.StartupPath + "//test.wav";
- _lameShell.OutFile = Application.StartupPath + "//testOut.mp3";
- _lameShell.Options = " -h --abr 128 ";
- _lameShell.Start();
- }
- private void cmdCancel_Click(object sender, EventArgs e)
- {
- _lameShell.Cancel();
- }
- private void _lameShell_Done()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new eventDone(_lameShell_Done));
- }
- else
- lblFeedback.Text = "Done";
- }
- private void _lameShell_Canceled()
- {
- if (this.InvokeRequired)
- this.Invoke(new eventCanceled(_lameShell_Canceled));
- else
- {
- lblFeedback.Text = "Canceled/Error";
- pBar.Value = 0;
- }
- }
- private void _lameShell_Progress(LameProgress Progress)
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new eventProgress(_lameShell_Progress), new object[] { Progress });
- }
- else
- {
- if (pBar.Maximum != Progress.FrameMax)
- {
- pBar.Value = 0;
- pBar.Maximum = Progress.FrameMax;
- }
- else
- {
- pBar.Value = Progress.FrameCurrent;
- }
- lblFeedback.Text = Progress.PercentDone + "%" + " ETA:" + Progress.ETA;
- }
- }
- }
- }
至于mp3 编码,看完上面的文章您肯定也会了,只要匹配正则就可以了. @"/D+(/d+)/(/d+)(/d+)/D+"
- c#利用lame.exe实现mp3编码
- LAME-MP3编码引擎(音频)
- lame mp3
- mp3 lame
- Visual Studio下用LAME将wav编码为mp3
- 基于lame对mp3进行分割的简单实现
- 基于lame对mp3进行分割的简单实现
- 基于lame对mp3进行分割的简单实现
- 基于lame对mp3进行分割的简单实现
- java+lame实现wav到mp3的转换
- PortAudio+webrtc+lame实现采集降噪增益mp3
- PortAudio+webrtc+lame实现采集降噪增益mp3
- lame编程:MP3解码
- mp3编码器lame命令行
- lame wav mp3
- lame.exe 参数说明
- ffmpeg开发之旅(4):MP3编码格式分析与lame库编译封装
- ffmpeg开发之旅(4):MP3编码格式分析与lame库编译封装
- WebStart啟動程式在關閉時關閉javaw進程
- Java Properties 类读取配置文件信息
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 看似简单的小问题
- getAttribute,setAttribute用法示例
- c#利用lame.exe实现mp3编码
- VisualAssist 10.4.1647.0 - Cracked.DLL - Av0id
- EditPlus配置C#运行环境一
- Google Earth 发布北京奥运场馆卫星图
- C#将类文件编译为dll文件
- bizgreat jewelry
- 扩展DataGrid
- C#实现P2P应用程序(转)
- 大数的加减乘除