commit 56daae07096d3049ed56ce58df28884f749a89c1 Author: 陈皓 <353374337@qq.com> Date: Fri Oct 5 12:39:32 2018 +0800 第一部实现解析m3u8 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c4efe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,261 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader.sln b/HaoYue.MediaDownloader/HaoYue.MediaDownloader.sln new file mode 100644 index 0000000..7516874 --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2026 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaoYue.MediaDownloader", "HaoYue.MediaDownloader\HaoYue.MediaDownloader.csproj", "{5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CB43CA6F-26B2-469E-B70C-D4AF311882CE} + EndGlobalSection +EndGlobal diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.config b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml new file mode 100644 index 0000000..189b4e6 --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml.cs b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml.cs new file mode 100644 index 0000000..391d3ca --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace HaoYue.MediaDownloader +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HaoYue.MediaDownloader.csproj b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HaoYue.MediaDownloader.csproj new file mode 100644 index 0000000..7b0b20b --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HaoYue.MediaDownloader.csproj @@ -0,0 +1,108 @@ + + + + + Debug + AnyCPU + {5DBDA955-1253-4DA7-8688-A9C9A0DA9BE9} + WinExe + HaoYue.MediaDownloader + HaoYue.MediaDownloader + v4.5.2 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\ControlzEx.3.0.2.4\lib\net45\ControlzEx.dll + + + ..\packages\MahApps.Metro.1.6.5\lib\net45\MahApps.Metro.dll + + + + + ..\packages\ControlzEx.3.0.2.4\lib\net45\System.Windows.Interactivity.dll + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + \ No newline at end of file diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HttpHelper.cs b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HttpHelper.cs new file mode 100644 index 0000000..c8c5c70 --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/HttpHelper.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace HaoYue.MediaDownloader +{ + public static class HttpHelper + { + /// + /// Http方式下载文件 + /// + /// http地址 + /// 本地文件 + /// + public static bool Download(string url, string localfile) + { + bool flag = false; + long startPosition = 0; // 上次下载的文件起始位置 + FileStream writeStream; // 写入本地文件流对象 + + long remoteFileLength = GetHttpLength(url);// 取得远程文件长度 + System.Console.WriteLine("remoteFileLength=" + remoteFileLength); + if (remoteFileLength == 745) + { + System.Console.WriteLine("远程文件不存在."); + return false; + } + + // 判断要下载的文件夹是否存在 + if (File.Exists(localfile)) + { + + writeStream = File.OpenWrite(localfile); // 存在则打开要下载的文件 + startPosition = writeStream.Length; // 获取已经下载的长度 + + if (startPosition >= remoteFileLength) + { + System.Console.WriteLine("本地文件长度" + startPosition + "已经大于等于远程文件长度" + remoteFileLength); + writeStream.Close(); + + return false; + } + else + { + writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位 + } + } + else + { + writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存创建一个文件 + startPosition = 0; + } + + + try + { + HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接 + + if (startPosition > 0) + { + myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置 + } + + + Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流 + + + byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容 + int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次 + + long currPostion = startPosition; + + while (contentSize > 0)// 如果读取长度大于零则继续读 + { + currPostion += contentSize; + int percent = (int)(currPostion * 100 / remoteFileLength); + System.Console.WriteLine("percent=" + percent + "%"); + + writeStream.Write(btArray, 0, contentSize);// 写入本地文件 + contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取 + } + + //关闭流 + writeStream.Close(); + readStream.Close(); + + flag = true; //返回true下载成功 + } + catch (Exception) + { + writeStream.Close(); + flag = false; //返回false下载失败 + } + + return flag; + } + + // 从文件头得到远程文件的长度 + private static long GetHttpLength(string url) + { + long length = 0; + + try + { + HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接 + HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); + + if (rsp.StatusCode == HttpStatusCode.OK) + { + length = rsp.ContentLength;// 从文件头得到远程文件的长度 + } + + rsp.Close(); + return length; + } + catch (Exception e) + { + return length; + } + + } + } +} diff --git a/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml new file mode 100644 index 0000000..0b50635 --- /dev/null +++ b/HaoYue.MediaDownloader/HaoYue.MediaDownloader/MainWindow.xaml @@ -0,0 +1,26 @@ + + +