2012-09-11 71 views
0

我想將ffmpeg生成的縮略圖傳遞給save_FTPUpload,其中save_FTPUpload函數將圖像文件上傳到ftp服務器。我怎樣才能做到這一點? 這裏使用縮略圖生成我的示例代碼的ffmpeg和「文件上傳」功能將文件上傳到FTP服務器:如何將ffmpeg輸出傳遞給C#中的其他子類?

public partial class testtttt : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     generateThumbnail(); 
     string fi_attachment = @"C:\2.jpg";//source image file 
     save_FTPUpload(fi_attachment); 
    } 

    private void generateThumbnail() 
    { 
     string thumbpath, thumbname, videofile; 

     videofile = "http://www.xxxx.com/video.Avi"; 
     thumbpath = @"C:\"; 
     thumbname = thumbpath + Path.GetFileNameWithoutExtension(videofile) + ".jpg"; 

     string thumbargs = "-i \"" + videofile + "\" -vframes 1 -s 60*30 -ss 00:00:00 -f image2 \"" + thumbname + "\""; 

     Process process = new Process(); 

     process.StartInfo.FileName = Server.MapPath("~\\ffmpeg\\bin\\ffmpeg.exe"); 
     process.StartInfo.Arguments = thumbargs; 

     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardInput = false; 
     process.StartInfo.RedirectStandardError = true; 
     process.StartInfo.CreateNoWindow = false; 
     process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 

     try 
     { 
      process.Start(); 
      string output2 = process.StandardError.ReadToEnd(); 
      string output3 = process.StandardOutput.ReadToEnd(); 

      if (process != null) 
      { 
       process.Close(); 
      } 
     } 

     catch (Exception) 
     { 
      this.lblMessage.Text = "Thumbnail created successfuly";//ex.Message.ToString(); 
     } 
    } 

    public bool save_FTPUpload(string fi_attachment) 
    { 
     bool fileSaved = false; 
     //string fi_attachment = @"C:\1.jpg"; 
     string filename = "3.jpg";//image name to be saved in ftp 
     string ftp_user = "*****"; //username 
     string ftp_pass = "*****"; //password 
     string ftpURI = "ftp://www.xxxx.com/thumb/"; //ftp path where the image will be saved 

     while (!fileSaved) 
     { 
      string file_ftpURI = string.Format("{0}/{1}", ftpURI, filename); 
      FtpWebRequest file_exist_request = (FtpWebRequest)FtpWebRequest.Create(file_ftpURI); 
      file_exist_request.Credentials = new NetworkCredential(ftp_user, ftp_pass); 
      file_exist_request.Method = WebRequestMethods.Ftp.GetFileSize; 
      try 
      { 
       FtpWebResponse response = (FtpWebResponse)file_exist_request.GetResponse(); 
      } 
      catch (WebException ex) 
      { 
       FtpWebResponse response = (FtpWebResponse)ex.Response; 
       if (response.StatusCode == 
        FtpStatusCode.ActionNotTakenFileUnavailable) 
       { 
        FtpWebRequest upload_request = (FtpWebRequest)FtpWebRequest.Create(file_ftpURI); 
        upload_request.Credentials = new NetworkCredential(ftp_user, ftp_pass); 

        upload_request.Method = WebRequestMethods.Ftp.UploadFile; 
        upload_request.UsePassive = true; 
        upload_request.UseBinary = true; 
        upload_request.KeepAlive = false; 

        using (var fs = File.OpenRead(fi_attachment)) 
        using (Stream upload_request_stream = upload_request.GetRequestStream()) 
        { 
         fs.CopyTo(upload_request_stream); 
        } 
        FtpWebResponse upload_response = (FtpWebResponse)upload_request.GetResponse(); 

        fileSaved = true; 
        this.Label1.Text = "Upload Successful"; 
       } 
      } 
     } 
     return fileSaved; 
    } 

回答

2

您確定輸出文件名是FFmpeg的將使用,那麼爲什麼不使用呢?

使用的文件名稱也可以出現在標準輸出中,但是由於您自己選擇了文件,因此您已經知道該文件。

因此,如果generateThumbnail會返回縮略名,您可以將它傳遞給FTPUpload。

+0

對於noob問題抱歉,但我怎麼能通過拇指名到FTPUpload? – Lynx

+1

一個解決方案是將'private void GenerateThumbnail()'改成'private string GenerateThumbnail()'。然後把'return thumbpath;'放在函數的最後。使用'string fi_attachment = GenerateThumbnail();'來生成縮略圖。 – Chrono

+0

感謝您的指導。我測試了你的建議,並且它工作得很好。 ffmpeg可以生成映像文件並將其保存在文件夾中,然後FTPUpload將文件上傳到FTP服務器。但是,如果我想直接從ffmpeg傳遞值(圖像文件)到FTPUpload而沒有使用ffmpeg,請先保存它? – Lynx

相關問題