[转]C# amr音频格式文件转换成mp3格式

微信语音都是使用amr音频格式的,这种格式文件放网页端是无法播放的,这时候需要后台转码成mp3格式

使用silk_v3_decoder.exe 和 lame.exe 文件通过cmd命令来转换成mp3格式

工具介绍: https://github.com/kn007/silk-v3-decoder/tree/master/windows

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
static void Main(string[] args)
{
    //取得当前工作目录的完整限定路径
    string currentWorkDir = Environment.CurrentDirectory;
    //silk文件完全路径
    string silkFilePath = Path.Combine(currentWorkDir, "silk""silk_v3_decoder.exe");
    //lame文件完全路径
    string lameFilePath = Path.Combine(currentWorkDir, "silk""lame.exe");
    //amr文件完全路径
    string amrFilePath = Path.Combine(currentWorkDir, "amr""msg.amr");
    //pcm文件完全路径
    string pcmFilePath = Path.Combine(currentWorkDir, "amr""msg.pcm");
    //mp3文件路径
    string mp3FilePath = Path.Combine(currentWorkDir, "amr", DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp3");
 
    //先生成pcm文件
    CmdHelper.ExecuteCmd(lamePath + "silk_v3_decoder.exe ", amrFilePath + " " + pcmFilePath);
    //生成mp3文件
    CmdHelper.ExecuteCmd(lamePath + "lame.exe"" -r -s 24000 --preset voice " + pcmFilePath + " " + mp3FilePath);
 
    //删除临时pcm文件
    if (File.Exists(pcmFilePath))
    {
        File.Delete(pcmFilePath);
    }
 
    Console.WriteLine("Hello World!");
    Console.ReadKey();
}
 
/// <summary>  
/// 执行CMD命令返回信息  
/// </summary>  
/// <param name="Command">命令</param>  
/// <returns>返回命令执行结果</returns>  
public static void ExecuteCmd(string fileName, string arg)
{
    //创建一个ProcessStartInfo对象 使用系统shell 指定命令和参数 设置标准输出
    var psi = new ProcessStartInfo(fileName, arg) { RedirectStandardOutput = true };
    //var psi = new ProcessStartInfo(Command) { RedirectStandardOutput = true };
    psi.UseShellExecute = false;
    //启动
    var proc = Process.Start(psi);
    if (proc == null)
    {
        Console.WriteLine("Can not exec.");
    }
    else
    {
        //开始读取
        using (var sr = proc.StandardOutput)
        {
            while (!sr.EndOfStream)
            {
                Console.WriteLine(sr.ReadLine());
            }
 
            if (!proc.HasExited)
            {
                proc.Kill();
            }
        }
    }
}

执行完就会生成对应的mp3文件了

源码下载: http://download.csdn.net/download/jx_521/10197078

转自:http://www.mikeblog.cn/article/details/112

posted @ 2021-12-29 11:48  面对疾风吧丶  阅读(695)  评论(0)    收藏  举报