星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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