我的需求是手机上传录音。客户因手机上传的文件是m4a录音文件,按需求方要求,只要是m4a文件统一转成mp3格式上传,并且保留m4a格式文件。这两种方法跟自己业务有关,希望能帮到相同业务需求的朋友
第一个方法:
直接改后缀名,这种编码格式肯定有问题,如果只是电脑播放的话,应该没啥问题,其他平台播放可能兼容不好。
第二种方法:
工具下载地址:百度网盘 请输入提取码
提取码:xi9f
正经转换为mp3格式,那就使用到ffmpeg.exe工具转换。
下面两个方法只是转文件的方法,与业务无关(如果只是转格式只需看这里)。上代码:
调用:
static void Main(string[] args)
{
var exePath = @"D:desktopM4aToMp3M4aToMp3ffmpeg.exe"; //转换工具地址
var m4aPath = @"D:desktopM4aToMp3M4aToMp3filesqwe.m4a"; //源文件地址,不知道为啥复制过来的路径不好使,但是得纯手打才管用
var mp3Path = @"D:desktopM4aToMp3M4aToMp3filesqwe.mp3"; //转换目标地址可直接命名自定义文件名
var result = ConvertAudio(exePath, m4aPath, mp3Path); //转换
}
///
/// 音频运行格式转换( .m4a => .mp3 )
///
/// ffmpeg.exe本地文件的存放路径
/// m4a源文件物理路径
/// mp3目标文件虚拟路径
///
public static string ConvertAudio(string ffmpegVirtualPath, string sourceFile, string fileVirtualPath)
{
//取得ffmpeg.exe的物理路径
string ffmpeg = ffmpegVirtualPath;
if (!File.Exists(ffmpeg))
{
return "找不到格式转换程序!";
}
if (!File.Exists(sourceFile))
{
return "找不到源文件!";
}
string destFile = fileVirtualPath;
System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//FilestartInfo.Arguments = " -i " + sourceFile + " -vn -ar 8 -ac 2 -ab 192 -f mp3 " + destFile;
FilestartInfo.Arguments = " -i " + sourceFile + " -f mp3 " + destFile;
try
{
//转换
System.Diagnostics.Process process = System.Diagnostics.Process.Start(FilestartInfo);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
StreamWriter q1 = process.StandardInput;
StreamReader q2 = process.StandardOutput;
}
catch (Exception ex)
{
return "格式转换失败!";
}
return "格式转换成功!";
}



