c# extract mp3 file from mp4 file

混江龙づ霸主 提交于 2020-01-17 12:24:27

问题


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.



来源:https://stackoverflow.com/questions/44981454/c-sharp-extract-mp3-file-from-mp4-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!