当前位置:网站首页>C # use ffmpeg for audio transcoding

C # use ffmpeg for audio transcoding

2022-07-04 09:33:00 Dandelion_ drq

Put it first ffmpeg Official documents and download addresses :
Official documents :http://ffmpeg.org/ffmpeg.html
Download address :http://ffmpeg.org/download.html


use ffmpeg Transcoding is simple , If you use all the default parameters, just use the following sentence :

ffmpeg.exe -i D:\test\1.aac -y D:\test\1.mp3    -- 1.aac Is the input file to transcode ,1.mp3 It's the output file ,-y Overwrite output file 

Of course ffmpeg Many parameters are supported , For example, what encoder to use , Specify the code rate, etc …… I won't go into details here ( The key is that I don't understand hhh)

After knowing how to use this powerful tool , Is in the C# How to use it in ~~

It's also very simple. , use Process Start a process to call ffmpeg Just fine .

Go straight to the code , I wrote a console program , Receive two parameters , They are input file and output file ( All absolute paths ), And then call ffmpeg Transcoding , Finally complete transcoding and output corresponding operation information .

using System;
using System.Diagnostics;

namespace AudioTranscoding
{
    class Program
    {
        static void Main(string[] args)
        {
            Process process = new Process();

            try
            {
                if (args.Length != 2)
                {
                    Console.WriteLine(" Illegal parameter ");
                    return;
                }

                string inputFile = args[0];
                string outputFile = args[1];

                process.StartInfo.FileName = "ffmpeg.exe";  //  You can also specify ffmpeg The absolute path of 
                process.StartInfo.Arguments = " -i " + inputFile + " -y " + outputFile;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardError = true;
                process.ErrorDataReceived += new DataReceivedEventHandler(Output);  //  capture ffmpeg.exe Error messages for 

                DateTime beginTime = DateTime.Now;

                process.Start();
                process.BeginErrorReadLine();   //  Start asynchronous read 

                Console.WriteLine("\n Start audio transcoding ...\n");

                process.WaitForExit();    //  Wait for transcoding to complete 

                if (process.ExitCode == 0)
                {
                    int exitCode = process.ExitCode;
                    DateTime endTime = DateTime.Now;
                    TimeSpan t = endTime - beginTime;
                    double seconds = t.TotalSeconds;
                    Console.WriteLine("\n Transcoding complete ! Total time :" + seconds + " second \n");
                }
                // ffmpeg.exe  An error occurred 
                else
                {
                    Console.WriteLine("\nffmpeg.exe  There was an error in the program , Transcoding failed !");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n error !!" + ex.ToString());
            }
            finally
            {
                process.Close();
            }
        }

        private static void Output(object sendProcess, DataReceivedEventArgs output)
        {
            Process p = sendProcess as Process;
            if (p.HasExited && p.ExitCode == 1) //  stay ffmpeg Output information only when an error occurs 
            {
                Console.WriteLine(output.Data);
            }
        }
    }
}

Running results :

Transcoding succeeded :
 Picture description here

An error occurred :
 Picture description here


Reference resources :
ffmpeg Common conversion commands
.net call ffmpeg Converting audio files amr - mp3

原网站

版权声明
本文为[Dandelion_ drq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141425122414.html

随机推荐