2013-05-22 32 views
10

Tôi có một yêu cầu lạ. Người dùng có thể tải lên video của họ ở bất kỳ định dạng nào (hoặc định dạng giới hạn). Chúng tôi phải lưu trữ chúng và chuyển đổi chúng sang định dạng .mp4 để chúng tôi có thể phát trong trang web của chúng tôi.Tải lên bất kỳ video nào và chuyển đổi thành .mp4 trực tuyến tại .net

Yêu cầu tương tự đối với tệp âm thanh.

Tôi đã googled nhưng tôi không thể có được bất kỳ ý tưởng thích hợp. Bất kỳ trợ giúp hoặc gợi ý .... ??

Cảm ơn trước

+0

tôi không biết nếu điều này sẽ giúp đỡ, nhưng có một chuyển đổi trực tuyến - có thể xem mã nguồn? http://video.online-convert.com/convert-to-mp4 –

+0

ngôn ngữ .net nào bạn sử dụng asp/c sharp? –

+0

bạn có thể muốn xem https://code.google.com/p/ffmpeg-sharp/ –

Trả lời

9

Bạn có thể chuyển đổi các file hầu như bất kỳ video/user audio sang mp4/mp3 với FFMpeg command line utility. Từ NET nó có thể được gọi là sử dụng thư viện wrapper như Video Converter for .NET (cái này là tốt đẹp vì tất cả mọi thứ được đóng gói vào một DLL):

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

Lưu ý rằng chuyển đổi video đòi hỏi tài nguyên CPU đáng kể; bạn nên chạy nó ở chế độ nền.

+0

Điều này ném ngoại lệ "Ngoại lệ loại 'System.ComponentModel.Win32Exception' xảy ra trong NReco.VideoConverter.dll nhưng không được xử lý trong mã người dùng Thông tin bổ sung: Tệp thực thi được chỉ định không một ứng dụng hợp lệ cho nền tảng hệ điều hành này. " – haroonxml

8

Your Answer

xin Thay .flv để .mp4 bạn sẽ nhận được câu trả lời của bạn

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 chuyển đổi thứ hai cho – meda

Các vấn đề liên quan