{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ryanwebster90/colab-yt-dl/blob/main/dl_yt_playlist.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "1) Youtube playlist link extractor : https://technmind.com/youtube-playlist-link-extractor/\n",
        "\n",
        "2) Extract Links from text : https://www.browserling.com/tools/extract-urls"
      ],
      "metadata": {
        "id": "vkCFp3XVxq48"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#Initialize\n",
        "import os\n",
        "def my_mkdirs(folder):\n",
        "  if os.path.exists(folder)==False:\n",
        "    os.makedirs(folder)\n",
        "my_mkdirs('/content/tmp/')\n",
        "\n",
        "#Reinstall youtube_dl because the version on Colab is outdated\n",
        "!python3 -m pip install --force-reinstall https://github.com/yt-dlp/yt-dlp/archive/master.tar.gz\n",
        "import yt_dlp as youtube_dl\n",
        "\n",
        "#Mount Google Drive\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "metadata": {
        "id": "1YSLwB9ex9HR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Enter the \"share\" links to the youtube videos here\n",
        "urls = ['https://www.youtube.com/embed/....',\n",
        "'https://www.youtube.com/embed/....']"
      ],
      "metadata": {
        "id": "YHqAThDCRj4C"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W7bW6g_AwxfX"
      },
      "source": [
        "#Download all the youtube videos as .M4A files\n",
        "%cd /content/tmp\n",
        "for ind,url in enumerate(urls):\n",
        "  !yt-dlp $url -f 'bestaudio[ext=m4a]' -o '%(title)s.m4a'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Convert all M4A files to MP3 (NOTE: Make sure to rename M4A files that contain parenthesis () in their name first!) \n",
        "output_folder = '/content/AudiosP1/'\n",
        "my_mkdirs(output_folder)\n",
        "import glob\n",
        "files = glob.glob('/content/tmp/*')\n",
        "for file in files:\n",
        "  out_file = f'{output_folder}{file[13:-3]}mp3'\n",
        "  file = file.replace(' ','\\ ')\n",
        "  out_file = out_file.replace(' ','\\ ')\n",
        "  !ffmpeg -i $file -vn -ab 384k -ar 44100 -y $out_file\n"
      ],
      "metadata": {
        "id": "48htqMULSrBz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Zip the folder and store it on Google Drive\n",
        "%cd /content/\n",
        "!zip -r /content/drive/MyDrive/AudiosP1.zip /content/AudiosP1"
      ],
      "metadata": {
        "id": "BoNaY4v6S2UA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Delete the folder (as a safeguard, this will not work unless you manually change the name of the folder first)\n",
        "%cd /content/\n",
        "import shutil\n",
        "shutil.rmtree(\"/content/xxx-AudiosP1\")"
      ],
      "metadata": {
        "id": "agjq4INSU0x2"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}