using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace XHWK.WKTool
{
using system;
///
/// 视频剪辑
/// VideoClipWindow.xaml 的交互逻辑
///
public partial class VideoClipWindow
{
///
/// 视频地址
///
string _videoPath;
///
/// 视频剪辑
///
public VideoClipWindow()
{
InitializeComponent();
Width = App.W_XHMicroLessonSystemWindow.ActualWidth;
Height = App.W_XHMicroLessonSystemWindow.ActualHeight;
}
public bool InitClipVideo(string mVideoPath)
{
if (FileToolsCommon.IsExistFile(mVideoPath))
{
_videoPath = mVideoPath;
QsMovie.Source = new Uri(_videoPath);
double prop = QsMovie.NaturalVideoHeight / (double)QsMovie.NaturalVideoWidth;
double gridWidth = GridMovie.ActualHeight / prop;
if (gridWidth <= GridMovie.ActualWidth)
{
QsMovie.Width = gridWidth;
QsMovie.Height = GridMovie.ActualHeight;
}
else
{
QsMovie.Height = GridMovie.ActualWidth * prop;
QsMovie.Width = GridMovie.ActualWidth;
}
DataContext = App.PageContextData;
}
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;
///
/// 播放
///
public void MoviePlay()
{
_isPlay = true;
ImgPlay.Visibility = Visibility.Collapsed;
ImgPause.Visibility = Visibility.Visible;
QsMovie.Play();
}
///
/// 暂停
///
public void MoviePause()
{
_isPlay = false;
ImgPlay.Visibility = Visibility.Visible;
ImgPause.Visibility = Visibility.Collapsed;
QsMovie.Pause();
}
///
/// slider滑动值随播放内容位置变化
///
///
///
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
TimelineSlider.Value = QsMovie.Position.TotalMilliseconds; //slider滑动值随播放内容位置变化
LblProgress.Content = new TimeSpan(
0,
0,
0,
0,
(int)QsMovie.Position.TotalMilliseconds
).ToString(@"hh\:mm\:ss");
}
///
/// 拖动进度条
///
///
///
private void TimelineMDown(object sender, EventArgs e)
{
_dispatcherTimer.Stop();
}
///
/// 拖动进度条
///
///
///
private void TimelineMUp(object sender, EventArgs e)
{
QsMovie.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 (QsMovie.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();
QsMovie.Stop();
_isPlay = false;
ImgPlay.Visibility = Visibility.Visible;
ImgPause.Visibility = Visibility.Collapsed;
UcSlider.StartValue = 0;
TimelineSlider.Value = 0;
_dispatcherTimer.Stop();
Hide();
}
double _statime;
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;
QsMovie.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 > QsMovie.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 > QsMovie.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)
{
// ignored
}
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 = QsMovie.NaturalDuration.TimeSpan.TotalMilliseconds; //设置slider最大值
UcSlider.Maximum = QsMovie.NaturalDuration.TimeSpan.TotalMilliseconds;
UcSlider.StartValue = 0;
UcSlider.EndValue = QsMovie.NaturalDuration.TimeSpan.TotalMilliseconds;
UcSlider_PreviewMouseMove(null, null);
_dispatcherTimer.Tick += dispatcherTimer_Tick; //超过计时间隔时发生
_dispatcherTimer.Interval = new TimeSpan(
0,
0,
0,
0,
500
); //DT间隔
_dispatcherTimer.Start(); //DT启动
#region 调整视频大小
double prop = QsMovie.NaturalVideoHeight / (double)QsMovie.NaturalVideoWidth;
double gridWidth = GridMovie.ActualHeight / prop;
if (gridWidth <= GridMovie.ActualWidth)
{
QsMovie.Width = gridWidth;
QsMovie.Height = GridMovie.ActualHeight;
}
else
{
QsMovie.Height = GridMovie.ActualWidth * prop;
QsMovie.Width = GridMovie.ActualWidth;
}
#endregion
}
}
}