From 0551c8117ccbbf4a34cbe7ef812c152ea16870ec Mon Sep 17 00:00:00 2001 From: akhi07rx <89210430+akhi07rx@users.noreply.github.com> Date: Tue, 30 May 2023 13:19:22 +0530 Subject: [PATCH] updated 1. Imported the `pytube` library to handle YouTube video downloads. 2. Added a new function `download_youtube_video()` to handle YouTube video downloads with quality options. 3. Added a check to detect YouTube links in the user input and invoke the `download_youtube_video()` function if a YouTube link is detected. 4. Modified the file naming logic to append the video quality at the end of the file name, but only for video files (excluding audio files). 5. Updated the error handling to catch exceptions specific to YouTube video downloads. 6. Introduced formatted text box display for the available quality options using the `dedent()` function from the `textwrap` module. --- Youtube_To_Google_Drive.ipynb | 94 +++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/Youtube_To_Google_Drive.ipynb b/Youtube_To_Google_Drive.ipynb index 07bcccd..4f39144 100644 --- a/Youtube_To_Google_Drive.ipynb +++ b/Youtube_To_Google_Drive.ipynb @@ -1,26 +1,11 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "authorship_tag": "ABX9TyNTtIBj43BGvfXJiHDIOgaW", - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { + "attachments": {}, "cell_type": "markdown", "metadata": { - "id": "view-in-github", - "colab_type": "text" + "colab_type": "text", + "id": "view-in-github" }, "source": [ "\"Open" @@ -39,6 +24,11 @@ }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "s6L01xuGIhMn" + }, + "outputs": [], "source": [ "from tqdm import tqdm\n", "import requests\n", @@ -48,6 +38,7 @@ "import os.path\n", "import sys\n", "import pytube\n", + "from textwrap import dedent\n", "\n", "drive.mount('/content/drive')\n", "\n", @@ -58,11 +49,43 @@ "if not os.path.exists(destination_folder):\n", " os.makedirs(destination_folder)\n", "\n", - "# Function to download YouTube video\n", + "# Function to download YouTube video with options\n", "def download_youtube_video(url, destination_folder):\n", " try:\n", " youtube = pytube.YouTube(url)\n", - " video = youtube.streams.first()\n", + "\n", + " # Collect available quality options\n", + " quality_options = []\n", + " for i, stream in enumerate(youtube.streams, start=1):\n", + " quality_options.append((i, stream.resolution, stream.mime_type))\n", + "\n", + " # Format quality options in a text box\n", + " quality_options_text = dedent('''\n", + " Available quality options:\n", + " -----------------------------------\n", + " | Option | Resolution | Format |\n", + " -----------------------------------''')\n", + " for option in quality_options:\n", + " option_text = f\"| {option[0]} | {option[1]} | {option[2]} |\\n\"\n", + " quality_options_text += option_text\n", + " quality_options_text += '-----------------------------------\\n'\n", + "\n", + " # Display quality options\n", + " print(quality_options_text)\n", + "\n", + " # Prompt the user to select the quality option\n", + " selected_option = input(\"Enter the number of the desired quality option (or 'max' for maximum quality): \")\n", + " if selected_option.lower() == 'max':\n", + " video = youtube.streams.get_highest_resolution()\n", + " else:\n", + " selected_option = int(selected_option)\n", + " video = list(youtube.streams)[selected_option - 1]\n", + "\n", + " video_quality = video.resolution if video.resolution else 'Audio'\n", + " file_extension = video.url.split('.')[-1]\n", + " file_name = video.title + f\" ({video_quality})\" + '.' + file_extension\n", + " file_path = os.path.join(destination_folder, file_name)\n", + "\n", " video.download(destination_folder)\n", " print(f\"Video '{video.title}' downloaded successfully to '{destination_folder}'.\\n\")\n", " except Exception as e:\n", @@ -121,15 +144,24 @@ "\n", "except Exception as e:\n", " print(f\"An error occurred: {str(e)}\")\n", - " print(\"Exiting the program.\")\n", - "\n", - "print(\"Exiting the program.\")\n" - ], - "metadata": { - "id": "s6L01xuGIhMn" - }, - "execution_count": null, - "outputs": [] + " print(\"Exiting the program.\")\n" + ] + } + ], + "metadata": { + "colab": { + "authorship_tag": "ABX9TyNTtIBj43BGvfXJiHDIOgaW", + "include_colab_link": true, + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" } - ] -} \ No newline at end of file + }, + "nbformat": 4, + "nbformat_minor": 0 +}