{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ], "metadata": { "id": "TjDXDTJr5P3k" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import os\n", "import requests\n", "import yt_dlp\n", "from concurrent.futures import ThreadPoolExecutor\n", "\n", "channel_url = \"https://www.youtube.com/@channelName/videos\"\n", "output_path = \"/content/drive/MyDrive/Youtube\"\n", "\n", "def download_video(entry, output_path):\n", " video_title = entry['title']\n", " video_folder = os.path.join(output_path, f\"{video_title}\")\n", "\n", " if not os.path.exists(video_folder):\n", " os.makedirs(video_folder)\n", "\n", " desc_file_path = os.path.join(video_folder, 'Description.txt')\n", " with open(desc_file_path, 'w') as desc_file:\n", " desc_file.write(f\"title:\\n {video_title}\\n\\n\\n\")\n", " desc_file.write(f\"Description:\\n {entry.get('description', '')}\\n\\n\\n\")\n", " desc_file.write(f\"tags:\\n {', '.join(entry.get('tags', []))}\\n\\n\\n\")\n", "\n", " thumb_url = entry.get('thumbnail')\n", " if thumb_url:\n", " thumb_file_path = os.path.join(video_folder, 'thumbnail.jpg')\n", " response = requests.get(thumb_url)\n", " with open(thumb_file_path, 'wb') as thumb_file:\n", " thumb_file.write(response.content)\n", "\n", " print(f\"{video_title} download...\")\n", " ydl_opts = {\n", " 'format': 'bestvideo[height<=1080]+bestaudio/best[height<=1080]',\n", " 'merge_output_format': 'mp4',\n", " 'outtmpl': os.path.join(video_folder, f\"{video_title}.%(ext)s\"),\n", " 'writedescription': False,\n", " 'writeinfojson': False,\n", " 'writethumbnail': False,\n", " }\n", " with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n", " ydl.download([entry['webpage_url']])\n", "\n", "def download_videos_from_channel(channel_url, output_path):\n", " ydl_opts = {\n", " 'skip_download': True,\n", " }\n", " with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n", " info_dict = ydl.extract_info(channel_url, download=False)\n", " with ThreadPoolExecutor(max_workers=5) as executor:\n", " executor.map(lambda entry: download_video(entry, output_path), info_dict.get('entries', []))\n", "download_videos_from_channel(channel_url, output_path)\n" ], "metadata": { "id": "zZrAPebH4_sL" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "provenance": [], "name": "Colab Youtube Channel Backup.ipynb", "include_colab_link": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }