File size: 8,518 Bytes
9f76952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install yt-dlp pydub ffmpeg-python\n",
        "!apt-get install ffmpeg -y  # For Colab, to make pydub work"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QURofI_GiSTK",
        "outputId": "7c46dcd6-d49c-4172-bf27-d9d408352cdf"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting yt-dlp\n",
            "  Downloading yt_dlp-2025.12.8-py3-none-any.whl.metadata (180 kB)\n",
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/180.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m180.3/180.3 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: pydub in /usr/local/lib/python3.12/dist-packages (0.25.1)\n",
            "Collecting ffmpeg-python\n",
            "  Downloading ffmpeg_python-0.2.0-py3-none-any.whl.metadata (1.7 kB)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.12/dist-packages (from ffmpeg-python) (1.0.0)\n",
            "Downloading yt_dlp-2025.12.8-py3-none-any.whl (3.3 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.3/3.3 MB\u001b[0m \u001b[31m55.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading ffmpeg_python-0.2.0-py3-none-any.whl (25 kB)\n",
            "Installing collected packages: yt-dlp, ffmpeg-python\n",
            "Successfully installed ffmpeg-python-0.2.0 yt-dlp-2025.12.8\n",
            "Reading package lists... Done\n",
            "Building dependency tree... Done\n",
            "Reading state information... Done\n",
            "ffmpeg is already the newest version (7:4.4.2-0ubuntu0.22.04.1).\n",
            "0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "from pydub import AudioSegment\n",
        "import yt_dlp\n",
        "\n",
        "# -------------------------------\n",
        "# 1️⃣ YouTube video URL\n",
        "# -------------------------------\n",
        "url = \"https://youtu.be/uvOF0qn_r_0?si=-Zd4-p22-bjgEAWT\"  # Replace VIDEO_ID with your YouTube link\n",
        "\n",
        "# -------------------------------\n",
        "# 2️⃣ Download audio using yt-dlp\n",
        "# -------------------------------\n",
        "ydl_opts = {\n",
        "    'format': 'bestaudio/best',\n",
        "    'outtmpl': 'video_audio.%(ext)s',\n",
        "    'postprocessors': [{\n",
        "        'key': 'FFmpegExtractAudio',\n",
        "        'preferredcodec': 'mp3',  # download as mp3 first\n",
        "        'preferredquality': '192',\n",
        "    }],\n",
        "}\n",
        "\n",
        "print(\"Downloading audio from YouTube...\")\n",
        "with yt_dlp.YoutubeDL(ydl_opts) as ydl:\n",
        "    ydl.download([url])\n",
        "\n",
        "# Find downloaded file\n",
        "for file in os.listdir():\n",
        "    if file.startswith(\"video_audio\") and file.endswith(\".mp3\"):\n",
        "        audio_file = file\n",
        "        break\n",
        "\n",
        "print(\"Downloaded:\", audio_file)\n",
        "\n",
        "# -------------------------------\n",
        "# 3️⃣ Convert audio to WAV (16kHz, mono)\n",
        "# -------------------------------\n",
        "audio = AudioSegment.from_file(audio_file)\n",
        "audio = audio.set_channels(1)      # mono\n",
        "audio = audio.set_frame_rate(16000) # 16 kHz\n",
        "wav_filename = \"znmd.wav\"\n",
        "audio.export(wav_filename, format=\"wav\")\n",
        "print(\"Converted to WAV:\", wav_filename)\n",
        "\n",
        "# -------------------------------\n",
        "# 4️⃣ Split WAV into 1-minute chunks\n",
        "# -------------------------------\n",
        "chunk_length_ms = 60 * 1000  # 1 minute\n",
        "for i, start in enumerate(range(0, len(audio), chunk_length_ms)):\n",
        "    chunk = audio[start:start+chunk_length_ms]\n",
        "    chunk_filename = f\"ZNMD_chunk_{i}.wav\"\n",
        "    chunk.export(chunk_filename, format=\"wav\")\n",
        "    print(\"Saved chunk:\", chunk_filename)\n",
        "\n",
        "print(\"All steps completed! Your audio is ready for diarization.\")\n"
      ],
      "metadata": {
        "id": "oPEdKqgLLUEw",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "0c940970-dd15-40f4-9bfb-9fb18120d879"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Downloading audio from YouTube...\n",
            "[youtube] Extracting URL: https://youtu.be/uvOF0qn_r_0?si=-Zd4-p22-bjgEAWT\n",
            "[youtube] uvOF0qn_r_0: Downloading webpage\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "WARNING: [youtube] No supported JavaScript runtime could be found. Only deno is enabled by default; to use another runtime add  --js-runtimes RUNTIME[:PATH]  to your command/config. YouTube extraction without a JS runtime has been deprecated, and some formats may be missing. See  https://github.com/yt-dlp/yt-dlp/wiki/EJS  for details on installing one\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[youtube] uvOF0qn_r_0: Downloading android sdkless player API JSON\n",
            "[youtube] uvOF0qn_r_0: Downloading web safari player API JSON\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "WARNING: [youtube] uvOF0qn_r_0: Some web_safari client https formats have been skipped as they are missing a url. YouTube is forcing SABR streaming for this client. See  https://github.com/yt-dlp/yt-dlp/issues/12482  for more details\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[youtube] uvOF0qn_r_0: Downloading m3u8 information\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "WARNING: [youtube] uvOF0qn_r_0: Some web client https formats have been skipped as they are missing a url. YouTube is forcing SABR streaming for this client. See  https://github.com/yt-dlp/yt-dlp/issues/12482  for more details\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[info] uvOF0qn_r_0: Downloading 1 format(s): 251\n",
            "[download] Destination: video_audio.webm\n",
            "[download] 100% of    3.73MiB in 00:00:00 at 13.24MiB/s  \n",
            "[ExtractAudio] Destination: video_audio.mp3\n",
            "Deleting original file video_audio.webm (pass -k to keep)\n",
            "Downloaded: video_audio.mp3\n",
            "Converted to WAV: znmd.wav\n",
            "Saved chunk: ZNMD_chunk_0.wav\n",
            "Saved chunk: ZNMD_chunk_1.wav\n",
            "Saved chunk: ZNMD_chunk_2.wav\n",
            "Saved chunk: ZNMD_chunk_3.wav\n",
            "Saved chunk: ZNMD_chunk_4.wav\n",
            "Saved chunk: ZNMD_chunk_5.wav\n",
            "All steps completed! Your audio is ready for diarization.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "UXUHD4sTIjhe"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}