private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Log("\n\n## End ##"); if (SaveToLog) { LogSw.Close(); } if (DownloadingVideo) { VideoProcess.Kill(); } else if (DownloadingPlaylist) { PlaylistProcess.Kill(); } /*if (VideoProcess.Id != 0 && !VideoProcess.HasExited) * VideoProcess.Kill(); * else if (PlaylistProcess.Id != 0 && !PlaylistProcess.HasExited) * PlaylistProcess.Kill();*/ }
private void ExportFromPlaylist_Click(object sender, EventArgs e) { if (ExportTXTSaveFileDialog.ShowDialog() != DialogResult.OK) { return; } try { Log("\nExporting txt from playlist/channel to: " + ExportTXTSaveFileDialog.FileName); Cursor.Current = Cursors.WaitCursor; DownloadProgressBar.Maximum = 2; InitializeProcess(PlaylistProcess); PlaylistProcess.StartInfo.Arguments = PlaylistTextBox.Text + " -i --get-id"; DownloadingPlaylist = PlaylistProcess.Start(); List <string> ExportResult = new List <string>(); StreamReader sr = PlaylistProcess.StandardOutput; DownloadingPlaylist = true; int Videos = 0; int ExportedVideos = 0; DownloadProgressBar.Value = 1; for (int i = 0; !PlaylistProcess.HasExited; i++) { string dLine = sr.ReadLine(); if (dLine == null) { continue; } if (dLine.StartsWith("WARNING")) { continue; } Videos++; if (dLine.StartsWith("ERROR")) { continue; } ExportResult.Add("https://youtube.com/watch?v=" + dLine); ExportedVideos++; } sr.Close(); DownloadingPlaylist = false; ; File.WriteAllLines(ExportTXTSaveFileDialog.FileName, ExportResult.ToArray()); DownloadProgressBar.Value = 2; Log("\nExported videos successfully: " + ExportedVideos + "/" + Videos); MessageBox.Show("Vídeos exportados con éxito: " + ExportedVideos + "/" + Videos, "Resultado de Exportar a txt", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { Log("\nError during export txt from playlist/channel: " + ex.Message); MessageBox.Show("Error:\n\n" + ex.ToString(), "Error exportando a txt", MessageBoxButtons.OK, MessageBoxIcon.Error); } Cursor.Current = Cursors.Default; DownloadProgressBar.Value = 0; }
private void DownloadPlaylist_Click(object sender, EventArgs e) { if (!CheckLibs(false)) { return; } Cursor.Current = Cursors.WaitCursor; DownloadPlaylist.Enabled = false; DownloadPlaylist.Text = "Descargando lista de reproducción/canal..."; DownloadPlaylist.Update(); Log("\n\nDownloading playlist/channel from YouTube"); InitializeProcess(PlaylistProcess); PlaylistProcess.StartInfo.Arguments = PlaylistTextBox.Text + " -i -f mp4" + ((MP3CheckBox.Checked) ? " -x --audio-format mp3": "") + ((MetadataCheckBox.Checked) ? " --add-metadata" : ""); DownloadingPlaylist = PlaylistProcess.Start(); StreamReader sr = PlaylistProcess.StandardOutput; string Info = ""; PageTypes PageType = PageTypes.Unknown; string PageName = ""; int PageVideos = 0; int DownloadedVideos = 0; int DownloadingVideo = 0; bool OpenDirAtEnd = OpenAtEndCheckBox.Checked; try { string FileToRename = "", FileNewName = ""; for (int i = 0; !PlaylistProcess.HasExited; i++) { string dLine = sr.ReadLine(); if (dLine == null) { continue; } switch (i) { case 0: if (PageType != PageTypes.Unknown) { continue; } if (dLine.StartsWith("[youtube:playlist]")) { PageType = PageTypes.Playlist; DownloadPlaylist.Text = "Descargando lista de reproducción..."; DownloadPlaylist.Update(); } else if (dLine.StartsWith("[youtube:channel]")) { PageType = PageTypes.Channel; i -= 1; DownloadPlaylist.Text = "Descargando canal..."; DownloadPlaylist.Update(); } else { throw new InvalidDataException("La URL especificada no corresponde a ninguna lista de reproducción/canal"); } continue; case 1: PageName = dLine.Remove(0, (PageType == PageTypes.Playlist) ? 33 : 46); PlaylistNameTextBox.Text = ((PageType == PageTypes.Playlist) ? "Lista de reproducción: " : "Canal: ") + PageName; PlaylistNameTextBox.Update(); continue; case 2: PageVideos = int.Parse(dLine.Remove(0, 28 + ((PageType == PageTypes.Playlist) ? 0 : 13) + PageName.Length + 14).Split(' ')[0]); DownloadedVideos = PageVideos; DownloadProgressBar.Maximum = PageVideos; ProgressText.Text = "1/" + PageVideos; //string PageVideosText = dLine.Remove(0, 28 + ((PageType == PageTypes.Playlist) ? 0 : 13) + PageName.Length + 14); continue; default: if (dLine.StartsWith("[download] Downloading video ") || dLine.StartsWith("[download] Finished downloading")) { if (FileToRename != "") { try { string DirectoryToMove = SaveTextBox.Text + ((DownloadInSubfolderCheckBox.Checked) ? PageName + "\\" : ""); if (!Directory.Exists(DirectoryToMove)) { Directory.CreateDirectory(DirectoryToMove); } if (MP3CheckBox.Checked) { FileToRename = FileToRename.Remove(FileToRename.Length - 1, 1) + "3"; } File.Move(SaveTextBox.Text + FileToRename, DirectoryToMove + FileNewName + ((MP3CheckBox.Checked) ? ".mp3" : ".mp4")); } catch (IOException ioex) { Log("\n[ERROR] Error trying to rename \"" + SaveTextBox.Text + FileToRename + "\" to \"" + SaveTextBox.Text + FileNewName + "\"\n" + ioex.ToString() + "\n"); Info += "\n ERROR AL MOVER/RENOMBRAR:\n " + ioex.ToString() + "\n Archivo guardado en: " + SaveTextBox.Text + FileToRename; } } if (dLine.StartsWith("[download] Finished downloading")) { continue; } DownloadingVideo = int.Parse(dLine.Remove(0, 29).Split(' ')[0]); Info += "\n\n" + DownloadingVideo + "/" + PageVideos + ": "; ProgressText.Text = DownloadingVideo + "/" + PageVideos; ProgressText.Update(); DownloadProgressBar.Value = DownloadingVideo; } if (dLine == "ERROR: This video is unavailable.") { DownloadedVideos--; Log("\n[ERROR] Video " + DownloadingVideo + "/" + PageVideos + " was not found"); Info += "VíDEO NO DISPONIBLE\n"; } if (dLine.StartsWith("[download] Destination: ")) { FileToRename = dLine.Remove(0, 24); FileNewName = FileToRename.Remove(FileToRename.Length - 16, 16); string TempURL = FileToRename.Remove(0, FileNewName.Length + 1).Remove(11, 4); Info += FileNewName + "\n URL: https://www.youtube.com/watch?v=" + TempURL; } if (dLine.EndsWith(" has already been downloaded")) { FileToRename = dLine.Remove(0, 11); FileToRename = FileToRename.Remove(FileToRename.Length - 28, 28); FileNewName = FileToRename.Remove(FileToRename.Length - 16, 16); string TempURL = FileToRename.Remove(0, FileNewName.Length + 1).Remove(11, 4); Info += FileNewName + "\n URL: https://www.youtube.com/watch?v=" + TempURL + "\n VÍDEO YA DESCARGADO"; } continue; } } sr.Close(); DownloadingPlaylist = false; Log("\nVídeos downloaded from " + ((MP3CheckBox.Checked) ? "and converted to .mp3 " : "") + "successfully from " + PageType + " # " + PageName + ": " + DownloadedVideos + "/" + PageVideos + "\n"); DownloadResultForm drm = new DownloadResultForm(Results.Info, "Vídeos descargados con éxito: " + DownloadedVideos + "/" + PageVideos, (PageType == PageTypes.Playlist) ? DownloadTypes.Playlist : DownloadTypes.Channel, PageName, MP3CheckBox.Checked, MetadataCheckBox.Checked, Info.Remove(0, 2)); } catch (Exception ex) { sr.Close(); DownloadingPlaylist = false; OpenDirAtEnd = false; Log("\n[ERROR] Error during download playlist/channel process: " + ex.Message); Info += "\n\n" + ex.ToString(); DownloadResultForm drm = new DownloadResultForm(Results.Error, "Error en la descarga de " + PageVideos + " vídeos", (PageType == PageTypes.Playlist) ? DownloadTypes.Playlist : DownloadTypes.Channel, PageName, MP3CheckBox.Checked, MetadataCheckBox.Checked, Info.Remove(0, 2)); } Cursor.Current = Cursors.Default; PlaylistNameTextBox.Text = "Nombre de la lista de reproducción/canal"; DownloadPlaylist.Enabled = true; DownloadPlaylist.Text = "Descargar lista de reproducción/canal"; DownloadProgressBar.Value = 0; ProgressText.Text = "--/--"; if (OpenDirAtEnd) { Process.Start("explorer.exe", OutputPath); } }