protected void OnCompleted(ProgressionArgs progressionArgs) { if (Completed != null) { Completed(this, progressionArgs); } }
protected void OnProgression(ProgressionArgs progressionArgs) { if (Progression != null) { Progression(this, progressionArgs); } }
private static void FillVideoSize(ProgressionArgs progression, Dictionary <string, string> thumbInfo) { var size = thumbInfo["size_high"]; if (progression.Mode == Mode.Low) { size = thumbInfo["size_low"]; } progression.VideoSize = long.Parse(size); }
private void Save(Stream stream, string filePath, ProgressionArgs videoInfo) { long buffSize = Math.Min(videoInfo.VideoSize, 1024 * 1024); byte[] buff = new byte[buffSize]; var readSize = stream.Read(buff, 0, 3); if (buff[0] == 'F' && buff[1] == 'L' && buff[2] == 'V') { filePath += ".flv"; } else { filePath += ".mp4"; } var tempFilePath = Path.GetTempFileName(); long totalReadSize = readSize; FileStreamArgs fileStreamArgs = new FileStreamArgs(); using (var outStream = File.OpenWrite(tempFilePath)) { fileStreamArgs.FileStream = outStream; outStream.Write(buff, 0, 3); while (readSize != 0) { readSize = stream.Read(buff, 0, buff.Length); outStream.Write(buff, 0, readSize); totalReadSize += readSize; videoInfo.DownloadedSize = totalReadSize; OnProgression(videoInfo); OnStreamWrite(fileStreamArgs); if (!outStream.CanWrite) { return; } } } if (File.Exists(filePath)) { File.Delete(filePath); } File.Move(tempFilePath, filePath); }
public bool Recode(string url, string path, bool allowedLowMode) { var thumbInfo = GetThumbInfo(url); var videoInfo = GetFlv(url); var progression = new ProgressionArgs() { Title = thumbInfo["title"] }; progression.Mode = GetMode(videoInfo["url"]); if (!videoInfo.ContainsKey("url") || (progression.Mode == Mode.Low && !allowedLowMode)) { return(false); } RequestPage(url); var req = (HttpWebRequest)WebRequest.Create(videoInfo["url"]); req.CookieContainer = _cookie; FillVideoSize(progression, thumbInfo); using (var stream = req.GetResponse().GetResponseStream()) { var fileName = thumbInfo["title"]; Array.ForEach(Path.GetInvalidFileNameChars(), c => fileName = fileName.Replace(c.ToString(), "")); Path.GetInvalidFileNameChars().ForEach(c => fileName = fileName.Replace(c.ToString(), "")); var filePath = Path.Combine(path, fileName); OnFilePathDecide(ref filePath); Save(stream, filePath, progression); } return(true); }