{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install pydub\nfrom pydub import AudioSegment\nimport os\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Variables to Trim Audio (Optional)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"startMin = 0\nstartSec = 0\n\nendMin = 0\nendSec = 10\n\n# Time to miliseconds\nstartTime = startMin*60*1000+startSec*1000\nendTime = endMin*60*1000+endSec*1000\n\nstartTime = startMin*60*1000+startSec*1000\nendTime = endMin*60*1000+endSec*1000","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Train Audio Path and Mp3 Path","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"audio_path = '../input/birdsong-recognition/train_audio/'\nwav_path = 'train_wav/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Save Names of Train Labels from folders","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"subFolderList = []\nfor x in os.listdir(audio_path):\n    if os.path.isdir(audio_path + '/' + x):\n        subFolderList.append(x)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Make Wav Folder to Export","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"if not os.path.exists(wav_path):\n    os.makedirs(wav_path)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Make Subfolders within Wav Folder","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"subFolderList = []\nfor x in os.listdir(audio_path):\n    if os.path.isdir(audio_path + '/' + x):\n        subFolderList.append(x)\n        if not os.path.exists(wav_path + '/' + x):\n            os.makedirs(wav_path +'/'+ x)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Count Items","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"sample_audio = []\ntotal = 0\nfor x in subFolderList:\n    all_files = [y for y in os.listdir(audio_path + x) if '.mp3' in y]\n    total += len(all_files)\n    sample_audio.append(audio_path  + x + '/'+ all_files[0])\n    \n    print('count: %d : %s' % (len(all_files), x ))\nprint(total)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mpeg2wav(mpeg_path, targetdir=''):\n    # Read Mp3\n    sound = AudioSegment.from_mp3(mpeg_path)\n    # Trim (10 secs)\n    sound = sound[startTime:endTime]\n    # Save Wav\n    sound.export(f\"{targetdir}.wav\", format=\"wav\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, label in tqdm(enumerate(subFolderList)):\n    #print(i,\":\",label)\n    all_files = [y for y in os.listdir(audio_path + label) if '.mp3' in y]\n    for file in all_files[:2]:\n        mpeg2wav(audio_path + label + '/' + file, wav_path + label + \"/\" + file.replace(\".mp3\",\"\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}