Example #1
0
        private bool disposedValue = false; // Для определения избыточных вызовов

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    FLVFile.Dispose();
                    if (AD1 != null)
                    {
                        AD1.Dispose();
                    }
                    if (AD2 != null)
                    {
                        AD2.Dispose();
                    }
                    if (UpdateStatusTimer != null)
                    {
                        UpdateStatusTimer.Dispose();
                    }
                }
                disposedValue     = true;
                AD1               = null;
                AD2               = null;
                UpdateStatusTimer = null;
            }
        }
Example #2
0
        public void StartDownload(string manifestUrl)
        {
            GetManifestAndSelectMedia(manifestUrl);
            Program.Message("Waiting first fragment...\r");
            // downloader already running in UpdateBootstrapInfo()
            DetermineAudioVideoPresentInDownloadedFragment();
            var        DecoderState = new DecoderLastState();
            bool       useAltAudio  = selectedMediaAlt != null;
            TagsFilter tagFilter    = TagsFilter.ALL;

            if (UpdateStatusTimer == null && !Program.isRedirected)
            {
                UpdateStatusTimer = new Timer(ShowDownloadStatus, null, 0, UpdateStatusInterval);
            }

            if (useAltAudio)
            {
                tagFilter = TagsFilter.VIDEO; // only video for main media if alternate media is selected
            }
            _currentTime       = INVALID_TIME;
            _mediaTime         = INVALID_TIME;
            _alternateTime     = INVALID_TIME;
            droppedAudioFrames = 0;
            droppedVideoFrames = 0;

            FLVTag mediaTag                 = null;
            FLVTag alternateTag             = null;
            bool   needSynchronizationAudio = true;

            if (!Program.ConsolePresent && Program.isRedirected)
            {
                Program.Message("Processing...");
            }
            // --------------- MAIN LOOP DECODE FRAGMENTS ----------------
            while (Downloader.TagsAvaliable(selectedMedia) || selectedMedia.Bootstrap.live)
            {
                if (mediaTag == null)
                {
                    mediaTag = Downloader.GetNextTag(selectedMedia);
                    if (mediaTag == null)
                    {
                        Thread.Sleep(100); // for decrease CPU load
                    }
                }

                if (useAltAudio && _mediaTime != INVALID_TIME && Downloader.TagsAvaliable(selectedMediaAlt))
                {
                    if (alternateTag == null)
                    {
                        alternateTag = Downloader.GetNextTag(selectedMediaAlt);
                    }

                    if (useAltAudio && alternateTag != null && alternateTag.IsAkamaiEncrypted)
                    {
                        if (AD2 == null)   // create and init only if need
                        {
                            AD2 = new AkamaiDecryptor();
                        }
                        AD2.DecryptFLVTag(alternateTag, manifest.baseURL, auth);
                    }

                    if (needSynchronizationAudio && (_mediaTime != INVALID_TIME || _alternateTime != INVALID_TIME))
                    {
                        uint alternateSynchronizationTime = _alternateTime != INVALID_TIME ? _alternateTime : _mediaTime;
                        alternateTag = Downloader.SeekAudioByTime(selectedMediaAlt, alternateSynchronizationTime);
                        if (alternateTag != null)
                        {
                            needSynchronizationAudio = false;
                        }
                    }
                }

                if (mediaTag != null && mediaTag.IsAkamaiEncrypted)
                {
                    if (AD1 == null)   // create and init only if need
                    {
                        AD1 = new AkamaiDecryptor();
                    }
                    AD1.DecryptFLVTag(mediaTag, manifest.baseURL, auth);
                }
                if (useAltAudio && alternateTag != null && alternateTag.IsAkamaiEncrypted)
                {
                    if (AD2 == null)   // create and init only if need
                    {
                        AD2 = new AkamaiDecryptor();
                    }
                    AD2.DecryptFLVTag(alternateTag, manifest.baseURL, auth);
                }

                if (ShouldFilterTag(mediaTag, tagFilter))
                {
                    if (mediaTag != null)
                    {
                        if (mediaTag is FLVTagVideo)
                        {
                            droppedVideoFrames++;
                        }
                        totalDroppedFrames++;
                    }
                    mediaTag = null;
                }
                else
                {
                    UpdateTimes(mediaTag);
                }

                if (ShouldFilterTag(alternateTag, TagsFilter.AUDIO))
                {
                    if (alternateTag != null)
                    {
                        droppedAudioFrames++;
                        totalDroppedFrames++;
                    }
                    alternateTag = null;
                }
                else
                {
                    UpdateTimes(alternateTag);
                }

                if (!useAltAudio)
                {
                    if (mediaTag != null)
                    {
                        _currentTime = mediaTag.Timestamp;
                        FLVFile.Write(mediaTag);
                        mediaTag = null;
                    }
                }
                else
                {
                    if (_mediaTime != INVALID_TIME || _alternateTime != INVALID_TIME)
                    {
                        if (alternateTag != null && (alternateTag.Timestamp >= _currentTime || _currentTime == INVALID_TIME) && (alternateTag.Timestamp <= _mediaTime))
                        {
                            _currentTime = alternateTag.Timestamp;
                            FLVFile.Write(alternateTag);
                            alternateTag = null;
                        }
                        else if (mediaTag != null && (mediaTag.Timestamp >= _currentTime || _currentTime == INVALID_TIME) && (mediaTag.Timestamp <= _alternateTime))
                        {
                            _currentTime = mediaTag.Timestamp;
                            FLVFile.Write(mediaTag);
                            mediaTag = null;
                        }
                    }
                }
                if ((duration > 0) && (FLVFile.LastTimestamp >= duration))
                {
                    Status = "Duration limit reached"; break;
                }
                if ((filesize > 0) && (FLVFile.Filesize >= filesize))
                {
                    Status = "File size limit reached"; break;
                }
            }
            DestroyUpdateStatusTimer();
            ShowDownloadStatus();
        }
