File size: 3,861 Bytes
eb916c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e5518
eb916c3
 
 
 
 
 
 
 
 
 
05e5518
eb916c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{
  "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": []
    }
  ]
}