Пример #1
0
        private string GetEnglishSubtitleUrl(string videoUrl)
        {
            string postUrl  = "http://www.amara.org/widget/rpc/xhr/show_widget";
            string postData = "video_url=%22" + videoUrl + "%22&is_remote=false&base_state=%7B%7D";
            var    txt      = DownloadHelper.DownloadHtml(postUrl, postData);

            //MessageBox.Show(txt);
            txt = txt.Substring(txt.IndexOf("\"en\""));
            txt = txt.Substring(txt.IndexOf("\"pk\":") + 6);
            var pk = txt.Substring(0, txt.IndexOf(","));

            txt = txt.Substring(txt.IndexOf("video_id") + 12);
            var vid = txt.Substring(0, txt.IndexOf("\""));

            return(string.Format("http://www.amara.org/widget/download-subs/srt/?video_id={0}&lang_pk={1}", vid, pk));
        }
Пример #2
0
        private IList <string> GetVideoUrls(string playListUrl)
        {
            if (!playListUrl.Contains("playlist"))
            {
                return(new List <string>()
                {
                    playListUrl
                });
            }
            var html   = DownloadHelper.DownloadHtml(playListUrl);
            var list   = watchRegex.Matches(html);
            var result = new List <string>();

            foreach (Match match in list)
            {
                string wUrl = match.Groups[1].Value;
                Debug.WriteLine(wUrl);
                result.Add("https://www.youtube.com" + wUrl);
            }
            return(result.Distinct().ToList());
        }
Пример #3
0
        private void btnDownloadSubtitle_Click(object sender, EventArgs e)
        {
            string[] lines = richTextBox1.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string line in lines)
            {
                foreach (string url in GetVideoUrls(line))
                {
                    try
                    {
                        var    subtitleUrl = GetEnglishSubtitleUrl(url);
                        string fileName;
                        var    content = DownloadHelper.DownloadTxtFile(subtitleUrl, out fileName);
                        var    path    = txbSaveFolder.Text + "\\" + fileName;
                        WriteFile(path, content);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(url + " error:" + ex.Message);
                    }
                }
            }
            MessageBox.Show("OK");
        }
Пример #4
0
        //private static int ThreadCount = 0;
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var argument = (ConversionTaskParameters)e.Argument;
            BackgroundWorker backgroundWorker = argument.BackgroundWorker;

            backgroundWorker.ReportProgress(0, "Waiting in queue");
            semaphore.Wait();
            //while (ThreadCount>=2)
            //{
            //    backgroundWorker.ReportProgress(0,"Waiting in queue");
            //    Thread.Sleep(5000);
            //}
            ////ThreadCount++;
            //Interlocked.Increment(ref ThreadCount);

            //this.CreateDownloadFolder();
            if (argument.ConversionProfile != null)
            {
                string fileNameWithoutExtension;
                string uRL;
                string str3;
                if (argument.OriginalFileLocation == null)
                {
                    throw new Exception("Incorect video location specified");
                }
                var  result = new VideoResult();
                bool flag   = false;
                if (
                    Regex.Match(argument.OriginalFileLocation,
                                @"(?:[Yy][Oo][Uu][Tt][Uu][Bb][Ee]\.[Cc][Oo][Mm]/((?:(?:(?:watch)|(?:watch_popup))(?:(?:\?|\#|\!|\&)?[\w]*=[\w]*)*(?:\?|\#|\!|\&)?v=(?<vid>-?[\w|-]*))|(?:v/(?<vid>-?[\w|-]*))))|(?:[Yy][Oo][Uu][Tt][Uu].[Bb][Ee]/(?<vid>-?[\w|-]*))")
                    .Success)
                {
                    string str4;
                    backgroundWorker.ReportProgress(0, "Determining location of the video stream");
                    var u = new YouTubeDownloader.YouTubeDownloader();
                    ResourceLocation location = u.ResolveVideoURL(argument.OriginalFileLocation, argument.QualityIndex,
                                                                  argument.ConversionProfile.PreferedType, out str4);
                    if ((location == null) || string.IsNullOrEmpty(location.URL))
                    {
                        throw new Exception("Unable to obtain initial information about this video");
                    }
                    flag = ((argument.ConversionProfile.PreferedType !=
                             YouTubeDownloader.YouTubeDownloader.VideoStreamTypes.Any) &&
                            !string.IsNullOrEmpty(argument.ConversionProfile.AlternativeConversionString)) &&
                           (location.StreamType != argument.ConversionProfile.PreferedType);
                    str3 = !flag
                               ? argument.ConversionProfile.ConversionStringTemplate
                               : argument.ConversionProfile.AlternativeConversionString;
                    uRL = location.URL;
                    fileNameWithoutExtension = TextUtil.FormatFileName(str4);
                    result.Title             = str4;
                }
                else
                {
                    fileNameWithoutExtension = Path.GetFileNameWithoutExtension(argument.OriginalFileLocation);
                    uRL             = argument.OriginalFileLocation;
                    result.Title    = fileNameWithoutExtension;
                    result.FileSize = new FileInfo(argument.OriginalFileLocation).Length;
                    str3            = !string.IsNullOrEmpty(argument.ConversionProfile.ConversionStringTemplate)
                               ? argument.ConversionProfile.ConversionStringTemplate
                               : argument.ConversionProfile.AlternativeConversionString;
                }
                backgroundWorker.ReportProgress(0, "***" + fileNameWithoutExtension);
                string str5 = string.Format("{0}.{1}", fileNameWithoutExtension,
                                            argument.ConversionProfile.OutputExtension);
                string str6          = DateTime.Now.Ticks.ToString();
                string targetTmpFile = Path.Combine(ApplicationSettings.Instance.DefaultDownloadFolder, str6);
                string targetFile    = Path.Combine(ApplicationSettings.Instance.DefaultDownloadFolder, str5);
                if (File.Exists(targetFile))
                {
                    backgroundWorker.ReportProgress(0, "Exist file:" + fileNameWithoutExtension);
                    return;
                }
                long resultSize = 0L;
                result.ResultPath = targetFile;
                if (((argument.ConversionProfile.ConversionStringTemplate != null) || flag) &&
                    !argument.IndirectConversion)
                {
                    try
                    {
                        e.Cancel = DownloadHelper.DownloadAndConvert(backgroundWorker, str3, uRL, targetFile,
                                                                     targetTmpFile, out resultSize);
                    }
                    catch (Exception exception)
                    {
                        resultSize             = 0L;
                        result.ResultException = exception;
                    }
                }
                else
                {
                    e.Cancel = DownloadHelper.InternalDownload(backgroundWorker, str3, uRL, targetFile, targetTmpFile,
                                                               out resultSize);
                }
                result.FileSize = resultSize;
                e.Result        = result;
                try
                {
                    DownloadSubtitle(uRL, fileNameWithoutExtension);
                }
                catch (Exception ex)
                {
                    //todo
                }
            }
        }