0
点赞
收藏
分享

微信扫一扫

ASP.NET 下载微信录音AMR文件,使用FFmpeg实现AMR转换MP3示例源码


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeChatAudioUtils
{
public class Program
{
public static void Main(string[] args)
{
// Token
string access_token = "";

// 音频id
string media_id = "";

// 请求URL
string reqURL = $"https://api.weixin.qq.com/cgi-bin/media/get?access_token={access_token}&media_id={media_id}";

// 本地测试转换amr文件路径
//string downPath = Path.GetFullPath("../../download/132532654780617863.amr");

// 下载字节流文件
string downPath = AudioUtils.DownByteFile(reqURL);

// 判断:文件下载状态,downPath不为空,则为下载成功
if (!string.IsNullOrWhiteSpace(downPath))
{
// AMR文件保存路径
Console.WriteLine("AMR:" + downPath);

// MP3保存文件路径
string mp3Path = Path.GetFullPath("../../download/" + DateTime.Now.ToFileTime() + ".mp3");

// 音频转MP3状态
bool isConvert = AudioUtils.ConvertToMp3(downPath, mp3Path);

// 判断:文件转换状态
if (isConvert)
{
// MP3保存文件路径
Console.WriteLine("MP3:" + mp3Path);
}
else
{
Console.WriteLine("转换失败!");
}
}
else
{
Console.WriteLine("下载失败!");
}

Console.ReadKey();
}


}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Net;
using System.Diagnostics;

namespace WeChatAudioUtils
{
public class AudioUtils
{
/// <summary>
/// 下载字节流文件
/// </summary>
public static string DownByteFile(string reqURL)
{
//初始化WebClient对象
WebClient webClient = new WebClient();

//获取文件字节流
byte[] respData = webClient.DownloadData(reqURL);

//保存文件路径
string savePath = Path.GetFullPath("../../download/" + DateTime.Now.ToFileTime() + ".amr");

bool isDown = ConvertByteToFile(respData, savePath);
if (isDown)
{
return savePath;
}
else
{
return "";
}
}

/// <summary>
/// 将字节流保存为文件
/// </summary>
/// <param name="readByte"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool ConvertByteToFile(byte[] readByte, string fileName)
{
FileStream fileStream = null;
try
{
fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
fileStream.Write(readByte, 0, readByte.Length);
}
catch
{
return false;
}

finally
{
if (fileStream != null)
fileStream.Close();
}

return true;

}

public static bool ConvertToMp3(string amrPath, string mp3Path)
{
string c = Path.GetFullPath("../../tools/ffmpeg/bin/") + @"ffmpeg.exe -i " + amrPath + " " + mp3Path;
string output = "";
RunCmd(c, out output);
if (!string.IsNullOrEmpty(output))
{
if (File.Exists(mp3Path))
return true;
else
return false;
}
else
return false;
}

/// <summary>
/// 执行Cmd命令
/// </summary>
private static void RunCmd(string cmd, out string output)
{
cmd = cmd.Trim().TrimEnd('&') + "&exit";
using (Process p = new Process())
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
p.StartInfo.CreateNoWindow = true; //不显示程序窗口
p.Start();//启动程序
//向cmd窗口写入命令
p.StandardInput.WriteLine(cmd);
p.StandardInput.AutoFlush = true;
//获取cmd窗口的输出信息
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}
}
}
}

 

举报

相关推荐

0 条评论