Skip to content

Instantly share code, notes, and snippets.

@ngnmtl
Last active June 9, 2024 02:53
Show Gist options
  • Save ngnmtl/d51b9b2e3e1dbe1b3b29c0b8122e1076 to your computer and use it in GitHub Desktop.
Save ngnmtl/d51b9b2e3e1dbe1b3b29c0b8122e1076 to your computer and use it in GitHub Desktop.

Revisions

  1. ngnmtl revised this gist Jun 9, 2024. No changes.
  2. ngnmtl revised this gist Jun 9, 2024. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion youtube-channel-download.ipynb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    {
    "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": [
    @@ -75,7 +85,8 @@
    "metadata": {
    "colab": {
    "provenance": [],
    "name": "Colab Youtube Channel Backup.ipynb"
    "name": "Colab Youtube Channel Backup.ipynb",
    "include_colab_link": true
    },
    "kernelspec": {
    "display_name": "Python 3",
  3. ngnmtl created this gist Jun 9, 2024.
    87 changes: 87 additions & 0 deletions youtube-channel-download.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    {
    "cells": [
    {
    "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"
    },
    "kernelspec": {
    "display_name": "Python 3",
    "name": "python3"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 0
    }