C# 如何以编程方式将 M4A 文件转换为 MP3 或 WMA 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/144466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:28:10  来源:igfitidea点击:

How do I convert an M4A file to an MP3 or WMA file programmatically?

提问by AlexeyMK

Greetings,

你好,

I'm trying to find either a free .NET library or a command-line executable that lets me convert M4A files to either MP3s or WMA files. Please help :).

我试图找到一个免费的 .NET 库或一个命令行可执行文件,让我可以将 M4A 文件转换为 MP3 或 WMA 文件。请帮忙 :)。

采纳答案by AlexeyMK

Found it!

找到了!

http://pieter.wigleven.com/it/archives/3

http://pieter.wigleven.com/it/archives/3

There may be other solutions, but this is gold for what I was looking for.

可能还有其他解决方案,但这对于我正在寻找的东西来说是黄金。

P.S. I've written a .NET DLLwhich handles this behind-the-scenes. It's pretty terrible code, but it gets the job done.

PS 我写了一个 .NET DLL来处理这个幕后问题。这是非常糟糕的代码,但它完成了工作。

回答by PhiLho

Interesting.
The link you give points to a command line utility.
If you really want to do that programmatically, you might be interested by the DLL version I found at Rarewares. Not sure if API description comes with it... :-)

有趣的。
您提供的链接指向命令行实用程序。
如果你真的想以编程方式做到这一点,你可能会对我在Rarewares找到的 DLL 版本感兴趣。不确定 API 描述是否随附... :-)

回答by cat

This is simple if you know the right tools:

如果您知道正确的工具,这很简单:

ffmpeg -i infile.m4a tmp.wav
lame tmp.wav outfile.mp3

Here a batch version (sorry Linux/Mac only):

这是一个批处理版本(抱歉,仅限 Linux/Mac):

#!/bin/bash

n=0
maxjobs=3

for i in *.m4a ; do
    ffmpeg -i "$i" "$TMP/${i%m4a}wav"
    (lame "$TMP/${i%m4a}wav" "${i%m4a}mp3" ; rm "$TMP/${i%m4a}wav") &

    # limit jobs
    if (( $(($((++n)) % $maxjobs)) == 0 )) ; then
        wait
    fi

done

回答by user2217261

from How to convert media file to WMA file

如何将媒体文件转换为 WMA 文件

string fileName = @"e:\Down\test.wmv";
DsConvert.ToWma(fileName, fileName + ".wma", DsConvert.WmaProfile.Stereo128);

回答by Amz

For UWP

对于 UWP

public class ConvertToMp3Manager
{
    public PrepareTranscodeResult PrepareTranscode = null;
    public MediaTranscoder TransCoder = null;
    public StorageFile SourceAudio { get; set; }
    public StorageFile DestinationAudio { get; set; }
    public AudioFormat AudioFormat { get; set; }
    public AudioEncodingQuality AudioQuality { get; set; }
    private MediaEncodingProfile profile = null;
    public  ConvertToMp3Manager(StorageFile sourceAudio, StorageFile destinationAudio, AudioFormat AudioType = AudioFormat.MP3, AudioEncodingQuality audioEncodingQuality = AudioEncodingQuality.High)
    {
        if (sourceAudio == null || destinationAudio == null)
            throw new ArgumentNullException("sourceAudio and destinationAudio cannot be null");
        switch (AudioType)
        {
            case AudioFormat.AAC:
            case AudioFormat.M4A:
                profile = MediaEncodingProfile.CreateM4a(audioEncodingQuality);
                break;
            case AudioFormat.MP3:
                profile = MediaEncodingProfile.CreateMp3(audioEncodingQuality);
                break;
            case AudioFormat.WMA:
                profile = MediaEncodingProfile.CreateWma(audioEncodingQuality);
                break;
        }
        this.SourceAudio = sourceAudio;
        this.DestinationAudio = destinationAudio;
        this.AudioFormat = AudioType;
        this.AudioQuality = audioEncodingQuality;
        this.TransCoder = new MediaTranscoder();
    }
    /// <summary>
    /// Return true if audio can be transcoded
    /// </summary>
    /// <returns></returns>
    public async Task<bool> ConvertAudioAsync()
    {
        PrepareTranscode = await this.TransCoder.PrepareFileTranscodeAsync(this.SourceAudio, this.DestinationAudio, profile);
        if (PrepareTranscode.CanTranscode)
        {
            var transcodeOp = PrepareTranscode.TranscodeAsync();
            return true;
        }
        else
            return false;
    }
    public static async Task<bool> ConvertAudioAsync(StorageFile sourceAudio, StorageFile destinationAudio, AudioFormat AudioType = AudioFormat.MP3, AudioEncodingQuality audioEncodingQuality = AudioEncodingQuality.High)
    {
        ConvertToMp3Manager convertToMp3Manager = new ConvertToMp3Manager(sourceAudio, destinationAudio, AudioType, audioEncodingQuality);
        var success = await convertToMp3Manager.ConvertAudioAsync();
        return success;
    }
}
public enum AudioFormat
{
    MP3,
    AAC,
    M4A,
    WMA
}