Last active
June 9, 2024 02:53
-
-
Save ngnmtl/d51b9b2e3e1dbe1b3b29c0b8122e1076 to your computer and use it in GitHub Desktop.
Colab Youtube Channel Backup.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/ngnmtl/d51b9b2e3e1dbe1b3b29c0b8122e1076/youtube-channel-download.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment