pianistprogrammer commited on
Commit
4b35ac2
·
verified ·
1 Parent(s): 5013762

Delete mp3-to-wav.ipynb

Browse files
Files changed (1) hide show
  1. mp3-to-wav.ipynb +0 -145
mp3-to-wav.ipynb DELETED
@@ -1,145 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "id": "f73b7d7c-9536-46df-a781-df8950f93bfc",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "import os\n",
11
- "from pydub import AudioSegment\n",
12
- "\n",
13
- "def convert_mp3_to_wav_and_clean(root_directory, folders_to_process):\n",
14
- " \"\"\"\n",
15
- " Converts all MP3 files to WAV format within specified folders and then removes the original MP3s.\n",
16
- "\n",
17
- " Args:\n",
18
- " root_directory (str): The parent directory containing the folders (MAG, OWN, FMA).\n",
19
- " folders_to_process (list): A list of folder names (e.g., ['MAG', 'OWN', 'FMA']) to process.\n",
20
- " \"\"\"\n",
21
- " print(f\"Starting MP3 to WAV conversion and cleanup in: {root_directory}\")\n",
22
- "\n",
23
- " # Check if pydub is installed and ffmpeg is accessible\n",
24
- " try:\n",
25
- " # Try to create a dummy AudioSegment to check for ffmpeg\n",
26
- " # This will raise an error if ffmpeg is not found or not configured\n",
27
- " _ = AudioSegment.silent(duration=1)\n",
28
- " print(\"pydub and ffmpeg seem to be correctly set up.\")\n",
29
- " except Exception as e:\n",
30
- " print(\"\\nERROR: pydub or ffmpeg not found or configured correctly!\")\n",
31
- " print(\"Please ensure you have pydub installed (`pip install pydub`)\")\n",
32
- " print(\"and ffmpeg installed and available in your system's PATH.\")\n",
33
- " print(f\"Details: {e}\")\n",
34
- " return\n",
35
- "\n",
36
- " if not os.path.isdir(root_directory):\n",
37
- " print(f\"Error: The specified root directory '{root_directory}' does not exist or is not a directory.\")\n",
38
- " return\n",
39
- "\n",
40
- " for folder_name in folders_to_process:\n",
41
- " folder_path = os.path.join(root_directory, folder_name)\n",
42
- "\n",
43
- " if not os.path.isdir(folder_path):\n",
44
- " print(f\"Warning: Folder '{folder_path}' not found. Skipping.\")\n",
45
- " continue\n",
46
- "\n",
47
- " print(f\"\\nProcessing folder: {folder_path}\")\n",
48
- " mp3_files_found = 0\n",
49
- " converted_count = 0\n",
50
- " deleted_count = 0\n",
51
- "\n",
52
- " # List all files in the current folder\n",
53
- " for file_name in os.listdir(folder_path):\n",
54
- " if file_name.endswith(\".mp3\"):\n",
55
- " mp3_files_found += 1\n",
56
- " mp3_file_path = os.path.join(folder_path, file_name)\n",
57
- " wav_file_name = os.path.splitext(file_name)[0] + \".wav\"\n",
58
- " wav_file_path = os.path.join(folder_path, wav_file_name)\n",
59
- "\n",
60
- " print(f\" Attempting to convert: {file_name}\")\n",
61
- "\n",
62
- " try:\n",
63
- " # Load the MP3 file\n",
64
- " audio = AudioSegment.from_mp3(mp3_file_path)\n",
65
- " # Export as WAV\n",
66
- " audio.export(wav_file_path, format=\"wav\")\n",
67
- " converted_count += 1\n",
68
- " print(f\" Converted to WAV: {wav_file_name}\")\n",
69
- "\n",
70
- " # Remove the original MP3 file\n",
71
- " os.remove(mp3_file_path)\n",
72
- " deleted_count += 1\n",
73
- " print(f\" Deleted original MP3: {file_name}\")\n",
74
- "\n",
75
- " except Exception as e:\n",
76
- " print(f\" Error processing '{file_name}': {e}\")\n",
77
- " \n",
78
- " if mp3_files_found == 0:\n",
79
- " print(f\" No MP3 files found in '{folder_path}'.\")\n",
80
- " else:\n",
81
- " print(f\" Finished processing '{folder_name}': {converted_count} converted, {deleted_count} original MP3s deleted.\")\n",
82
- "\n",
83
- " print(\"\\nScript execution finished.\")\n",
84
- "\n",
85
- "\n",
86
- "# --- How to use this script ---\n",
87
- "# 1. Save this code as a Python file (e.g., convert_audio.py).\n",
88
- "# 2. **Install pydub:** If you haven't already, open your terminal/command prompt and run:\n",
89
- "# `pip install pydub`\n",
90
- "# 3. **Install ffmpeg:** pydub requires ffmpeg to be installed and accessible in your system's PATH.\n",
91
- "# * For Windows: Download from https://ffmpeg.org/download.html and add to PATH.\n",
92
- "# * For macOS: `brew install ffmpeg` (if you have Homebrew)\n",
93
- "# * For Linux: `sudo apt-get install ffmpeg` (Debian/Ubuntu) or `sudo dnf install ffmpeg` (Fedora)\n",
94
- "# 4. **Replace 'YOUR_PARENT_DIRECTORY_PATH_HERE'** with the actual path to the directory\n",
95
- "# that contains your GTZAN, MAG, OWN, and FMA folders.\n",
96
- "# Example: If your GTZAN, MAG, OWN, FMA are all directly under `/Users/YourName/AudioData/`,\n",
97
- "# then set `parent_data_path = '/Users/YourName/AudioData/'`\n",
98
- "# 5. Uncomment the last line to run the function.\n",
99
- "# 6. Run the script from your terminal: `python convert_audio.py`\n",
100
- "\n",
101
- "# !!! IMPORTANT: Before running, it is highly recommended to back up your data\n",
102
- "# !!! as this script will delete original MP3 files after conversion.\n",
103
- "\n",
104
- "# Set the parent directory path here (where GTZAN, MAG, OWN, FMA are located)\n",
105
- "parent_data_path = '.'\n",
106
- "\n",
107
- "# Define the folders to process\n",
108
- "folders_to_convert = ['MAG', 'OWN', 'FMA'] # Add or remove folders as needed\n",
109
- "\n",
110
- "# Call the function to perform the conversion and cleanup\n",
111
- "# Uncomment the line below to execute the script:\n",
112
- "convert_mp3_to_wav_and_clean(parent_data_path, folders_to_convert)\n"
113
- ]
114
- },
115
- {
116
- "cell_type": "code",
117
- "execution_count": null,
118
- "id": "ef64a560-034c-4277-b00b-3ccb730264e4",
119
- "metadata": {},
120
- "outputs": [],
121
- "source": []
122
- }
123
- ],
124
- "metadata": {
125
- "kernelspec": {
126
- "display_name": "Python 3 (ipykernel)",
127
- "language": "python",
128
- "name": "python3"
129
- },
130
- "language_info": {
131
- "codemirror_mode": {
132
- "name": "ipython",
133
- "version": 3
134
- },
135
- "file_extension": ".py",
136
- "mimetype": "text/x-python",
137
- "name": "python",
138
- "nbconvert_exporter": "python",
139
- "pygments_lexer": "ipython3",
140
- "version": "3.11.9"
141
- }
142
- },
143
- "nbformat": 4,
144
- "nbformat_minor": 5
145
- }