星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VideoClipWindow.xaml.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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(VideoPath, startTime, EndTime, filePath);
  234. MessageBoxResult br = MessageWindow.Show("剪辑完成是否打开文件夹?", "剪辑", MessageBoxButton.OKCancel);
  235. if (br == MessageBoxResult.OK)
  236. {
  237. //System.Diagnostics.Process.Start(FileToolsCommon.GetDirectoryName(filePath));
  238. System.Diagnostics.Process.Start("explorer.exe", "/select," + filePath);
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// 输入开始时间
  244. /// </summary>
  245. /// <param name="sender"></param>
  246. /// <param name="e"></param>
  247. private void TxbStartTime_LostFocus(object sender, RoutedEventArgs e)
  248. {
  249. //判断是否为时间格式
  250. if (!TimeStrRegularJudg(TxbStartTime.Text))
  251. {
  252. MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff");
  253. return;
  254. }
  255. TimeSpan time = ConvertToTimeSpan(TxbStartTime.Text);
  256. if (time.TotalMilliseconds > UcSlider.EndValue)
  257. {
  258. MessageWindow.Show("开始时间不能大于结束时间!");
  259. return;
  260. }
  261. if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds - 1000 || time.TotalMilliseconds > UcSlider.EndValue - 1000)
  262. {
  263. MessageWindow.Show("剪辑时间不能小于1秒!");
  264. return;
  265. }
  266. UcSlider.StartValue = time.TotalMilliseconds;
  267. UcSlider_PreviewMouseLeftButtonUp(null, null);
  268. }
  269. /// <summary>
  270. /// 输入结束时间
  271. /// </summary>
  272. /// <param name="sender"></param>
  273. /// <param name="e"></param>
  274. private void TxbEndTime_LostFocus(object sender, RoutedEventArgs e)
  275. {
  276. //判断是否为时间格式
  277. if (!TimeStrRegularJudg(TxbEndTime.Text))
  278. {
  279. MessageWindow.Show("请输入正确的时间格式:hh:mm:ss.fff");
  280. return;
  281. }
  282. TimeSpan time = ConvertToTimeSpan(TxbEndTime.Text);
  283. if (time.TotalMilliseconds < UcSlider.StartValue)
  284. {
  285. MessageWindow.Show("结束时间不能小于开始时间!");
  286. return;
  287. }
  288. if (time.TotalMilliseconds < UcSlider.StartValue + 1000)
  289. {
  290. MessageWindow.Show("剪辑时间不能小于1秒!");
  291. return;
  292. }
  293. if (time.TotalMilliseconds > QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds)
  294. {
  295. MessageWindow.Show("结束时间不能超出视频总时长!");
  296. return;
  297. }
  298. UcSlider.EndValue = time.TotalMilliseconds;
  299. UcSlider_PreviewMouseLeftButtonUp(null, null);
  300. }
  301. /// <summary>
  302. /// string转TimeSpan
  303. /// </summary>
  304. /// <param name="str"></param>
  305. /// <returns></returns>
  306. public TimeSpan ConvertToTimeSpan(string str)
  307. {
  308. int day = 0;
  309. int hour = 0;
  310. int min = 0;
  311. int sec = 0;
  312. int mil = 0;
  313. string[] strArray1 = str.Split(':');
  314. try
  315. {
  316. if (strArray1[0].Contains("."))
  317. {
  318. string[] strArray2 = strArray1[0].Split('.');
  319. day = Convert.ToInt32(strArray2[0]);
  320. hour = Convert.ToInt32(strArray2[1]);
  321. }
  322. else
  323. {
  324. day = 0;
  325. hour = Convert.ToInt32(strArray1[0]);
  326. }
  327. min = Convert.ToInt32(strArray1[1]);
  328. if (strArray1[2].Contains("."))
  329. {
  330. string[] strArray3 = strArray1[2].Split('.');
  331. sec = Convert.ToInt32(strArray3[0]);
  332. mil = Convert.ToInt32(strArray3[1]);
  333. }
  334. else
  335. {
  336. sec = Convert.ToInt32(strArray1[2]);
  337. }
  338. }
  339. catch (Exception)
  340. {
  341. }
  342. return new TimeSpan(day, hour, min, sec, mil);
  343. }
  344. /// <summary>
  345. /// 判断是否为时间格式
  346. /// </summary>
  347. /// <param name="str"></param>
  348. /// <returns></returns>
  349. public bool TimeStrRegularJudg(string str)
  350. {
  351. try
  352. {
  353. Regex reg = new Regex(@"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d.\d\d\d)$");
  354. return reg.IsMatch(str);
  355. }
  356. catch (Exception)
  357. {
  358. return false;
  359. }
  360. }
  361. /// <summary>
  362. /// 鼠标点击移动
  363. /// </summary>
  364. /// <param name="sender"></param>
  365. /// <param name="e"></param>
  366. private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  367. {
  368. DragMove();
  369. }
  370. /// <summary>
  371. /// 启动定时器 同步进度条和视频进度
  372. /// </summary>
  373. /// <param name="sender"></param>
  374. /// <param name="e"></param>
  375. private void Element_MediaOpened(object sender, RoutedEventArgs e)
  376. {
  377. timelineSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds; //设置slider最大值
  378. UcSlider.Maximum = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds;
  379. UcSlider.StartValue = 0;
  380. UcSlider.EndValue = QS_Movie.NaturalDuration.TimeSpan.TotalMilliseconds;
  381. UcSlider_PreviewMouseMove(null, null);
  382. int sec = (int)QS_Movie.NaturalDuration.TimeSpan.TotalSeconds;
  383. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //超过计时间隔时发生
  384. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //DT间隔
  385. dispatcherTimer.Start(); //DT启动
  386. #region 调整视频大小
  387. double Prop = (double)QS_Movie.NaturalVideoHeight / (double)QS_Movie.NaturalVideoWidth;
  388. double gridWidth = GridMovie.ActualHeight / Prop;
  389. if (gridWidth <= GridMovie.ActualWidth)
  390. {
  391. QS_Movie.Width = gridWidth;
  392. QS_Movie.Height = GridMovie.ActualHeight;
  393. }
  394. else
  395. {
  396. QS_Movie.Height = GridMovie.ActualWidth * Prop;
  397. QS_Movie.Width = GridMovie.ActualWidth;
  398. }
  399. #endregion
  400. }
  401. }
  402. }