public async Task <(bool, string)> AsyncGenrateGif(Movie movie) { bool result = true; string message = ""; string GifPath = BasePicPath + "Gif\\" + movie.id + ".gif"; if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { result = false; message = Jvedio.Language.Resources.Message_SetFFmpeg; return(result, message); } if (!Directory.Exists(BasePicPath + "Gif\\")) { Directory.CreateDirectory(BasePicPath + "Gif\\"); } string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Length == 0) { result = false; message = Jvedio.Language.Resources.FailToCutOffVideo; return(result, message); } string cutofftime = cutoffArray[new Random().Next(cutoffArray.Length - 1)]; object[] list = new object[] { cutofftime, movie.filepath, GifPath }; await BeginGenGif(list); if (!File.Exists(GifPath)) { result = false; message = $"{Jvedio.Language.Resources.FailToGenerate} {GifPath}"; } return(result, message); }
public bool ScreenShot(string id) { if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { return(false); } Movie movie = DataBase.SelectMovieByID(id); int SemaphoreNum = vieModel.ScreenShot_Num; string ScreenShotPath = ""; if (Properties.Settings.Default.ScreenShotToExtraPicPath) { ScreenShotPath = BasePicPath + "ExtraPic\\" + movie.id; } else { ScreenShotPath = BasePicPath + "ScreenShot\\" + movie.id; } if (!Directory.Exists(ScreenShotPath)) { Directory.CreateDirectory(ScreenShotPath); } string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Length == 0) { return(false); } SemaphoreScreenShot = new Semaphore(SemaphoreNum, SemaphoreNum); vieModel.ScreenShot_TotalNum_S = cutoffArray.Count(); vieModel.ScreenShot_CurrentProgress_S = 0; for (int i = 0; i < cutoffArray.Count(); i++) { List <object> list = new List <object>() { cutoffArray[i], i.ToString(), movie.filepath, ScreenShotPath }; Thread threadObject = new Thread(BeginScreenShot); threadObject.Start(list); } //等待直到所有线程完成 while (vieModel.ScreenShot_CurrentProgress_S < vieModel.ScreenShot_TotalNum_S) { Task.Delay(500).Wait(); } return(true); }
private bool GenGif(string id) { Movie movie = DataBase.SelectMovieByID(id); string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Count() < 2) { return(false); } string cutoffTime = cutoffArray[new Random().Next(1, cutoffArray.Count() - 1)]; string filePath = movie.filepath; string GifSavePath = BasePicPath + "Gif\\"; if (!Directory.Exists(GifSavePath)) { Directory.CreateDirectory(GifSavePath); } GifSavePath += id + ".gif"; if (string.IsNullOrEmpty(cutoffTime)) { return(false); } if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { return(false); } System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p.StartInfo.CreateNoWindow = true; //不显示程序窗口 p.Start(); //启动程序 int width = vieModel.Gif_Width; int height = vieModel.Gif_Height; string str = $"\"{Properties.Settings.Default.FFMPEG_Path}\" -y -t {vieModel.Gif_Length} -ss {cutoffTime} -i \"{filePath}\" -s {width}x{height} \"{GifSavePath}\""; p.StandardInput.WriteLine(str + "&exit"); p.StandardInput.AutoFlush = true; string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序执行完退出进程 p.Close(); return(true); }
public async Task <(bool, string)> AsyncGenrateGif(Movie movie) { bool result = true; string message = ""; string GifPath = ""; if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { result = false; message = "未配置 FFmpeg.exe 路径"; return(result, message); } int num = Properties.Settings.Default.ScreenShot_ThreadNum; GifPath = BasePicPath + "Gif\\" + movie.id + ".gif"; if (!Directory.Exists(BasePicPath + "Gif\\")) { Directory.CreateDirectory(BasePicPath + "Gif\\"); } string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Length == 0) { result = false; message = "未成功分割影片截图"; return(result, message); } string cutofftime = cutoffArray[new Random().Next(cutoffArray.Length - 1)]; object[] list = new object[] { cutofftime, movie.filepath, GifPath }; await BeginGenGif(list); if (!File.Exists(GifPath)) { result = false; message = $"未成功生成 {GifPath}"; } return(result, message); }
public async Task <(bool, string)> AsyncScreenShot(Movie movie) { bool result = true; string message = ""; List <string> outputPath = new List <string>(); await Task.Run(() => { // n 个线程截图 if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { result = false; message = "未配置 FFmpeg.exe 路径"; return; } int num = Properties.Settings.Default.ScreenShot_ThreadNum; string ScreenShotPath = ""; if (Properties.Settings.Default.ScreenShotToExtraPicPath) { ScreenShotPath = BasePicPath + "ExtraPic\\" + movie.id; } else { ScreenShotPath = BasePicPath + "ScreenShot\\" + movie.id; } if (!Directory.Exists(ScreenShotPath)) { Directory.CreateDirectory(ScreenShotPath); } string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Length == 0) { result = false; message = "未成功分割影片截图"; return; } int SemaphoreNum = cutoffArray.Length > 10 ? 10 : cutoffArray.Length;//最多 10 个线程截图 SemaphoreScreenShot = new Semaphore(SemaphoreNum, SemaphoreNum); ScreenShotCurrent = 0; int ScreenShotTotal = cutoffArray.Count(); ScreenShotLockObject = new object(); for (int i = 0; i < cutoffArray.Count(); i++) { outputPath.Add($"{ScreenShotPath}\\ScreenShot-{i.ToString().PadLeft(2, '0')}.jpg"); List <object> list = new List <object>() { cutoffArray[i], i.ToString(), movie.filepath, ScreenShotPath }; Thread threadObject = new Thread(BeginScreenShot); threadObject.Start(list); } //等待直到所有线程结束 while (ScreenShotCurrent != ScreenShotTotal) { Task.Delay(100).Wait(); } //cmdTextBox.AppendText($"已启用 {cutoffArray.Count()} 个线程, 3-10S 后即可截图成功\n"); }); foreach (var item in outputPath) { if (!File.Exists(item)) { result = false; message = $"未成功生成 {item}"; break; } } return(result, message); }
public async Task <(bool, string)> AsyncScreenShot(Movie movie) { bool result = true; string message = ""; List <string> outputPath = new List <string>(); await Task.Run(() => { // n 个线程截图 if (!File.Exists(Properties.Settings.Default.FFMPEG_Path)) { result = false; message = Jvedio.Language.Resources.Message_SetFFmpeg; return; } int num = Properties.Settings.Default.ScreenShot_ThreadNum; string ScreenShotPath = ""; ScreenShotPath = BasePicPath + "ScreenShot\\" + movie.id; if (!Directory.Exists(ScreenShotPath)) { Directory.CreateDirectory(ScreenShotPath); } string[] cutoffArray = MediaParse.GetCutOffArray(movie.filepath); //获得影片长度数组 if (cutoffArray.Length == 0) { result = false; message = Jvedio.Language.Resources.FailToCutOffVideo; return; } if (!File.Exists(movie.filepath)) { result = false; message = Jvedio.Language.Resources.NotExists; return; } int SemaphoreNum = cutoffArray.Length > 10 ? 10 : cutoffArray.Length;//最多 10 个线程截图 SemaphoreScreenShot = new Semaphore(SemaphoreNum, SemaphoreNum); ScreenShotCurrent = 0; int ScreenShotTotal = cutoffArray.Count(); ScreenShotLockObject = new object(); for (int i = 0; i < cutoffArray.Count(); i++) { outputPath.Add($"{ScreenShotPath}\\ScreenShot-{i.ToString().PadLeft(2, '0')}.jpg"); List <object> list = new List <object>() { cutoffArray[i], i.ToString(), movie.filepath, ScreenShotPath }; Thread threadObject = new Thread(BeginScreenShot); threadObject.Start(list); } //等待直到所有线程结束 while (ScreenShotCurrent != ScreenShotTotal) { Task.Delay(100).Wait(); } }); foreach (var item in outputPath) { if (!File.Exists(item)) { result = false; message = $"{Jvedio.Language.Resources.FailToGenerate} {item}"; break; } } return(result, message); }