星火微课系统客户端
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

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