using Common.system; using System; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Input; using System.Windows.Threading; namespace XHWK.WKTool { /// /// 视频剪辑 /// VideoClipWindow.xaml 的交互逻辑 /// public partial class VideoClipWindow : Window { /// /// 视频地址 /// string VideoPath; /// /// 视频剪辑 /// public VideoClipWindow() { InitializeComponent(); Width = APP.W_XHMicroLessonSystemWindow.ActualWidth; Height = APP.W_XHMicroLessonSystemWindow.ActualHeight; } public bool InitClipVideo(string _videoPath) { if (FileToolsCommon.IsExistFile(_videoPath)) { VideoPath = _videoPath; QS_Movie.Source = new Uri(VideoPath); double Prop = (double)QS_Movie.NaturalVideoHeight / (double)QS_Movie.NaturalVideoWidth; double gridWidth = GridMovie.ActualHeight / Prop; if (gridWidth <= GridMovie.ActualWidth) { QS_Movie.Width = gridWidth; QS_Movie.Height = GridMovie.ActualHeight; } else { QS_Movie.Height = GridMovie.ActualWidth * Prop; QS_Movie.Width = GridMovie.ActualWidth; } DataContext = APP.pageData; } else { return false; } return true; } /// /// 计时器 /// DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 定义一个DT /// /// 播放/暂停 /// /// /// private void Play_Click(object sender, RoutedEventArgs e) { if (IsPlay) { MoviePause(); } else { MoviePlay(); } } /// /// 正在播放 /// bool IsPlay = false; /// /// 播放 /// public void MoviePlay() { IsPlay = true; ImgPlay.Visibility = Visibility.Collapsed; ImgPause.Visibility = Visibility.Visible; QS_Movie.Play(); } /// /// 暂停 /// public void MoviePause() { IsPlay = false; ImgPlay.Visibility = Visibility.Visible; ImgPause.Visibility = Visibility.Collapsed; QS_Movie.Pause(); } /// /// 暂停 /// /// /// private void Pause_Click(object sender, RoutedEventArgs e) { MoviePause(); } /// /// slider滑动值随播放内容位置变化 /// /// /// private void dispatcherTimer_Tick(object sender, EventArgs e) { timelineSlider.Value = QS_Movie.Position.TotalMilliseconds; //slider滑动值随播放内容位置变化 LblProgress.Content = new TimeSpan(0, 0, 0, 0, (int)QS_Movie.Position.TotalMilliseconds).ToString(@"hh\:mm\:ss"); } /// /// 拖动进度条 /// /// /// private void timelineMDown(object sender, EventArgs e) { dispatcherTimer.Stop(); } /// /// 拖动进度条 /// /// /// private void timelineMUp(object sender, EventArgs e) { QS_Movie.Position = new TimeSpan(0, 0, 0, 0, (int)timelineSlider.Value); dispatcherTimer.Start(); MoviePlay(); } /// /// 加载视频 /// /// /// private void QS_Movie_Loaded(object sender, RoutedEventArgs e) { //GridMovie.Height UcSlider.SilderWidth = (int)timelineSlider.ActualWidth; MoviePlay(); while (QS_Movie.NaturalVideoWidth == 0) { System.Threading.Thread.Sleep(50); } MoviePause(); //new Thread(new ThreadStart(new Action(() => //{ // Dispatcher.Invoke(() => // { //}); //}))).Start(); } /// /// 关闭 /// /// /// private void Button_Click(object sender, RoutedEventArgs e) { //this.Close(); QS_Movie.Stop(); IsPlay = false; ImgPlay.Visibility = Visibility.Visible; ImgPause.Visibility = Visibility.Collapsed; UcSlider.StartValue = 0; timelineSlider.Value = 0; dispatcherTimer.Stop(); this.Hide(); } double statime = 0; private void UcSlider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //dispatcherTimer.Stop(); statime = UcSlider.StartValue; } private void UcSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Math.Abs(UcSlider.StartValue - statime) > 100) { dispatcherTimer.Stop(); timelineSlider.Value = UcSlider.StartValue; QS_Movie.Position = new TimeSpan(0, 0, 0, 0, (int)timelineSlider.Value); dispatcherTimer.Start(); UcSlider_PreviewMouseMove(null, null); MoviePlay(); } } private void UcSlider_PreviewMouseMove(object sender, MouseEventArgs e) { TxbStartTime.Text = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue).ToString(@"hh\:mm\:ss\.fff"); TxbEndTime.Text = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue).ToString(@"hh\:mm\:ss\.fff"); LblVideoTime.Content = new TimeSpan(0, 0, 0, 0, ((int)UcSlider.EndValue - (int)UcSlider.StartValue)).ToString(@"hh\:mm\:ss\.fff"); //LblStartTime.Content = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue).ToString(@"hh\:mm\:ss"); //LblEndTime.Content = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue).ToString(@"hh\:mm\:ss"); //LblVideoTime.Content = new TimeSpan(0, 0, 0, 0, ((int)(UcSlider.EndValue - (UcSlider.EndValue % 1000)) - (int)(UcSlider.StartValue - (UcSlider.StartValue % 1000)))).ToString(@"hh\:mm\:ss"); } /// /// 剪辑 /// /// /// private void BtnClip_Click(object sender, RoutedEventArgs e) { if ((UcSlider.EndValue - UcSlider.StartValue) < 1000) { MessageWindow.Show("剪辑时间不得小于1秒!"); return; } dispatcherTimer.Stop(); MoviePause(); string Extension = FileToolsCommon.GetIOExtension(VideoPath); string FileName = FileToolsCommon.GetIOFileNameNoExtension(VideoPath); System.Windows.Forms.SaveFileDialog saveDg = new System.Windows.Forms.SaveFileDialog(); saveDg.Filter = "(*" + Extension + ")|*" + Extension;// + "|(*.MP4)|*.MP4"; //saveDg.FileName = tableName + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ""; saveDg.FileName = FileName + "剪辑"; saveDg.AddExtension = true; saveDg.RestoreDirectory = true; if (saveDg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filePath = saveDg.FileName; //duration.ToString(@"dd\.hh\:mm\:ss"); TimeSpan startTP = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue); string startTime = startTP.ToString(@"hh\:mm\:ss\.fff"); TimeSpan EndTP = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue); string EndTime = EndTP.ToString(@"hh\:mm\:ss\.fff"); APP.FFmpeg.ClipVideo(VideoPath, startTime, EndTime, filePath); MessageBoxResult br = MessageWindow.Show("剪辑完成是否打开文件夹?", "剪辑", MessageBoxButton.OKCancel); if (br == MessageBoxResult.OK) { //System.Diagnostics.Process.Start(FileToolsCommon.GetDirectoryName(filePath)); System.Diagnostics.Process.Start("explorer.exe", "/select," + filePath); } } } /// /// 输入开始时间 /// /// /// private void TxbStartTime_LostFocus(object sender, RoutedEventArgs e) { //判断是否为时间格式 if (!TimeStrRegularJudg(TxbStartTime.Text)) { MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff"); return; } TimeSpan time = ConvertToTimeSpan(TxbStartTime.Text); if (time.TotalMilliseconds > UcSlider.EndValue) { MessageWindow.Show("开始时间不能大于结束时间!"); return; } if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds - 1000 || time.TotalMilliseconds > UcSlider.EndValue - 1000) { MessageWindow.Show("剪辑时间不能小于1秒!"); return; } UcSlider.StartValue = time.TotalMilliseconds; UcSlider_PreviewMouseLeftButtonUp(null, null); } /// /// 输入结束时间 /// /// /// private void TxbEndTime_LostFocus(object sender, RoutedEventArgs e) { //判断是否为时间格式 if (!TimeStrRegularJudg(TxbEndTime.Text)) { MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff"); return; } TimeSpan time = ConvertToTimeSpan(TxbEndTime.Text); if (time.TotalMilliseconds < UcSlider.StartValue) { MessageWindow.Show("结束时间不能小于开始时间!"); return; } if (time.TotalMilliseconds < UcSlider.StartValue + 1000) { MessageWindow.Show("剪辑时间不能小于1秒!"); return; } if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds) { MessageWindow.Show("结束时间不能超出视频总时长!"); return; } UcSlider.EndValue = time.TotalMilliseconds; UcSlider_PreviewMouseLeftButtonUp(null, null); } /// /// string转TimeSpan /// /// /// public TimeSpan ConvertToTimeSpan(string str) { int day = 0; int hour = 0; int min = 0; int sec = 0; int mil = 0; string[] strArray1 = str.Split(':'); try { if (strArray1[0].Contains(".")) { string[] strArray2 = strArray1[0].Split('.'); day = Convert.ToInt32(strArray2[0]); hour = Convert.ToInt32(strArray2[1]); } else { day = 0; hour = Convert.ToInt32(strArray1[0]); } min = Convert.ToInt32(strArray1[1]); if (strArray1[2].Contains(".")) { string[] strArray3 = strArray1[2].Split('.'); sec = Convert.ToInt32(strArray3[0]); mil = Convert.ToInt32(strArray3[1]); } else { sec = Convert.ToInt32(strArray1[2]); } } catch (Exception) { } return new TimeSpan(day, hour, min, sec, mil); } /// /// 判断是否为时间格式 /// /// /// public bool TimeStrRegularJudg(string str) { try { Regex reg = new Regex(@"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d.\d\d\d)$"); return reg.IsMatch(str); } catch (Exception) { return false; } } /// /// 鼠标点击移动 /// /// /// private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DragMove(); } /// /// 启动定时器 同步进度条和视频进度 /// /// /// private void Element_MediaOpened(object sender, RoutedEventArgs e) { timelineSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds; //设置slider最大值 UcSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds; UcSlider.StartValue = 0; UcSlider.EndValue = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds; UcSlider_PreviewMouseMove(null, null); int sec = (int)QS_Movie.NaturalDuration.TimeSpan.TotalSeconds; dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //超过计时间隔时发生 dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //DT间隔 dispatcherTimer.Start(); //DT启动 #region 调整视频大小 double Prop = (double)QS_Movie.NaturalVideoHeight / (double)QS_Movie.NaturalVideoWidth; double gridWidth = GridMovie.ActualHeight / Prop; if (gridWidth <= GridMovie.ActualWidth) { QS_Movie.Width = gridWidth; QS_Movie.Height = GridMovie.ActualHeight; } else { QS_Movie.Height = GridMovie.ActualWidth * Prop; QS_Movie.Width = GridMovie.ActualWidth; } #endregion } } }