Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mp3

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 :).

like image 822
AlexeyMK Avatar asked Sep 27 '08 21:09

AlexeyMK


People also ask

How do I convert M4A to MP3 using Windows Media Player?

How Do I convert M4A to MP3 using Windows media player? Open your M4A file with Windows Media Player. Click on the Organize button at the top left, select Options. Select the Rip Music tab and click the Change button to specify the location you want to rip music to and choose MP3 as the format.


3 Answers

Found it!

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 DLL which handles this behind-the-scenes. It's pretty terrible code, but it gets the job done.

like image 172
AlexeyMK Avatar answered Oct 18 '22 13:10

AlexeyMK


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):

#!/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
like image 37
cat Avatar answered Oct 18 '22 14:10

cat


For 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
}
like image 34
Amz Avatar answered Oct 18 '22 14:10

Amz