{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"In my previous [notebook](https://www.kaggle.com/code/mbmmurad/mp3-to-wav-conversion), we tried to convert mp3 audio files to wav files. But it took almost 25 minutes to convert the Validation files. In this notebook we'll try a different way to convert the mp3 files to wav files using joblib. This notebook takes only 8-9 minutes to process the validation files. \n(3times faster)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:19:04.757196Z","iopub.execute_input":"2022-07-02T16:19:04.757537Z","iopub.status.idle":"2022-07-02T16:19:04.764002Z","shell.execute_reply.started":"2022-07-02T16:19:04.757508Z","shell.execute_reply":"2022-07-02T16:19:04.762854Z"}}},{"cell_type":"markdown","source":"**Reference** :\n\nIdea is from this notebook : https://www.kaggle.com/code/kingabzpro/asr-mp3-to-wav-dataset?fbclid=IwAR3D61p3PW9lY-IGTOLrjf52b1_SQkc2FFuHl9kEbGumVlgHLqVOGJoQl5w","metadata":{}},{"cell_type":"markdown","source":"# Importing Necessary Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport shutil\n\nfrom pydub import AudioSegment\nfrom joblib import Parallel, delayed","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_PATH = \"../input/dlsprint/validation_files\"\nOUTPUT_DIR = \"./validation_files_wav\"","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:20:49.63947Z","iopub.execute_input":"2022-07-02T16:20:49.640189Z","iopub.status.idle":"2022-07-02T16:20:49.644883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(OUTPUT_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:20:55.960762Z","iopub.execute_input":"2022-07-02T16:20:55.9616Z","iopub.status.idle":"2022-07-02T16:20:56.055688Z","shell.execute_reply.started":"2022-07-02T16:20:55.961553Z","shell.execute_reply":"2022-07-02T16:20:56.053743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_fn(filename):\n    \n    path = f\"{ROOT_PATH}/{filename}\"\n    save_path = f\"{OUTPUT_DIR}\"\n    if not os.path.exists(save_path):\n        os.makedirs(save_path, exist_ok=True)\n    \n    if os.path.exists(path):\n        try:\n            sound = AudioSegment.from_mp3(path)\n            sound = sound.set_frame_rate(16000)\n            sound.export(f\"{save_path}/{filename[:-4]}.wav\", format=\"wav\")\n        except:\n            print(path)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:21:23.496276Z","iopub.execute_input":"2022-07-02T16:21:23.497359Z","iopub.status.idle":"2022-07-02T16:21:23.505196Z","shell.execute_reply.started":"2022-07-02T16:21:23.497311Z","shell.execute_reply":"2022-07-02T16:21:23.504152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = ROOT_PATH\naudio_files = os.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:21:36.575215Z","iopub.execute_input":"2022-07-02T16:21:36.575945Z","iopub.status.idle":"2022-07-02T16:21:38.429802Z","shell.execute_reply.started":"2022-07-02T16:21:36.575909Z","shell.execute_reply":"2022-07-02T16:21:38.428828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\n\nParallel(n_jobs=8, backend=\"multiprocessing\")(\n    delayed(save_fn)(filename) for filename in tqdm(audio_files)\n)\n\nend = time.time()\nprint(\"total time to process: {x} seconds\".format(x=end-start))","metadata":{"execution":{"iopub.status.busy":"2022-07-02T16:21:40.601262Z","iopub.execute_input":"2022-07-02T16:21:40.601671Z"},"trusted":true},"execution_count":null,"outputs":[]}]}