上传任何video并在.net中在线转换为.mp4

我有一个奇怪的要求。 用户可以上传任何格式(或有限格式)的video。 我们必须存储它们并将它们转换为.mp4格式,以便我们可以在我们的网站中播放它。

音频文件的要求也相同。

我用谷歌搜索,但我无法得到任何正确的想法。 任何帮助或建议…. ??

提前致谢

您可以使用FFMpeg命令行实用程序将几乎任何video/音频用户文件转换为mp4 / mp3。 从.NET可以使用像Video Converter for .NET这样的包装器库来调用它(这个很好,因为所有内容都打包到一个DLL中):

(new NReco.VideoConverter.FFMpegConverter()).ConvertMedia(pathToVideoFile, pathToOutputMp4File, Formats.mp4) 

请注意,video转换需要大量CPU资源; 在后台运行它是个好主意。

你的答案

请将.flv替换为.mp4,您将得到答案

 private bool ReturnVideo(string fileName) { string html = string.Empty; //rename if file already exists int j = 0; string AppPath; string inputPath; string outputPath; string imgpath; AppPath = Request.PhysicalApplicationPath; //Get the application path inputPath = AppPath + "OriginalVideo"; //Path of the original file outputPath = AppPath + "ConvertVideo"; //Path of the converted file imgpath = AppPath + "Thumbs"; //Path of the preview file string filepath = Server.MapPath("~/OriginalVideo/" + fileName); while (File.Exists(filepath)) { j = j + 1; int dotPos = fileName.LastIndexOf("."); string namewithoutext = fileName.Substring(0, dotPos); string ext = fileName.Substring(dotPos + 1); fileName = namewithoutext + j + "." + ext; filepath = Server.MapPath("~/OriginalVideo/" + fileName); } try { this.fileuploadImageVideo.SaveAs(filepath); } catch { return false; } string outPutFile; outPutFile = "~/OriginalVideo/" + fileName; int i = this.fileuploadImageVideo.PostedFile.ContentLength; System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile)); while (a.Exists == false) { } long b = a.Length; while (i != b) { } string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\""; ConvertNow(cmd); string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\""; ConvertNow(imgargs); return true; } private void ConvertNow(string cmd) { string exepath; string AppPath = Request.PhysicalApplicationPath; //Get the application path exepath = AppPath + "ffmpeg.exe"; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = exepath; //Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe" proc.StartInfo.Arguments = cmd; //The command which will be executed proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.RedirectStandardOutput = false; proc.Start(); while (proc.HasExited == false) { } } protected void btn_Submit_Click(object sender, EventArgs e) { ReturnVideo(this.fileuploadImageVideo.FileName.ToString()); } 

我知道这有点老问题,但如果我到这里,其他人也会看到这个。 你不应该使用它来处理信息来启动ffmpeg。 这与它有很大关系。 Xabe.FFmpeg你可以通过运行来做到这一点

 await Conversion.Convert("inputfile.mkv", "file.mp4").Start() 

这是最简单的用法之一。 该库为FFmpeg提供了流畅的API。