diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml
index 0b50635..4aa333f 100644
--- a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml
+++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml
@@ -10,17 +10,26 @@
mc:Ignorable="d"
Title="皓月流媒体下载器(m3u8版) Ver 0.0.1 alpha" Height="450" Width="645">
-
-
-
-
-
-
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml.cs b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml.cs
index 356489c..3a8d727 100644
--- a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml.cs
+++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@@ -25,8 +26,16 @@ namespace HaoYue.MediaDownloader
/// [0] 视频格式 [1]m3u8文件
///
public int filetype { get; set; }
+ ///
+ /// [0]未处理[1]在下中[2]已下载[3]处理失败
+ ///
+ public int State { get; set; }
+
+ public int Index { get; set; }
}
+
+
///
/// MainWindow.xaml 的交互逻辑
///
@@ -35,12 +44,26 @@ namespace HaoYue.MediaDownloader
public MainWindow()
{
InitializeComponent();
+ this.TextVideoPath.Text = Environment.CurrentDirectory;
}
+ ///
+ /// 任务列表
+ ///
+ public static List TaskList;
+
string mypath = Environment.CurrentDirectory;
private void DoStart(object sender, RoutedEventArgs e)
{
+ MyRun();
+ }
+
+ public void MyRun()
+ {
+ //禁止UI在处理过程中操作
+ this.TextUrl.IsEnabled = false;
+ this.BtnDoStart.IsEnabled = false;
AddLog("处理");
if (this.TextUrl.Text == "")
{
@@ -48,33 +71,46 @@ namespace HaoYue.MediaDownloader
}
else
{
- this.TextUrl.IsEnabled = false;
- this.BtnDoStart.IsEnabled = false;
- string ThisTaskDir = mypath + "\\Download\\" + DateTime.Now.ToString("yyyyMMdd-hhmmss.FFF") + "\\";
+ string M3u8FileUrl = this.TextUrl.Text;
+ string SiteDir = this.TextUrl.Text.Substring(0, this.TextUrl.Text.LastIndexOf('/')) + "/";
+ string M3u8FileName = this.TextUrl.Text.Substring(this.TextUrl.Text.LastIndexOf('/') + 1);
- if (!Directory.Exists(ThisTaskDir))
+ ThreadPool.QueueUserWorkItem((obj) =>
{
- Directory.CreateDirectory(ThisTaskDir);
- AddLog("创建媒体任务目录 "+ThisTaskDir);
- }
- string FileName = this.TextUrl.Text.Substring(this.TextUrl.Text.LastIndexOf('/')+1);
- string SiteDir = this.TextUrl.Text.Substring(0,this.TextUrl.Text.LastIndexOf('/'));
+ string ThisTaskDir = mypath + "\\Download\\" + DateTime.Now.ToString("yyyyMMdd-hhmmss.FFF") + "\\";
+
- AddLog("开始下载m3u8文件 " + FileName);
- HttpHelper.Download(this.TextUrl.Text,ThisTaskDir + FileName);
- AddLog("下载完毕");
+ if (!Directory.Exists(ThisTaskDir))
+ {
+ Directory.CreateDirectory(ThisTaskDir);
+ AddLog("创建媒体任务目录 " + ThisTaskDir);
+ }
- CheckM3u8File(ThisTaskDir + FileName);
+ AddLog("开始下载m3u8文件 " + M3u8FileName);
+ HttpHelper.Download(M3u8FileUrl, ThisTaskDir + M3u8FileName);
+ AddLog("下载完毕");
- AddLog("处理完毕");
+ TaskList = CheckM3u8File(ThisTaskDir + M3u8FileName);
+
+ AddLog("开始下载流程");
+ StartDownLoad(SiteDir,ThisTaskDir);
+ AddLog("开始拼接视频分片");
+ //AddLog("处理完毕");
+ ////恢复UI可操作
+ //this.TextUrl.IsEnabled = true;
+ //this.BtnDoStart.IsEnabled = true;
+ }, null);
}
}
+
+
private List CheckM3u8File(string FilePath)
{
AddLog("开始解析m3u8文件");
var ALLList = new List();
string FileText = File.ReadAllText(FilePath);
+ int i = 0;
foreach (string s in FileText.Split('\n'))
{
if (s.Trim().Length > 0 && s.Trim().Substring(0, 1) != "#")//去空格后长度不为1 且不是注释
@@ -86,21 +122,113 @@ namespace HaoYue.MediaDownloader
{
filetype = 1;
}
- ALLList.Add(new ModelOfM3u8() { Path = s ,filetype = filetype});
+ ALLList.Add(new ModelOfM3u8() { Path = s ,filetype = filetype,State = 0,Index = ++i });
}
}
- AddLog("解析完毕,共"+ALLList.Where(w => w.filetype == 0 ).Count()+"个ts视频切片");
+ AddLog("解析完毕,共"+ALLList.Where(w => w.filetype == 0 ).Count()+"个ts视频切片,共"+ALLList.Where(w => w.filetype == 1 ).Count()+"个m3u8配置文件");
return ALLList;
}
+ public void StartDownLoad(string SiteDir,string ThisTaskDir)
+ {
+ //如果队列中还有未进行的任务则进行
+ while (TaskList.Where(w => w.State == 0).Count() > 0){
+ var taskinfo = TaskList.Where(w => w.State == 0).FirstOrDefault();
+ if (taskinfo != null)
+ {
+ TaskList[TaskList.IndexOf(taskinfo)].State = 1;
+ AddLog("开始下载文件" + taskinfo.Path);
+ HttpHelper.Download(SiteDir + taskinfo.Path, ThisTaskDir + taskinfo.Index.ToString("000000")+".ts");
+ //更新进度条
+ this.MyProgressBar.Dispatcher.BeginInvoke(new mydoublesetter(SetProgressBar),
+ ((double)TaskList.Where(w => w.State != 0).Count() / (double)TaskList.Count()) * 100.0f
+ );
+ AddLog("" + taskinfo.Path + " 下载完毕");
+ }
+ }
+ AddLog("进程结束");
+
+ CommanderStr(ThisTaskDir);
+
+ if (File.Exists(ThisTaskDir + "\\final.ts"))
+ {
+ //移动文件到上一层
+ AddLog("移动文件到上一层");
+ string newfilapath = (ThisTaskDir + "\\..\\" + "\\" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".ts");
+ File.Move((ThisTaskDir + "\\final.ts"), newfilapath);
+
+ //删除视频切片
+ AddLog("删除视频切片");
+ Directory.Delete(ThisTaskDir);
+ System.Diagnostics.Process.Start("Explorer.exe", ThisTaskDir + "\\..\\");
+ AddLog("处理完成");
+ }
+ else
+ {
+ AddLog("合并处理失败");
+ }
+ }
+
+
+ public void CommanderStr(string FileItemDir)
+ {
+ string c = @"copy /b "+ FileItemDir + "\\*.ts " + FileItemDir + "\\final.ts";
+ Cmd(c);
+ }
+
+ ///
+ /// 执行Cmd命令
+ ///
+ private void Cmd(string c)
+ {
+ try
+ {
+ System.Diagnostics.Process process = new System.Diagnostics.Process();
+ process.StartInfo.FileName = "cmd.exe";
+ process.StartInfo.UseShellExecute = false;
+ process.StartInfo.CreateNoWindow = true;
+ process.StartInfo.RedirectStandardOutput = true;
+ process.StartInfo.RedirectStandardInput = true;
+ process.Start();
+ process.StandardInput.WriteLine(c);
+ process.StandardInput.AutoFlush = true;
+ process.StandardInput.WriteLine("exit");
+
+ StreamReader reader = process.StandardOutput;//截取输出流
+ process.WaitForExit();
+ }
+ catch
+ { }
+ }
+
+
+
public void AddLog(string str)
{
- this.ListLog.Items.Add(DateTime.Now.ToString("hh:mm:ss >") + str);
- //要使用显示最后一行
- this.ListLog.SelectedIndex = this.ListLog.Items.Count - 1;
- this.ListLog.ScrollIntoView(this.ListLog.SelectedIndex);
- //待续...
+ this.ListLog.Dispatcher.BeginInvoke(new mystringsetter(Set_myLog), DateTime.Now.ToString("hh:mm:ss >") + str);
+ //this.ListLog.Items.Add(DateTime.Now.ToString("hh:mm:ss >") + str);
+ ////要使用显示最后一行
+ //this.ListLog.SelectedIndex = this.ListLog.Items.Count - 1;
+ //this.ListLog.ScrollIntoView(this.ListLog.SelectedIndex);
+ ////待续...
}
+
+ //一些类型委托
+ public delegate void mydoublesetter(double value);
+ public delegate void mystringsetter(string value);
+
+
+ public void Set_myLog(string value)
+ {
+ this.ListLog.Items.Add(value);
+ this.ListLog.ScrollIntoView(ListLog.Items[ListLog.Items.Count - 1]);
+ }
+
+ public void SetProgressBar(double value)
+ {
+ this.MyProgressBar.Value = value;
+ }
+
}
}