代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
利用lame.exe实现mp3编码
作者:
dezai
/ 发布于
2014/3/10
/
577
//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.事件、委托、线程 usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Text.RegularExpressions; usingSystem.Diagnostics; usingSystem.Threading; usingSystem.Windows.Forms; namespaceCSharpShell { publicdelegatevoideventDone(); publicdelegatevoideventProgress(LameProgressProgress); publicdelegatevoideventIgnoredLine(stringLine); publicdelegatevoideventCanceled(); publicclassShell { privateProcess_lameProcess=newProcess(); privateThread_lameThread; privateint_percentDone; privatebool_isRunning=false; privateProcessStartInfo_startInfo=newProcessStartInfo(); privateRegex_regLine=newRegex(@"(/d+)/(/d+)/D+(/d+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d:]+)/D+([/d/.]+)/D+([/d:]+)",RegexOptions.IgnoreCase |RegexOptions.CultureInvariant |RegexOptions.IgnorePatternWhitespace |RegexOptions.Compiled); privatestring_inFile; privatestring_outFile; privatestring_options; publiceventeventDoneDone; publiceventeventProgressProgress; publiceventeventIgnoredLineIgnoredLine; publiceventeventCanceledCanceled; publicShell() { DefStartInfo(); _startInfo.WorkingDirectory=Application.StartupPath; } publicShell(stringInFile,stringOutFile,stringLamePath,stringOptions) { DefStartInfo(); if(string.IsNullOrEmpty(LamePath)) _startInfo.WorkingDirectory=Application.StartupPath; _inFile=InFile; _outFile=OutFile; _options=Options; } privatevoidDefStartInfo() { _startInfo.FileName="lame.exe"; _startInfo.UseShellExecute=false; _startInfo.RedirectStandardOutput=true; _startInfo.RedirectStandardError=true; _startInfo.CreateNoWindow=true; } publicstringInFile { get{return_inFile;} set{_inFile=value;} } publicstringOutFile { get{return_outFile;} set{_outFile=value;} } publicstringOptions { get{return_options;} set{_options=value;} } publicstringLamePath { get{return_startInfo.WorkingDirectory;} set{_startInfo.WorkingDirectory=value;} } privatevoidReset() { stringarguments=""; _percentDone=0; _lameProcess=newProcess(); if(_options!="") arguments=_options+""; arguments+="/"/""+_inFile+"/"/""; if(_outFile!="") arguments+="/"/""+_outFile+"/"/""; _startInfo.Arguments=arguments; _lameProcess.StartInfo=_startInfo; } publicboolStart() { if(_isRunning||_inFile=="-"||_outFile=="-") returnfalse; _isRunning=true; Reset(); try { _lameThread=newThread(newThreadStart(LameReader)); _lameThread.IsBackground=true; _lameThread.Name="LameReader"; _lameThread.Start(); returntrue; } catch(Exceptionex) { returnfalse; } } privatevoidLameReader() { Matchm; stringoneLine; 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(newLameProgress(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(Exceptionex) { if(_lameProcess!=null) { _lameProcess.Close(); _lameProcess=null; } MessageBox.Show(ex.Message+"/n"+"Areyousurelame.exeishere:"+"/n"+_startInfo.WorkingDirectory); } finally { _isRunning=false; } } publicvoidCancel() { try { if(_lameProcess!=null) { _lameProcess.Kill(); } } catch { } } } publicclassLameProgress { privateMatch_m; publicintFrameCurrent { get{returnConvert.ToInt32(_m.Groups[1].Value);} } publicintFrameMax { get{returnConvert.ToInt32(_m.Groups[2].Value);} } publicintPercentDone { get { returnConvert.ToInt32(_m.Groups[3].Value); } } publicstringCPUTime { get { return_m.Groups[4].Value; } } publicstringCPUEstimate { get { return_m.Groups[5].Value; } } publicstringREALTime { get { return_m.Groups[6].Value; } } publicstringREALEstimate { get { return_m.Groups[7].Value; } } publicSinglePlayCPU { get { returnConvert.ToSingle(_m.Groups[8].Value); } } publicstringETA { get { return_m.Groups[9].Value; } } publicLameProgress(Matchm) { _m=m; } } } 下面是一个引用Shell的实例: 界面有一个progressbar(pBar),两个button(cmdStart,cmdCancel), 一个label(lblFeedback) usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; namespaceCSharpShell { publicpartialclassForm1:Form { privateShell_lameShell; publicForm1() { InitializeComponent(); } [STAThread] staticvoidMain() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(newForm1()); } privatevoidForm1_Load(objectsender,EventArgse) { _lameShell=newShell(); _lameShell.Done+=neweventDone(_lameShell_Done); _lameShell.Canceled+=neweventCanceled(_lameShell_Canceled); _lameShell.Progress+=neweventProgress(_lameShell_Progress); } privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse) { _lameShell.Cancel(); } privatevoidcmdStart_Click(objectsender,EventArgse) { _lameShell.InFile=Application.StartupPath+"//test.wav"; _lameShell.OutFile=Application.StartupPath+"//testOut.mp3"; _lameShell.Options="-h--abr128"; _lameShell.Start(); } privatevoidcmdCancel_Click(objectsender,EventArgse) { _lameShell.Cancel(); } privatevoid_lameShell_Done() { if(this.InvokeRequired) { this.Invoke(neweventDone(_lameShell_Done)); } else lblFeedback.Text="Done"; } privatevoid_lameShell_Canceled() { if(this.InvokeRequired) this.Invoke(neweventCanceled(_lameShell_Canceled)); else { lblFeedback.Text="Canceled/Error"; pBar.Value=0; } } privatevoid_lameShell_Progress(LameProgressProgress) { if(this.InvokeRequired) { this.Invoke(neweventProgress(_lameShell_Progress),newobject[]{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+"
试试其它关键字
mp3编码
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3