2013-05-22 65 views
10

我有一個奇怪的要求。用戶可以上傳任何格式的視頻(或有限格式)。我們必須將它們存儲並將它們轉換爲.mp4格式,以便我們可以在我們的網站上播放它。上傳任何視頻並在線轉換爲.mp4 .net

對於音頻文件也是同樣的要求。

我已使用Google搜索,但無法獲得任何正確的想法。任何幫助或建議....?

在此先感謝

+0

我不知道這是否會幫助,但有一個在線轉換器 - 也許查看源? http://video.online-convert.com/convert-to-mp4 –

+0

什麼.net語言你使用asp/c sharp? –

+0

你可能想看看https://code.google.com/p/ffmpeg-sharp/ –

回答

9

您幾乎所有的視頻/音頻的用戶文件轉換成MP4/MP3與FFMpeg command line utility。從.NET它可以使用像Video Converter for .NET(因爲一切都打包成一個DLL這個人是不錯的)包裝庫被稱爲:

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

注意,視頻轉換需要顯著的CPU資源;在後臺運行它是個好主意。

+0

這引發異常「在NReco.VideoConverter.dll中發生類型'System.ComponentModel.Win32Exception'的異常,但未在用戶代碼中處理 其他信息:指定的可執行文件不是這個OS平臺的有效應用程序。「 – haroonxml

8

Your Answer

請更換。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()); 
    } 
+0

@Tejas什麼是第二次轉換 – meda