开发者

Convert MP4 to Ogg with C#

开发者 https://www.devze.com 2023-01-04 03:45 出处:网络
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 librarie

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 (e.g. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消