博主喝口茶,一毛也是爱

收缩

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

2871 人阅读
分类:

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

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

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

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


和博主交个朋友吧
    发布篇幅
    • 文章总数:0
    • 原创:0
    • 转载:0
    • 译文:0
    文章分类
      文章存档
      阅读排行