Skip to content

Commit

Permalink
Update Youtube_To_Google_Drive.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
akhi07rx committed May 30, 2023
1 parent 0551c81 commit 6cca26a
Showing 1 changed file with 33 additions and 99 deletions.
132 changes: 33 additions & 99 deletions Youtube_To_Google_Drive.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,9 @@
},
"outputs": [],
"source": [
"from tqdm import tqdm\n",
"import requests\n",
"from google.colab import drive\n",
"import os\n",
"from urllib.parse import urlparse, unquote\n",
"import os.path\n",
"import sys\n",
"import pytube\n",
"from textwrap import dedent\n",
"from pytube import YouTube\n",
"\n",
"drive.mount('/content/drive')\n",
"\n",
"# Define the destination folder in Google Drive\n",
"# Define the destination folder\n",
"destination_folder = '/content/drive/MyDrive/DOWNLOADS 02/'\n",
"\n",
"# Create the destination folder if it doesn't exist\n",
Expand All @@ -52,99 +42,43 @@
"# Function to download YouTube video with options\n",
"def download_youtube_video(url, destination_folder):\n",
" try:\n",
" youtube = pytube.YouTube(url)\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",
" youtube = YouTube(url)\n",
"\n",
" # Get the available video streams\n",
" streams = youtube.streams.filter(type='video', progressive=True).order_by('resolution')\n",
"\n",
" # Print available video qualities\n",
" print(\"Available video qualities:\")\n",
" for i, stream in enumerate(streams, start=1):\n",
" print(f\"{i}. Resolution: {stream.resolution}, Format: {stream.mime_type}\")\n",
"\n",
" # Prompt the user to select the video quality\n",
" selected_option = None\n",
" while selected_option is None:\n",
" user_input = input(\"Enter the number of the desired video quality: \")\n",
"\n",
" if user_input.isdigit() and int(user_input) in range(1, len(streams) + 1):\n",
" # Retrieve the selected stream\n",
" selected_stream = streams[int(user_input) - 1]\n",
" selected_option = user_input\n",
" else:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" # Download the video\n",
" file_name = f\"{youtube.title} ({selected_stream.resolution})\"\n",
" file_path = os.path.join(destination_folder, file_name)\n",
" selected_stream.download(output_path=destination_folder, filename=file_name)\n",
"\n",
" print(f\"Video '{youtube.title}' downloaded successfully to '{file_path}'.\\n\")\n",
"\n",
" video.download(destination_folder)\n",
" print(f\"Video '{video.title}' downloaded successfully to '{destination_folder}'.\\n\")\n",
" except Exception as e:\n",
" print(f\"An error occurred: {str(e)}\")\n",
"\n",
"try:\n",
" while True:\n",
" try:\n",
" user_input = input(\"\\n\\nEnter the download link (or 'exit' to quit): \")\n",
" except KeyboardInterrupt:\n",
" print(\"\\nKeyboard interrupt detected. Exiting the program.\")\n",
" break\n",
"\n",
" if user_input.lower() == 'exit':\n",
" break\n",
"\n",
" # Check if the input is a YouTube link\n",
" if \"youtube.com\" in user_input or \"youtu.be\" in user_input:\n",
" download_youtube_video(user_input, destination_folder)\n",
" continue\n",
"\n",
" try:\n",
" response = requests.get(user_input, stream=True)\n",
" response.raise_for_status()\n",
" except requests.exceptions.MissingSchema:\n",
" print(\"You have entered a wrong command. Please enter again.\\n\")\n",
" continue\n",
"\n",
" file_size = int(response.headers.get('content-length', 0))\n",
"\n",
" file_size_mb = file_size / (1024 ** 2)\n",
" file_size_gb = file_size / (1024 ** 3)\n",
"\n",
" file_size_str = f\"{file_size_mb:.2f} MB / {file_size_gb:.2f} GB\"\n",
"\n",
" progress_bar = tqdm(total=file_size, unit='B', unit_scale=True, leave=True)\n",
"\n",
" parsed_url = urlparse(user_input)\n",
" file_name = unquote(os.path.basename(parsed_url.path))\n",
"\n",
" file_path = os.path.join(destination_folder, file_name)\n",
"\n",
" with open(file_path, 'wb') as file:\n",
" for chunk in response.iter_content(chunk_size=8192):\n",
" if chunk:\n",
" file.write(chunk)\n",
" progress_bar.update(len(chunk))\n",
" sys.stdout.flush()\n",
"\n",
" progress_bar.close()\n",
"\n",
" print()\n",
" print(f\"File name: {file_name}\")\n",
" print(f\"File size: {file_size_str}\")\n",
" print(f\"File '{file_name}' uploaded successfully to '{destination_folder}'.\\n\")\n",
"# Prompt the user for the YouTube video URL\n",
"youtube_url = input(\"Enter the YouTube video URL: \")\n",
"\n",
"except Exception as e:\n",
" print(f\"An error occurred: {str(e)}\")\n",
" print(\"Exiting the program.\")\n"
"# Download the YouTube video\n",
"download_youtube_video(youtube_url, destination_folder)\n"
]
}
],
Expand Down

0 comments on commit 6cca26a

Please sign in to comment.