星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoClipWindow.xaml.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using Common.system;
  2. using System;
  3. using System.Text.RegularExpressions;
  4. using System.Windows;
  5. using System.Windows.Input;
  6. using System.Windows.Threading;
  7. namespace XHWK.WKTool
  8. {
  9. /// <summary>
  10. /// 视频剪辑
  11. /// VideoClipWindow.xaml 的交互逻辑
  12. /// </summary>
  13. public partial class VideoClipWindow : Window
  14. {
  15. /// <summary>
  16. /// 视频地址
  17. /// </summary>
  18. string VideoPath;
  19. /// <summary>
  20. /// 视频剪辑
  21. /// </summary>
  22. public VideoClipWindow()
  23. {
  24. InitializeComponent();
  25. Width = App.W_XHMicroLessonSystemWindow.ActualWidth;
  26. Height = App.W_XHMicroLessonSystemWindow.ActualHeight;
  27. }
  28. public bool InitClipVideo(string _videoPath)
  29. {
  30. if (FileToolsCommon.IsExistFile(_videoPath))
  31. {
  32. VideoPath = _videoPath;
  33. QS_Movie.Source = new Uri(VideoPath);
  34. double Prop = (double)QS_Movie.NaturalVideoHeight / (double)QS_Movie.NaturalVideoWidth;
  35. double gridWidth = GridMovie.ActualHeight / Prop;
  36. if (gridWidth <= GridMovie.ActualWidth)
  37. {
  38. QS_Movie.Width = gridWidth;
  39. QS_Movie.Height = GridMovie.ActualHeight;
  40. }
  41. else
  42. {
  43. QS_Movie.Height = GridMovie.ActualWidth * Prop;
  44. QS_Movie.Width = GridMovie.ActualWidth;
  45. }
  46. DataContext = App.PageContextData;
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. return true;
  53. }
  54. /// <summary>
  55. /// 计时器
  56. /// </summary>
  57. DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 定义一个DT
  58. /// <summary>
  59. /// 播放/暂停
  60. /// </summary>
  61. /// <param name="sender"></param>
  62. /// <param name="e"></param>
  63. private void Play_Click(object sender, RoutedEventArgs e)
  64. {
  65. if (IsPlay)
  66. {
  67. MoviePause();
  68. }
  69. else
  70. {
  71. MoviePlay();
  72. }
  73. }
  74. /// <summary>
  75. /// 正在播放
  76. /// </summary>
  77. bool IsPlay = false;
  78. /// <summary>
  79. /// 播放
  80. /// </summary>
  81. public void MoviePlay()
  82. {
  83. IsPlay = true;
  84. ImgPlay.Visibility = Visibility.Collapsed;
  85. ImgPause.Visibility = Visibility.Visible;
  86. QS_Movie.Play();
  87. }
  88. /// <summary>
  89. /// 暂停
  90. /// </summary>
  91. public void MoviePause()
  92. {
  93. IsPlay = false;
  94. ImgPlay.Visibility = Visibility.Visible;
  95. ImgPause.Visibility = Visibility.Collapsed;
  96. QS_Movie.Pause();
  97. }
  98. /// <summary>
  99. /// 暂停
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. private void Pause_Click(object sender, RoutedEventArgs e)
  104. {
  105. MoviePause();
  106. }
  107. /// <summary>
  108. /// slider滑动值随播放内容位置变化
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. private void dispatcherTimer_Tick(object sender, EventArgs e)
  113. {
  114. timelineSlider.Value = QS_Movie.Position.TotalMilliseconds; //slider滑动值随播放内容位置变化
  115. LblProgress.Content = new TimeSpan(0, 0, 0, 0, (int)QS_Movie.Position.TotalMilliseconds).ToString(@"hh\:mm\:ss");
  116. }
  117. /// <summary>
  118. /// 拖动进度条
  119. /// </summary>
  120. /// <param name="sender"></param>
  121. /// <param name="e"></param>
  122. private void timelineMDown(object sender, EventArgs e)
  123. {
  124. dispatcherTimer.Stop();
  125. }
  126. /// <summary>
  127. /// 拖动进度条
  128. /// </summary>
  129. /// <param name="sender"></param>
  130. /// <param name="e"></param>
  131. private void timelineMUp(object sender, EventArgs e)
  132. {
  133. QS_Movie.Position = new TimeSpan(0, 0, 0, 0, (int)timelineSlider.Value);
  134. dispatcherTimer.Start();
  135. MoviePlay();
  136. }
  137. /// <summary>
  138. /// 加载视频
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void QS_Movie_Loaded(object sender, RoutedEventArgs e)
  143. {
  144. //GridMovie.Height
  145. UcSlider.SilderWidth = (int)timelineSlider.ActualWidth;
  146. MoviePlay();
  147. while (QS_Movie.NaturalVideoWidth == 0)
  148. {
  149. System.Threading.Thread.Sleep(50);
  150. }
  151. MoviePause();
  152. //new Thread(new ThreadStart(new Action(() =>
  153. //{
  154. // Dispatcher.Invoke(() =>
  155. // {
  156. //});
  157. //}))).Start();
  158. }
  159. /// <summary>
  160. /// 关闭
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void Button_Click(object sender, RoutedEventArgs e)
  165. {
  166. //this.Close();
  167. QS_Movie.Stop();
  168. IsPlay = false;
  169. ImgPlay.Visibility = Visibility.Visible;
  170. ImgPause.Visibility = Visibility.Collapsed;
  171. UcSlider.StartValue = 0;
  172. timelineSlider.Value = 0;
  173. dispatcherTimer.Stop();
  174. this.Hide();
  175. }
  176. double statime = 0;
  177. private void UcSlider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  178. {
  179. //dispatcherTimer.Stop();
  180. statime = UcSlider.StartValue;
  181. }
  182. private void UcSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  183. {
  184. if (Math.Abs(UcSlider.StartValue - statime) > 100)
  185. {
  186. dispatcherTimer.Stop();
  187. timelineSlider.Value = UcSlider.StartValue;
  188. QS_Movie.Position = new TimeSpan(0, 0, 0, 0, (int)timelineSlider.Value);
  189. dispatcherTimer.Start();
  190. UcSlider_PreviewMouseMove(null, null);
  191. MoviePlay();
  192. }
  193. }
  194. private void UcSlider_PreviewMouseMove(object sender, MouseEventArgs e)
  195. {
  196. TxbStartTime.Text = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue).ToString(@"hh\:mm\:ss\.fff");
  197. TxbEndTime.Text = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue).ToString(@"hh\:mm\:ss\.fff");
  198. LblVideoTime.Content = new TimeSpan(0, 0, 0, 0, ((int)UcSlider.EndValue - (int)UcSlider.StartValue)).ToString(@"hh\:mm\:ss\.fff");
  199. //LblStartTime.Content = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue).ToString(@"hh\:mm\:ss");
  200. //LblEndTime.Content = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue).ToString(@"hh\:mm\:ss");
  201. //LblVideoTime.Content = new TimeSpan(0, 0, 0, 0, ((int)(UcSlider.EndValue - (UcSlider.EndValue % 1000)) - (int)(UcSlider.StartValue - (UcSlider.StartValue % 1000)))).ToString(@"hh\:mm\:ss");
  202. }
  203. /// <summary>
  204. /// 剪辑
  205. /// </summary>
  206. /// <param name="sender"></param>
  207. /// <param name="e"></param>
  208. private void BtnClip_Click(object sender, RoutedEventArgs e)
  209. {
  210. if ((UcSlider.EndValue - UcSlider.StartValue) < 1000)
  211. {
  212. MessageWindow.Show("剪辑时间不得小于1秒!");
  213. return;
  214. }
  215. dispatcherTimer.Stop();
  216. MoviePause();
  217. string Extension = FileToolsCommon.GetIOExtension(VideoPath);
  218. string FileName = FileToolsCommon.GetIOFileNameNoExtension(VideoPath);
  219. System.Windows.Forms.SaveFileDialog saveDg = new System.Windows.Forms.SaveFileDialog();
  220. saveDg.Filter = "(*" + Extension + ")|*" + Extension;// + "|(*.MP4)|*.MP4";
  221. //saveDg.FileName = tableName + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + "";
  222. saveDg.FileName = FileName + "剪辑";
  223. saveDg.AddExtension = true;
  224. saveDg.RestoreDirectory = true;
  225. if (saveDg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  226. {
  227. string filePath = saveDg.FileName;
  228. //duration.ToString(@"dd\.hh\:mm\:ss");
  229. TimeSpan startTP = new TimeSpan(0, 0, 0, 0, (int)UcSlider.StartValue);
  230. string startTime = startTP.ToString(@"hh\:mm\:ss\.fff");
  231. TimeSpan EndTP = new TimeSpan(0, 0, 0, 0, (int)UcSlider.EndValue);
  232. string EndTime = EndTP.ToString(@"hh\:mm\:ss\.fff");
  233. App.FFmpeg.ClipVideo
  234. (
  235. VideoPath,
  236. startTime,
  237. EndTime,
  238. filePath
  239. );
  240. MessageBoxResult br = MessageWindow.Show("剪辑完成是否打开文件夹?", "剪辑", MessageBoxButton.OKCancel);
  241. if (br == MessageBoxResult.OK)
  242. {
  243. //System.Diagnostics.Process.Start(FileToolsCommon.GetDirectoryName(filePath));
  244. System.Diagnostics.Process.Start("explorer.exe", "/select," + filePath);
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// 输入开始时间
  250. /// </summary>
  251. /// <param name="sender"></param>
  252. /// <param name="e"></param>
  253. private void TxbStartTime_LostFocus(object sender, RoutedEventArgs e)
  254. {
  255. //判断是否为时间格式
  256. if (!TimeStrRegularJudg(TxbStartTime.Text))
  257. {
  258. MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff");
  259. return;
  260. }
  261. TimeSpan time = ConvertToTimeSpan(TxbStartTime.Text);
  262. if (time.TotalMilliseconds > UcSlider.EndValue)
  263. {
  264. MessageWindow.Show("开始时间不能大于结束时间!");
  265. return;
  266. }
  267. if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds - 1000 || time.TotalMilliseconds > UcSlider.EndValue - 1000)
  268. {
  269. MessageWindow.Show("剪辑时间不能小于1秒!");
  270. return;
  271. }
  272. UcSlider.StartValue = time.TotalMilliseconds;
  273. UcSlider_PreviewMouseLeftButtonUp(null, null);
  274. }
  275. /// <summary>
  276. /// 输入结束时间
  277. /// </summary>
  278. /// <param name="sender"></param>
  279. /// <param name="e"></param>
  280. private void TxbEndTime_LostFocus(object sender, RoutedEventArgs e)
  281. {
  282. //判断是否为时间格式
  283. if (!TimeStrRegularJudg(TxbEndTime.Text))
  284. {
  285. MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff");
  286. return;
  287. }
  288. TimeSpan time = ConvertToTimeSpan(TxbEndTime.Text);
  289. if (time.TotalMilliseconds < UcSlider.StartValue)
  290. {
  291. MessageWindow.Show("结束时间不能小于开始时间!");
  292. return;
  293. }
  294. if (time.TotalMilliseconds < UcSlider.StartValue + 1000)
  295. {
  296. MessageWindow.Show("剪辑时间不能小于1秒!");
  297. return;
  298. }
  299. if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds)
  300. {
  301. MessageWindow.Show("结束时间不能超出视频总时长!");
  302. return;
  303. }
  304. UcSlider.EndValue = time.TotalMilliseconds;
  305. UcSlider_PreviewMouseLeftButtonUp(null, null);
  306. }
  307. /// <summary>
  308. /// string转TimeSpan
  309. /// </summary>
  310. /// <param name="str"></param>
  311. /// <returns></returns>
  312. public TimeSpan ConvertToTimeSpan(string str)
  313. {
  314. int day = 0;
  315. int hour = 0;
  316. int min = 0;
  317. int sec = 0;
  318. int mil = 0;
  319. string[] strArray1 = str.Split(':');
  320. try
  321. {
  322. if (strArray1[0].Contains("."))
  323. {
  324. string[] strArray2 = strArray1[0].Split('.');
  325. day = Convert.ToInt32(strArray2[0]);
  326. hour = Convert.ToInt32(strArray2[1]);
  327. }
  328. else
  329. {
  330. day = 0;
  331. hour = Convert.ToInt32(strArray1[0]);
  332. }
  333. min = Convert.ToInt32(strArray1[1]);
  334. if (strArray1[2].Contains("."))
  335. {
  336. string[] strArray3 = strArray1[2].Split('.');
  337. sec = Convert.ToInt32(strArray3[0]);
  338. mil = Convert.ToInt32(strArray3[1]);
  339. }
  340. else
  341. {
  342. sec = Convert.ToInt32(strArray1[2]);
  343. }
  344. }
  345. catch (Exception)
  346. {
  347. }
  348. return new TimeSpan(day, hour, min, sec, mil);
  349. }
  350. /// <summary>
  351. /// 判断是否为时间格式
  352. /// </summary>
  353. /// <param name="str"></param>
  354. /// <returns></returns>
  355. public bool TimeStrRegularJudg(string str)
  356. {
  357. try
  358. {
  359. Regex reg = new Regex(@"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d.\d\d\d)$");
  360. return reg.IsMatch(str);
  361. }
  362. catch (Exception)
  363. {
  364. return false;
  365. }
  366. }
  367. /// <summary>
  368. /// 鼠标点击移动
  369. /// </summary>
  370. /// <param name="sender"></param>
  371. /// <param name="e"></param>
  372. private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  373. {
  374. DragMove();
  375. }
  376. /// <summary>
  377. /// 启动定时器 同步进度条和视频进度
  378. /// </summary>
  379. /// <param name="sender"></param>
  380. /// <param name="e"></param>
  381. private void Element_MediaOpened(object sender, RoutedEventArgs e)
  382. {
  383. timelineSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds; //设置slider最大值
  384. UcSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds;
  385. UcSlider.StartValue = 0;
  386. UcSlider.EndValue = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds;
  387. UcSlider_PreviewMouseMove(null, null);
  388. int sec = (int)QS_Movie.NaturalDuration.TimeSpan.TotalSeconds;
  389. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //超过计时间隔时发生
  390. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //DT间隔
  391. dispatcherTimer.Start(); //DT启动
  392. #region 调整视频大小
  393. double Prop = (double)QS_Movie.NaturalVideoHeight / (double)QS_Movie.NaturalVideoWidth;
  394. double gridWidth = GridMovie.ActualHeight / Prop;
  395. if (gridWidth <= GridMovie.ActualWidth)
  396. {
  397. QS_Movie.Width = gridWidth;
  398. QS_Movie.Height = GridMovie.ActualHeight;
  399. }
  400. else
  401. {
  402. QS_Movie.Height = GridMovie.ActualWidth * Prop;
  403. QS_Movie.Width = GridMovie.ActualWidth;
  404. }
  405. #endregion
  406. }
  407. }
  408. }