FFmpeg C#应用(二):音频格式转换

C#程序中,使用ffmpeg.exe可对音频文件进行格式转换和转码。下面的代码可将amr格式的音频文件转为ogg格式的音频文件,inputPath 为输入文件路径,outputPath为输出文件路径。

-acodec libvorbis”指定转码使用的库,“-ar 44100 -ac 2”指定转码后的音频文件参数,其中“-ac 2”指定双声道。“-acodec libvorbis -ar 44100 -ac 2”这段指令在某些格式之间转换时可省略,例如ogg转为mp3时,直接使用“ExecuteCommand(pc,"\""+ ffmpegPath + "\"" + " -i " + "\""+ inputPath + "\"" + " " + "\""+ outputPath + "\"", out output, outerror);”即可。ExecuteCommand函数请参考之前的文章《FFmpeg C#应用(一):执行指令》。

 

public bool VoiceTransfer(string inputPath,stringoutputPath)

        {

            if(System.IO.File.Exists(inputPath))

            {

                if(System.IO.File.Exists(outputPath))

                {

                    File.Delete(outputPath);

                }

 

                stringffmpegPath =new FileInfo(Process.GetCurrentProcess().MainModule.FileName).DirectoryName+@"\ffmpeg.exe";

                stringoutput = string.Empty;

                stringerror = string.Empty;

                Processpc = new Process();

                ExecuteCommand(pc, "\"" + ffmpegPath +"\"" + "-i " +"\"" +inputPath + "\"" + " -acodec libvorbis -ar 44100 -ac 2 " +"\"" + outputPath + "\"", outoutput, out error);

   Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})",RegexOptions.Compiled);

                if(!string.IsNullOrEmpty(error))

                {

                    Matchm = regex.Match(error);

                    if(m != null)

                    {

                        if (!m.Success)

                        {

                            File.Delete(inputPath);

 

                            returntrue;

                        }

                    }

                }

            }

 

            returnfalse;

        }

资料

FFmpeg官网: http://www.ffmpeg.org

FFmpeg doc : http://www.ffmpeg.org/documentation.html

FFmpeg wiki : https://trac.ffmpeg.org/wiki


猜你喜欢

转载自blog.csdn.net/wzh0316/article/details/79870144