完成基本u3m8解析、下载切片并拼接视频功能(待续)

This commit is contained in:
陈皓 2018-10-05 16:49:19 +08:00
parent 56daae0709
commit 73ed54c4f0
2 changed files with 169 additions and 32 deletions

View File

@ -10,17 +10,26 @@
mc:Ignorable="d"
Title="皓月流媒体下载器(m3u8版) Ver 0.0.1 alpha" Height="450" Width="645">
<Grid>
<Label Content="m3u8流媒体URL地址" HorizontalAlignment="Left" Margin="10,11,0,0" VerticalAlignment="Top" Width="133"/>
<TextBox HorizontalAlignment="Left" x:Name="TextUrl" Height="23" Margin="148,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="399"/>
<Button x:Name="BtnDoStart" Content="开 始 解 析" HorizontalAlignment="Left" Margin="552,11,0,0" VerticalAlignment="Top" Width="75" Click="DoStart"/>
<Label Content="处理日志:" HorizontalAlignment="Left" Margin="10,32,0,0" VerticalAlignment="Top"/>
<ListBox x:Name="ListLog" HorizontalAlignment="Left" Height="137" Margin="10,62,0,0" VerticalAlignment="Top" Width="617" IsSynchronizedWithCurrentItem="True"
Background="#888888"
<Label Content="m3u8流媒体URL地址" HorizontalAlignment="Left" Margin="10,32,0,0" VerticalAlignment="Top" Width="133"/>
<TextBox HorizontalAlignment="Left" x:Name="TextUrl" Height="23" Margin="148,31,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="399"
/>
<Label Content="下载进度:" HorizontalAlignment="Left" Margin="10,392,0,0" VerticalAlignment="Top"/>
<ProgressBar HorizontalAlignment="Left" Height="4" Margin="84,402,0,0" VerticalAlignment="Top" Width="543"/>
<Button x:Name="BtnDoStart" Content="开 始 解 析" HorizontalAlignment="Left" Margin="552,32,0,0" VerticalAlignment="Top" Width="75" Click="DoStart"/>
<Label Content="处理日志:" HorizontalAlignment="Left" Margin="9,109,0,0" VerticalAlignment="Top"/>
<ListBox x:Name="ListLog" HorizontalAlignment="Left" Height="248" Margin="10,139,0,0" VerticalAlignment="Top" Width="617" IsSynchronizedWithCurrentItem="True"
Background="#888888"
/>
<Label Content="下载进度:" HorizontalAlignment="Left" Margin="9,390,0,0" VerticalAlignment="Top"/>
<ProgressBar x:Name="MyProgressBar" HorizontalAlignment="Left" Height="20" Margin="84,392,0,0" VerticalAlignment="Top" Width="543"/>
<Label Content="线程数(暂未实现)" HorizontalAlignment="Left" Margin="403,66,0,0" VerticalAlignment="Top"/>
<Slider HorizontalAlignment="Left" IsEnabled="False" Margin="524,71,0,0" VerticalAlignment="Top" Width="95"/>
<Button Content="..." HorizontalAlignment="Left" Margin="368,65,0,0" VerticalAlignment="Top" Width="20"/>
<Label Content="视频存储路径:" HorizontalAlignment="Left" Margin="10,66,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="TextVideoPath" IsEnabled="False" HorizontalAlignment="Left" Height="23" Margin="148,65,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="202"/>
<RadioButton Content="通用模式" IsChecked="True" HorizontalAlignment="Left" Margin="14,5,0,0" VerticalAlignment="Top"/>
<RadioButton Content="重庆电视台视频解析" IsEnabled="False" HorizontalAlignment="Left" Margin="115,5,0,0" VerticalAlignment="Top"/>
</Grid>
</controls:MetroWindow>

View File

@ -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文件
/// </summary>
public int filetype { get; set; }
/// <summary>
/// [0]未处理[1]在下中[2]已下载[3]处理失败
/// </summary>
public int State { get; set; }
public int Index { get; set; }
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
@ -35,12 +44,26 @@ namespace HaoYue.MediaDownloader
public MainWindow()
{
InitializeComponent();
this.TextVideoPath.Text = Environment.CurrentDirectory;
}
/// <summary>
/// 任务列表
/// </summary>
public static List<ModelOfM3u8> 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<ModelOfM3u8> CheckM3u8File(string FilePath)
{
AddLog("开始解析m3u8文件");
var ALLList = new List<ModelOfM3u8>();
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);
}
/// <summary>
/// 执行Cmd命令
/// </summary>
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;
}
}
}