c# extract mp3 file from mp4 file

2019-09-20 11:02发布

问题:

is there any easy way of extracting a mp3 file from a mp4 file?

I've already tried to change the file extension, but that won't let me to edit the mp3 description.

thank you!

回答1:

Use Xabe.FFmpeg. It's free (non-commercial use), has public repository on GitHub, cross-platform (written in .NET Standard).

Extracting mp3 from mp4 just by 3 lines:

    string output = Path.ChangeExtension(Path.GetTempFileName(), FileExtensions.Mp3);
    IConversionResult result = await Conversion.ExtractAudio(Resources.Mp4WithAudio, output)
                                               .Start();

It requires FFmpeg executables like in other answer but you can download it by

    FFmpeg.GetLatestVersion();

Full documentation available here - Xabe.FFmpeg Documentation



回答2:

using FFMPEG you could do something like this:

using System;
using System.Diagnostics;

namespace ConvertMP4ToMP3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var inputFile = args[0] + ".mp4";
            var outputFile = args[1] + ".mp3";
            var mp3out = "";
            var ffmpegProcess = new Process();
            ffmpegProcess.StartInfo.UseShellExecute = false;
            ffmpegProcess.StartInfo.RedirectStandardInput = true;
            ffmpegProcess.StartInfo.RedirectStandardOutput = true;
            ffmpegProcess.StartInfo.RedirectStandardError = true;
            ffmpegProcess.StartInfo.CreateNoWindow = true;
            ffmpegProcess.StartInfo.FileName = "//path/to/ffmpeg";
            ffmpegProcess.StartInfo.Arguments = " -i " + inputFile + " -vn -f mp3 -ab 320k output " + outputFile;
            ffmpegProcess.Start();
            ffmpegProcess.StandardOutput.ReadToEnd();
            mp3out = ffmpegProcess.StandardError.ReadToEnd();
            ffmpegProcess.WaitForExit();
            if (!ffmpegProcess.HasExited)
            {
                ffmpegProcess.Kill();
            }
            Console.WriteLine(mp3out);
        }
    }
}

Perhaps? Here your would call the .exe from the command line with the path to the input and output files minus the extensions, a la:

C:\>ConvertMP3ToMP4.exe C:\Videos\Aqua\BarbieGirl C:\Music\Aqua\BarbiGirl

You will, of course, need to provide the full path to the FFMPEG executable on line 19. A small caveat here, I just back of the enveloped this so the code may have problems, I did not compile or test it, this is just a starting point. Good luck.