C# 使用ffmpeg.exe进行音频转换完整demo


今天在处理微信的开发接口时候,发现微信多媒体上传接口中返回的音频格式是amr。坑人的是现在大部分的web 播放器,不支持amr的格式播放。试了很多方法都不行。

没办法,只要找一个妥协的解决方案:将amr转换成mp3再进行播放。

于是,我在网上找了不少的音频转换方案,其中有DIrectshow和 ffmpeg。DIrectshow使用起来比较复杂,我就采用ffmpeg。通过调用ffmpeg.exe传递命令参数的方式,进行音频格式的转换。

该程序,主要是封装了调用ffmpeg.exe,并执行命令参数的功能。

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.IO;
namespace  WavConvertAmr
{
     public  class  WavConvertToAmr
     {
         /// <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 ;
         }
     }     
}

调用代码:

1
2
3
4
5
6
7
8
9
10
11
12
         private  void  button3_Click( object  sender, EventArgs e)
         {
             OpenFileDialog open =  new  OpenFileDialog();
             if  (open.ShowDialog() == DialogResult.OK)
             {
                 string  fileName = open.FileName;
                 string  targetFileName =  "C:\\2222.mp3" ;
                 WavConvertAmr.WavConvertToAmr toamr =  new  WavConvertAmr.WavConvertToAmr();
                 toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName);
                 MessageBox.Show( "转换成功" );
             }
         }

示例下载: 

链接:http://pan.baidu.com/s/1jGxa4M6   密码:bnqw


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM