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 async Task <bool> BeginGenGif(object o) { return(await Task.Run(() => { object[] list = o as object[]; string cutoffTime = list[0] as string; string filePath = list[1] as string; string GifPath = list[2] as string; int duration = Properties.Settings.Default.Gif_Duration; int width = Properties.Settings.Default.Gif_Width; int height = Properties.Settings.Default.Gif_Height; if (Properties.Settings.Default.Gif_AutoHeight) { (double w, double h) = MediaParse.GetWidthHeight(filePath); if (w != 0) { height = (int)(h / w * (double)width); } } if (width <= 0 || width > 1980) { width = 280; } if (height <= 0 || height > 1080) { height = 170; } if (string.IsNullOrEmpty(cutoffTime)) { 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(); //启动程序 string str = $"\"{Properties.Settings.Default.FFMPEG_Path}\" -y -t {duration} -ss {cutoffTime} -i \"{filePath}\" -s {width}x{height} \"{GifPath}\""; Console.WriteLine(str); p.StandardInput.WriteLine(str + "&exit"); p.StandardInput.AutoFlush = true; string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();//等待程序执行完退出进程 p.Close(); SingleScreenShotCompleted?.Invoke(this, new ScreenShotEventArgs(str, GifPath)); return true; })); }
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); }
private void Button_Click(object sender, RoutedEventArgs e) { Stopwatch.Start(); //测试 MediaParse string path1 = @"F:\No\FC2\FC2PPV-1458145.mp4"; string path2 = @"F:\No\步兵系列\Tokyo\n1078_juri_motomiya_hh_n_fhd.wmv"; string path3 = @"G:\No\FC2\fc2-ppv-684994.mp4"; //Console.WriteLine(MediaParse.GetVedioDuration(path1)); //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); //Stopwatch.Restart(); //Console.WriteLine(MediaParse.GetVedioDuration(path2)); //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); //Stopwatch.Restart(); //Console.WriteLine(MediaParse.GetVedioDuration(path3)); //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); //foreach (var item in MediaParse.GetCutOffArray(path1)) { Console.WriteLine(item); } //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); //Stopwatch.Restart(); //foreach (var item in MediaParse.GetCutOffArray(path2)) { Console.WriteLine(item); } //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); //Stopwatch.Restart(); //foreach (var item in MediaParse.GetCutOffArray(path3)) { Console.WriteLine(item); } //Stopwatch.Stop(); //Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); Console.WriteLine(MediaParse.GetMediaInfo(path1).Format); Stopwatch.Stop(); Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); Stopwatch.Restart(); Console.WriteLine(MediaParse.GetMediaInfo(path2).Format); Stopwatch.Stop(); Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); Stopwatch.Restart(); Console.WriteLine(MediaParse.GetMediaInfo(path3).Format); Stopwatch.Stop(); Console.WriteLine("运行时间:" + Stopwatch.ElapsedMilliseconds); }
public async Task <bool> BeginGenGif(object o) { return(await Task.Run(async() => { object[] list = o as object[]; string cutoffTime = list[0] as string; string filePath = list[1] as string; string GifPath = list[2] as string; int duration = Properties.Settings.Default.Gif_Duration; int width = Properties.Settings.Default.Gif_Width; int height = Properties.Settings.Default.Gif_Height; if (Properties.Settings.Default.Gif_AutoHeight) { (double w, double h) = MediaParse.GetWidthHeight(filePath); if (w != 0) { height = (int)(h / w * (double)width); } } if (width <= 0 || width > 1980) { width = 280; } if (height <= 0 || height > 1080) { height = 170; } if (string.IsNullOrEmpty(cutoffTime)) { return false; } string str = $"\"{Properties.Settings.Default.FFMPEG_Path}\" -y -t {duration} -ss {cutoffTime} -i \"{filePath}\" -s {width}x{height} \"{GifPath}\""; string error = await new FFmpegHelper(str, duration * 5).Run(); SingleScreenShotCompleted?.Invoke(this, new ScreenShotEventArgs(str, GifPath, error)); 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); }