Example #3
0
        public static void Main(string[] args)
        {
            System.Net.ServicePointManager.UseNagleAlgorithm = true;
            System.Net.ServicePointManager.CheckCertificateRevocationList = false;
            System.Net.ServicePointManager.DefaultConnectionLimit         = 20;
            System.Net.ServicePointManager.Expect100Continue = false;
            try {
                AppDomain.CurrentDomain.UnhandledException += (sender, e) => FatalExceptionObject(e.ExceptionObject);

                try {
                    isRedirected = Console.CursorVisible && false;
                } catch {
                    isRedirected = true;
                }
                if (isRedirected)
                {
                    Console.SetOut(new StreamWriter(Console.OpenStandardError())
                    {
                        AutoFlush = true
                    });
                }

                Check4Redirect2Process(ref args);

                CLI cli = new CLI(args);
                if (cli.ChkParam("help"))
                {
                    cli.DisplayHelpAndQuit();
                }
                if (cli.ChkParam("version"))
                {
                    cli.DisplayVersionAndQuit();
                }
                if (cli.ChkParam("waitkey"))
                {
                    waitkey = true;
                }
                if (cli.ChkParam("nowaitkey"))
                {
                    waitkey = false;
                }
                if (cli.ChkParam("filesize"))
                {
                    uint.TryParse(cli.GetParam("filesize"), out filesize);
                }
                if (cli.ChkParam("threads"))
                {
                    int.TryParse(cli.GetParam("threads"), out threads);
                }
                if (cli.ChkParam("start"))
                {
                    uint.TryParse(cli.GetParam("start"), out start);
                }
                if (cli.ChkParam("auth"))
                {
                    auth = "?" + cli.GetParam("auth");
                }
                if (cli.ChkParam("headers"))
                {
                    headers = cli.GetParam("headers");
                }
                if (cli.ChkParam("urlbase"))
                {
                    baseUrl = cli.GetParam("urlbase");
                }
                if (cli.ChkParam("quality"))
                {
                    quality = cli.GetParam("quality");
                }
                if (cli.ChkParam("manifest"))
                {
                    manifestUrl = cli.GetParam("manifest");
                }
                if (cli.ChkParam("outdir"))
                {
                    outDir = cli.GetParam("outdir");
                }
                if (cli.ChkParam("outfile"))
                {
                    outFile = cli.GetParam("outfile");
                }
                if (cli.ChkParam("logfile"))
                {
                    logfile = cli.GetParam("logfile");
                }
                if (cli.ChkParam("skip"))
                {
                    fromTimestamp = GetTimestampFromString(cli.GetParam("skip"));
                }
                if (cli.ChkParam("duration"))
                {
                    duration = GetTimestampFromString(cli.GetParam("duration"));
                }
                if (cli.ChkParam("debug"))
                {
                    debug = true;
                }
                if (cli.ChkParam("play"))
                {
                    play = true;
                }
                if (cli.ChkParam("showtime"))
                {
                    showtime = true;
                }
                if (cli.ChkParam("fproxy"))
                {
                    fproxy = true;
                }
                if (cli.ChkParam("continue"))
                {
                    fcontinue = true;
                }
                if (cli.ChkParam("verbose"))
                {
                    verbose = true;
                }
                if (cli.ChkParam("testalt"))
                {
                    testalt = true;
                }
                if (cli.ChkParam("quiet"))
                {
                    quiet = true;
                }
                if (cli.ChkParam("postdata"))
                {
                    postData = cli.GetParam("postdata");
                }
                if (cli.ChkParam("referer"))
                {
                    HTTP.Referer = cli.GetParam("referer");
                }
                if (cli.ChkParam("cookies"))
                {
                    HTTP.Cookies = cli.GetParam("cookies");
                }
                if (cli.ChkParam("useragent"))
                {
                    HTTP.Useragent = cli.GetParam("useragent");
                }
                if (cli.ChkParam("username"))
                {
                    HTTP.Username = cli.GetParam("username");
                }
                if (cli.ChkParam("password"))
                {
                    HTTP.Password = cli.GetParam("password");
                }
                if (cli.ChkParam("proxy"))
                {
                    HTTP.Proxy = cli.GetParam("proxy");
                }
                if (cli.ChkParam("proxyuser"))
                {
                    HTTP.ProxyUsername = cli.GetParam("proxyuser");
                }
                if (cli.ChkParam("proxypass"))
                {
                    HTTP.ProxyPassword = cli.GetParam("proxypass");
                }
                if (cli.ChkParam("osproxy"))
                {
                    HTTP.UseSystemProxy = true;
                }
                if (cli.ChkParam("adkey"))
                {
                    sessionKey = AkamaiDecryptor.Unhexlify(cli.GetParam("adkey"));
                }
                if (cli.ChkParam("lang"))
                {
                    lang = cli.GetParam("lang");
                }
                if (cli.ChkParam("alt"))
                {
                    alt = cli.GetParam("alt");
                }

                //if (HTTP.Referer == "") {
                //    var m = Regex.Match(manifestUrl, @"^(.*?://.*?/)");
                //    if (m.Success) HTTP.Referer = m.Groups[1].Value;
                //}

                if (!string.IsNullOrEmpty(headers))
                {
                    foreach (string header in headers.Split(new char[] { '|', '$' }))
                    {
                        Match m = Regex.Match(header, "(.*?):(.*)");
                        if (m.Success)
                        {
                            string name  = m.Groups[1].Value;
                            string value = m.Groups[2].Value;
                            switch (name.ToLower())
                            {
                            case "referer": HTTP.Referer = value; break;

                            case "useragent":
                            case "user-agent": HTTP.Useragent = value; break;

                            default: HTTP.Headers.Set(name, value); break;
                            }
                        }
                    }
                }

                ShowHeader("HDSdump by WendyH v<c:White>" + Version);

                if (manifestUrl == "")
                {
                    Quit("<c:Red>Please specify the manifest. (switch '<c:White>-h</c>' or '<c:White>--help</c>' for help message)");
                }

                if (showtime)
                {
                    ShowTimeElapsed("", true);
                }

                Check4KnownLinks(manifestUrl);
                cli.Params["manifest"] = manifestUrl;
                if (HTTP.Referer.Length > 0)
                {
                    cli.Params["referer"] = HTTP.Referer;
                }

                bool usePipe = outFile.IndexOf(@"\\.\pipe\") == 0;
                cli.Params["threads"] = threads.ToString();
                cli.Params["outfile"] = redir2Prog != null ? redir2Prog.ProcessName : (isRedirected && !usePipe ? "<redirected>" : outFile);
                cli.EchoSetsParameters();

                if (!cli.ChkParam("oldmethod"))
                {
                    HDSDumper HdsDumper = new HDSDumper();
                    HdsDumper.FLVFile.outFile       = outDir + outFile;
                    HdsDumper.FLVFile.play          = play;
                    HdsDumper.FLVFile.usePipe       = usePipe;
                    HdsDumper.Downloader.maxThreads = threads;
                    HdsDumper.duration      = duration;
                    HdsDumper.filesize      = filesize;
                    HdsDumper.start         = start;
                    HdsDumper.auth          = auth;
                    HdsDumper.baseUrl       = baseUrl;
                    HdsDumper.quality       = quality;
                    HdsDumper.postData      = postData;
                    HdsDumper.lang          = lang;
                    HdsDumper.alt           = alt;
                    HdsDumper.testalt       = testalt;
                    HdsDumper.fromTimestamp = fromTimestamp;
                    HdsDumper.sessionKey    = sessionKey;

                    if (fcontinue)
                    {
                        HdsDumper.FLVFile.GetLastTimestampFromExistingFile();
                        HdsDumper.fromTimestamp = HdsDumper.FLVFile.LastTimestamp;
                    }

                    if (ConsolePresent)
                    {
                        Console.CancelKeyPress += delegate {
                            HdsDumper?.ShowMessageAtTheEnd();
                            HdsDumper?.FixFileMetadata();
                        };
                    }

                    try {
                        Message("Processing manifest info...");
                        HdsDumper.StartDownload(manifestUrl);
                    } catch (Exception e) {
                        HdsDumper.DestroyUpdateStatusTimer();
                        Message("\r\n<c:Red>" + e.Message);
                    } finally {
                        HdsDumper.ShowMessageAtTheEnd();
                        HdsDumper.FixFileMetadata();
                    }
                    if (!string.IsNullOrEmpty(HdsDumper.Status))
                    {
                        Quit(HdsDumper.Status);
                    }
                    else
                    {
                        Quit("Done.");
                    }
                }

                // ========== OLD METHOD ==========
                F4FOldMethod f4f = new F4FOldMethod();
                // Disable metadata if it invalidates the stream duration
                if ((f4f.fromTimestamp > 0) || (start > 0) || (duration > 0) || (filesize > 0))
                {
                    f4f.metadata = false;
                }

                f4f.fromTimestamp = fromTimestamp;
                f4f.usePipe       = usePipe;
                f4f.play          = play;
                f4f.threads       = threads;
                f4f.duration      = (int)duration;
                f4f.filesize      = (int)filesize;
                f4f.start         = (int)start;
                f4f.ad.sessionKey = sessionKey;
                f4f.auth          = auth;
                f4f.baseUrl       = baseUrl;
                f4f.quality       = quality;
                f4f.PostData      = postData;
                f4f.Lang          = lang;
                f4f.alt           = alt;
                if (fcontinue)
                {
                    f4f.CheckLastTSExistingFile();
                }

                f4f.DownloadFragments(manifestUrl);
                Quit("Done.");
            } catch (Exception huh) {
                FatalExceptionObject(huh);
            }
        }