2013-05-29 43 views
1

在我的項目中,我保存視頻,使用ffmpeg將格式轉換爲.swf。轉換和保存視頻工作正常,但我在創建縮略圖時遇到了問題。它保存了一個「picture.jpg」,但沒有圖像。看着它的縮略圖,它只有正常的photoviewer標誌,這很好,但是當我試圖打開圖片時,它給出了一條消息: Windows照片瀏覽器:Windows照片瀏覽器無法打開此圖片,因爲該文件似乎已損壞,損壞,或者太大。 (這張圖片是2.7MB) - 從我的相機拍攝的照片大約是5MB,然後打開。從ffmpeg保存後打開jpg圖片不起作用

PictureViewer:無法顯示「image.jpg」,因爲找不到合適的圖形導入程序。

Paint:Paint無法讀取此文件。這不是有效的位圖文件或目前不支持其格式。 圖像將無法打開任何我做保存圖像:

protected void btnUploadVideo_Click(object sender, EventArgs e) 
    { 
     string dtMonth = DateTime.Now.ToString("MMM"); 
     string dtYear = DateTime.Now.ToString("yyyy"); 
     lblMsg.Visible = false; 

     lblMsg.Visible = true; 

     // Before attempting to save the file, verify 
     // that the FileUpload control contains a file. 
     if (fuPath.HasFile) 
     { 
      // Get the size in bytes of the file to upload. 
      int fileSize = fuPath.PostedFile.ContentLength; 

      // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded. 
      if (fileSize < 10497717) 
      { 
       // Call a helper method routine to save the file. 
       SaveFile2(); 
      } 
     } 
     else 
      // Notify the user that a file was not uploaded. 
      lblMsg.Text = "You did not specify a file to upload."; 
    } 

    private void SaveFile2() 
    { 
     string dtMonth = DateTime.Now.ToString("MMM"); 
     string dtYear = DateTime.Now.ToString("yyyy"); 

     if (fuPath != null || fuPath.PostedFile != null) 
     { 
      postedfilename = fuPath.PostedFile.FileName; 
      SavePath = Server.MapPath("~\\Video\\"); 
      string NewFName = postedfilename; 
      NewFName = NewFName.Substring(NewFName.LastIndexOf("\\") + 1, NewFName.Length - NewFName.LastIndexOf(".")) + "." + NewFName.Substring(NewFName.LastIndexOf(".") + 1); 
      Filenamewithpath = SavePath + NewFName; 
      string outputPath = Server.MapPath("~\\uploads\\" + dtYear + "\\" + dtMonth + "\\SWF\\"); 
      CreateDirectoryIfNotExists(SavePath); 
      CreateDirectoryIfNotExists(outputPath); 

      //Save The file 
      fuPath.PostedFile.SaveAs(Filenamewithpath); 

      //Start Converting 
      string inputfile, outputfile, filargs; 
      string withoutext; 

      //Get the file name without Extension 
      withoutext = Path.GetFileNameWithoutExtension(Filenamewithpath); 

      //Input file path of uploaded image 
      inputfile = SavePath + NewFName; 

      //output file format in swf 

      outputfile = outputPath + withoutext + ".swf"; 
      Session["outputfile"] = withoutext + ".swf"; 
      //file orguments for FFMEPG 

      // Create the path and file name to check for duplicates. 
      string pathToCheck = outputfile; 

      // Create a temporary file name to use for checking duplicates. 
      string tempfileName = ""; 

      // Check to see if a file already exists with the 
      // same name as the file to upload.   
      if (System.IO.File.Exists(pathToCheck)) 
      { 
       int counter = 2; 
       while (System.IO.File.Exists(pathToCheck)) 
       { 
        // if a file with this name already exists, 
        // prefix the filename with a number. 
        tempfileName = outputPath + counter.ToString() + withoutext + ".swf"; 
        pathToCheck = tempfileName; 
        counter++; 
       } 

       outputfile = tempfileName; 
       // Notify the user that the file name was changed. 
       lblMsg.Text = "A file with the same name already exists." + 
        "<br />Your file was saved as " + counter.ToString() + withoutext + ".swf"; 
      } 

      filargs = "-i " + inputfile + " -ar 22050 " + outputfile; 

      string spath; 
      spath = Server.MapPath("."); 
      Process proc; 
      proc = new Process(); 
      proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe"; 
      proc.StartInfo.Arguments = filargs; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.CreateNoWindow = false; 
      proc.StartInfo.RedirectStandardOutput = false; 
      try 
      { 

       proc.Start(); 
       fuPath.PostedFile.SaveAs(outputfile); 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 
      proc.WaitForExit(); 
      proc.Close(); 

      //Create Thumbs 

      string thumbPath, thumbName; 
      string thumbargs; 

      thumbPath = Server.MapPath("~\\uploads\\" + dtYear + "\\" + dtMonth + "\\Thumb\\"); 
      CreateDirectoryIfNotExists(thumbPath); 

      thumbName = thumbPath + withoutext + ".jpg"; 

      // Create the path and file name to check for duplicates. 
      string thumbPathToCheck = thumbName; 

      // Create a temporary file name to use for checking duplicates. 
      string thumbTempfileName = ""; 

      // Check to see if a file already exists with the 
      // same name as the file to upload.   
      if (System.IO.File.Exists(thumbPathToCheck)) 
      { 
       int counter = 2; 
       while (System.IO.File.Exists(thumbPathToCheck)) 
       { 
        // if a file with this name already exists, 
        // prefix the filename with a number. 
        thumbTempfileName = thumbPath + counter.ToString() + withoutext + ".jpg"; 
        thumbPathToCheck = thumbTempfileName; 
        counter++; 
       } 

       thumbName = thumbTempfileName; 
       // Notify the user that the file name was changed. 
       lblMsg.Text = "A file with the same name already exists." + 
        "<br />Your file was saved as " + counter.ToString() + withoutext + ".jpg"; 
      } 

      //thumbargs = "-i " + inputfile + " -an -ss 00:00:03 -s 120×90 -vframes 1 -f mjpeg " + thumbName; 
      // thumbargs = "-i " + inputfile + "-f image2 -ss 1.000 -vframes 1 " + thumbName; 
      thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:10 -s 150x150 " + thumbName; 

      // thumbargs = "ffmpeg -i" + inputfile + " -ss 0:00:01.000 -sameq -vframes 1 " + withoutext + ".jpg"; 
      // thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:07 -s 150x150 " + thumbName; 
      Process thumbproc = new Process(); 
      thumbproc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe"; 
      thumbproc.StartInfo.Arguments = thumbargs; 
      thumbproc.StartInfo.UseShellExecute = false; 
      thumbproc.StartInfo.CreateNoWindow = true; 
      thumbproc.StartInfo.RedirectStandardOutput = false; 

      string data = "";     

      try 
      { 
       fuPath.PostedFile.SaveAs(thumbName); 
       thumbproc.Start(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.Message); 
      } 

      thumbproc.WaitForExit(); 
      thumbproc.Close(); 
      File.Delete(inputfile); 
      lblMsg.Text = "Video Uploaded Successfully"; 
      hyp.Visible = true; 
      hyp.NavigateUrl = "WatchVideo.aspx"; 

      SaveToDB(outputfile, thumbName); 
     } 
    } 

編輯: 如果是thumbargs,我已經嘗試了一些方法,但沒有成功:

 //thumbargs = "-i " + inputfile + " -an -ss 00:00:03 -s 120×90 -vframes 1 -f mjpeg " + thumbName; 
    // thumbargs = "-i " + inputfile + "-f image2 -ss 1.000 -vframes 1 " + thumbName; 
     thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:10 -s 150x150 " + thumbName; 
    // thumbargs = "ffmpeg -i" + inputfile + " -ss 0:00:01.000 -sameq -vframes 1 " + withoutext + ".jpg"; 

這是它的外觀保存時,但不能打開.... enter image description here

編輯:

我嘗試添加t條他對命令提示符的參數看它是否在那裏工作,但它給了我一個消息:[NULL @ 000000000025f7a0]無法找到'vframes'的合適輸出格式 vframes:無效參數 我使用的線:

ffmpeg -i VID2012.3GP vframes 1 VID2012.jpg 

如果它在IIS中沒有正確設置,我該如何設置它?編輯: 我改變了一點我的代碼。調試通過我的代碼沒有異常被拋出,但如果我點了鼠標的過程中(後啓動)它「顯示類型System.InvalidOperationException的一個例外」

thumbargs = "-i " + postedfilename + " vframes 1" + thumbName; 
      ProcessStartInfo thumbProcstartIfo = new ProcessStartInfo(); 
      thumbProcstartIfo.FileName = @"\ffmpeg\ffmpeg.exe"; 
      thumbProcstartIfo.Arguments = thumbargs; 
      thumbProcstartIfo.UseShellExecute = false; 
      thumbProcstartIfo.CreateNoWindow = true; 
      thumbProcstartIfo.RedirectStandardOutput = false; 

      try 
      { 
       using (var process = new Process()) 
       { 
        process.StartInfo = thumbProcstartIfo; 
        process.Start(); 
        process.WaitForExit(); 
       } 
      } 
      catch (InvalidOperationException ex) 
      { 
       lblMsg.Text = ex.ToString(); 
      } 
+2

是否有某些原因讓你調用'thumbproc = new Process()'兩次?不確定它是否與你的問題有關,但看起來不正確。 –

+1

嗨..這是一個錯誤,但拿出它仍然不會使圖像工作.... – Kerieks

回答

1

我認爲它必須做的這些行:

thumbproc.Start(); 
fuPath.PostedFile.SaveAs(thumbname); 

我想到的是,第二線上傳的文件保存到您的數據目錄和第一行開始處理文件。這些線不應該交換?

fuPath.PostedFile.SaveAs(thumbname); 
thumbproc.Start(); 
+0

嗨雷蒙,感謝您的答覆,但它沒有影響的形象。它仍然不起作用.... – Kerieks

+0

不是%d的問題。它可以被解釋爲通配符... –

+0

嗨雷蒙,謝謝你的回覆,不,這不是問題,我刪除了所有的字符(% - )但仍然不工作..... – Kerieks