asp.net core在linux進行上傳視頻ffmpeg截圖


項目要求,視頻要提供一張截圖。在linux使用ffmpeg,沒有window方便可以直接用exe文件;因為我們使用的操作系統是centos7.0+ ,必須先安裝; 

 

1、在centos上安裝FFMPEG; 

所以先找了ffmpeg的安裝。一開始,是網上一堆教程,全手動安裝,安裝過程遇到一堆問題。然后一個問題解決,又遇到另一個,然后了三四個問題后。最后果然放棄安裝。從網上搜索到了yum install一鍵自動安裝的。嘗試了,可用。紀錄 下來

第一步: 按照自己centOS版本安裝 nux-dextop repository
一下是指令!
  On CentOS/RHEL 6.*:
   sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
  On CentOS/RHEL 7:
  $ sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
  Now verify that Nux Dextop repository is successfully installed:
  $ yum repolist
第二步:yum install ffmpeg


安裝后,查看版本,來進行是否安裝成功判斷;(用whereis ffmpeg可以查看其安裝路徑;)


/usr/bin/ffmpeg -version

 

 

2、在asp.net core進行視頻截圖;我上的代碼,也是網上抄過來的。代碼寫后,在window運行正常;然后發布到linux環境上,一直這個報錯,遇到:No such file or directory ;

一開始以為是命令寫錯,在linux命令行執行轉換命令,都能正常執行,然后又認為是linux權限問題;各種嘗試;后面果斷認為是asp.net core出問題。或是代碼有問題;

 public string convertVideoImage(string VideoPath = "")
        {
            string str_MyProg = AppSettings.GetValue("FfmpegPath");//工具路徑。從上面看,就是/usr/bin/ffmpeg
            if (string.IsNullOrEmpty(VideoPath))
            {
                return string.Empty;
            }
            string str_CommandArgs = "";
            var file1 = new FileInfo(VideoPath);
            if (file1.Exists)
            {
                try
                {
                    string save_folder = file1.FullName.Replace(file1.Name, "");
                    string image_file = "video_" + file1.Name.Replace(file1.Extension, ".jpg");
                    //#設置參數以直接輸出圖像序列(幀),第四秒
                    str_CommandArgs = "-i " + VideoPath + " -ss 00:00:04 -vframes 1 -an -y  -f mjpeg " + save_folder + image_file;
                    System.Diagnostics.ProcessStartInfo cmd_StartInfo = new System.Diagnostics.ProcessStartInfo(str_MyProg, str_CommandArgs);
                    cmd_StartInfo.RedirectStandardError = false; //set false
                    cmd_StartInfo.RedirectStandardOutput = false; //set false
                    cmd_StartInfo.UseShellExecute = true; //set true
                    cmd_StartInfo.CreateNoWindow = true;  //don't need the black window
                                                          //創建一個進程,分配它的ProcessStartInfo並啟動它
                    System.Diagnostics.Process cmd = new System.Diagnostics.Process();
                    cmd.StartInfo = cmd_StartInfo;
                    cmd.Start();
                    System.Threading.Thread.Sleep(1000);


                    return image_file;
                }
                catch (Exception ee)
                {
                    throw new Exception(ee.StackTrace + ee.Message + " for: " + str_MyProg + " " + str_CommandArgs);
                }
            }
            else
            {
                throw new Exception("No exists file:" + VideoPath);
              
            }
        }

 

上面的代碼,在window執行都正確,在linux下,就會報找不到路徑(No such file or directory);

在各個網站上查找,最終問題,是出現在 ,

UseShellExecute 一定要設置為false; 
 cmd_StartInfo.UseShellExecute = false; //set true 

特紀錄下來,發現在asp.net core,用FFMPEG文章的還是比較少;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM