1,我們需要借助一個工具ffmpeg(http://www.ffmpeg.org/download.html)
2,截取第一幀的方法
/// <summary> /// 從視頻中截取img格式圖片 /// </summary> /// <param name="applicationPath">ffmpeg.exe文件路徑</param> /// <param name="fileNamePath">視頻文件路徑(帶文件名)</param> /// <param name="targetImgNamePath">生成img圖片路徑(帶文件名)</param> private void ConverToImg(string applicationPath, string fileNamePath, string targetImgNamePath) { //string c = applicationPath + @"\ffmpeg.exe -i" + fileNamePath + targetImgNamePath+"-ss 00:00:05 -r 1 -vframes 1 -an -vcodec mjpeg " ; string c = applicationPath + @"\ffmpeg.exe -ss 00:00:05 -i" + " " + fileNamePath + " " + targetImgNamePath + " " + "-r 1 -vframes 1 -an -vcodec mjpeg ";//速度快 Cmd(c); //-i:設定輸入文件名 //-r:設定幀 此處設為1幀 //-f:設定輸出格式 //-ss 從指定時間截圖 //-vcodec:設定影像解碼器,沒有輸入時為文件原來相同的解碼器 //-vframes 設置轉換多少楨(frame)的視頻 //-an 不處理聲音 }
/// <summary> /// 程序中調用CMD.exe程序,並且不顯示命令行窗口界面 /// </summary> /// <param name="c">執行的cmd命令</param> private void Cmd(string c) { try { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.CreateNoWindow = true; //不顯示程序窗口 process.StartInfo.FileName = "cmd.exe";//要執行的程序名稱 process.StartInfo.UseShellExecute = false; //process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true;//由調用程序獲取輸出信息 process.StartInfo.RedirectStandardInput = true; //可能接受來自調用程序的輸入信息 process.Start();//啟動程序 process.StandardInput.WriteLine(c); process.StandardInput.AutoFlush = true; process.StandardInput.WriteLine("exit"); } catch { } }