Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
akhi07rx committed May 30, 2023
1 parent 03ee142 commit 0551c81
Showing 1 changed file with 63 additions and 31 deletions.
94 changes: 63 additions & 31 deletions Youtube_To_Google_Drive.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<a href=\"https://colab.research.google.com/github/akhi07rx/Youtube-To-Google-Drive/blob/main/Youtube_To_Google_Drive.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
Expand All @@ -39,6 +24,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "s6L01xuGIhMn"
},
"outputs": [],
"source": [
"from tqdm import tqdm\n",
"import requests\n",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 0551c81

Please sign in to comment.