C#利用開源軟件ffMpeg截取視頻圖片

#region 從視頻畫面中截取一幀畫面爲圖片
        /// <summary>
        /// 從視頻畫面中截取一幀畫面爲圖片
        /// </summary>
        /// <param name="VideoName">視頻文件,絕對路徑</param>
        /// <param name="WidthAndHeight">圖片的尺寸如:240*180</param>
        /// <param name="CutTimeFrame">開始截取的時間如:"1"</param>
        /// <returns></returns>
        public string GetPicFromVideo(string VideoName, string WidthAndHeight, string CutTimeFrame)
        {
            string ffmpeg = Server.MapPath("/") + "ffmpeg.exe";//ffmpeg執行文件的路徑
            string PicName = VideoName + ".jpg";//視頻圖片的名字,絕對路徑
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.Arguments = " -i " + VideoName
                                + " -y -f image2 -ss " + CutTimeFrame
                                + " -t 0.001 -s " + WidthAndHeight
                                + " " + PicName;  //設定程式執行參數
            try
            {
                System.Diagnostics.Process.Start(startInfo);
                Thread.Sleep(5000);//線程掛起,等待ffmpeg截圖完畢
            }
            catch (Exception)
            {
                return "";
            }

            //返回視頻圖片完整路徑
            if (System.IO.File.Exists(PicName))
                return PicName;
            return "";
        }
        #endregion
相關文章
相關標籤/搜索