简体   繁体   中英

Convert MP4 to Ogg with C#

Does anyone know a simple way of converting a mp4 file to an ogg file?

Have to do it, but don't have much knowlegde about it, and all I can find are programs, not examples or libraries.

Thanks in advance

I would recommend dispatching this to FFMPEG - http://www.ffmpeg.org/ - using a Process and command line arguments. You can redirect I/O if you need to (eg logging). Just do something like process.WaitForExit() after you've started it. You could do this on a background thread ( BackgroundWorker , ThreadPool , etc...) if you need to not block the UI.

Extending Chad's answer I used the NReco.VideoConverter which is a helpful wrapper for FFMPEG . My code to convert a MP4 to OGG is as follows.

1.Save the file to a temporary file in local storage.

var path = Path.GetTempPath() + name;
using (var file = File.Create(path))
{
 stream.Seek(0, SeekOrigin.Begin);
 await stream.CopyToAsync(file);
 file.Close();
 return path;
}

2.Now use the video converter to convert the file, simple!

var output = new MemoryStream();
var ffMpeg = new FFMpegConverter();
ffMpeg.ConvertMedia(filePath, output, Format.ogg);
output.Seek(0, SeekOrigin.Begin);
return output;
static void Mp4ToOgg(string fileName)
{
    DsReader dr = new DsReader(fileName);
    if (dr.HasAudio)
    {
        string waveFile = fileName + ".wav";
        WaveWriter ww = new WaveWriter(File.Create(waveFile),
            AudioCompressionManager.FormatBytes(dr.ReadFormat()));
        ww.WriteData(dr.ReadData());
        ww.Close();
        dr.Close();
        try
        {
            Sox.Convert(@"sox.exe", waveFile, waveFile + ".ogg", SoxAudioFileType.Ogg);
        }
        catch (SoxException ex)
        {
            throw;
        }

    }
}

from How to converts a mp4 file to an ogg file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM