|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on Thu Jun 10 15:23:55 2021 |
| 5 | +
|
| 6 | +@author: jsingh |
| 7 | +""" |
| 8 | +# Imports |
| 9 | + |
| 10 | +import os |
| 11 | +import glob |
| 12 | + |
| 13 | +from pydub import AudioSegment |
| 14 | + |
| 15 | + |
| 16 | +# Functions |
| 17 | + |
| 18 | +def convert_from_m4a(file_path): |
| 19 | + """ |
| 20 | + Converts m4a audio into wav format. |
| 21 | + """ |
| 22 | + try: |
| 23 | + track = AudioSegment.from_file(file_path, "m4a") |
| 24 | + file_handle = track.export( |
| 25 | + file_path.replace(".m4a", ".wav"), format='wav' |
| 26 | + ) |
| 27 | + except FileNotFoundError: |
| 28 | + print("{} does not appear to be valid. Please check.") |
| 29 | + except Exception as e: |
| 30 | + print(e) |
| 31 | + |
| 32 | + |
| 33 | +def convert_from_mp3(file_path): |
| 34 | + """ |
| 35 | + Converts mp3 audio into wav format. |
| 36 | + """ |
| 37 | + try: |
| 38 | + track = AudioSegment.from_file(file_path, "mp3") |
| 39 | + file_handle = track.export( |
| 40 | + file_path.replace(".mp3", ".wav"), format='wav' |
| 41 | + ) |
| 42 | + except FileNotFoundError: |
| 43 | + print("{} does not appear to be valid. Please check.") |
| 44 | + except Exception as e: |
| 45 | + print(e) |
| 46 | + |
| 47 | + |
| 48 | +def convert_from_mp4(file_path): |
| 49 | + """ |
| 50 | + Converts mp4 audio into wav format. |
| 51 | + """ |
| 52 | + try: |
| 53 | + track = AudioSegment.from_file(file_path, "mp4") |
| 54 | + file_handle = track.export( |
| 55 | + file_path.replace(".mp4", ".wav"), format='wav' |
| 56 | + ) |
| 57 | + except FileNotFoundError: |
| 58 | + print("{} does not appear to be valid. Please check.") |
| 59 | + except Exception as e: |
| 60 | + print(e) |
| 61 | + |
| 62 | + |
| 63 | +def convert_from_aac(file_path): |
| 64 | + """ |
| 65 | + Converts aac audio into wav format. |
| 66 | + """ |
| 67 | + try: |
| 68 | + track = AudioSegment.from_file(file_path, "aac") |
| 69 | + file_handle = track.export( |
| 70 | + file_path.replace(".aac", ".wav"), format='wav' |
| 71 | + ) |
| 72 | + except FileNotFoundError: |
| 73 | + print("{} does not appear to be valid. Please check.") |
| 74 | + except Exception as e: |
| 75 | + print(e) |
| 76 | + |
| 77 | + |
| 78 | +def convert_files_to_wav(dir_path, audio_format="m4a"): |
| 79 | + """ |
| 80 | + Converts all the audio files in the input directory path |
| 81 | + with the extension specified by audio_format input |
| 82 | + into .wav audio files, and saves them in the same directory. |
| 83 | + """ |
| 84 | + # Read contents of dir |
| 85 | + # Only select files with the mentioned extension |
| 86 | + files = glob.glob(os.path.join(dir_path, "*." + audio_format)) |
| 87 | + |
| 88 | + # Convert to wav |
| 89 | + # The wav files save in the same dir as specified by dir_path |
| 90 | + if audio_format == "m4a": |
| 91 | + cntr = 0 |
| 92 | + for aud_file in files: |
| 93 | + convert_from_m4a(aud_file) |
| 94 | + cntr += 1 |
| 95 | + print( |
| 96 | + "{} {} files converted to .wav and saved in {}".format( |
| 97 | + cntr, audio_format, dir_path |
| 98 | + ) |
| 99 | + ) |
| 100 | + elif audio_format == "mp3": |
| 101 | + cntr = 0 |
| 102 | + for aud_file in files: |
| 103 | + convert_from_mp3(aud_file) |
| 104 | + cntr += 1 |
| 105 | + print( |
| 106 | + "{} {} files converted to .wav and saved in {}".format( |
| 107 | + cntr, audio_format, dir_path |
| 108 | + ) |
| 109 | + ) |
| 110 | + elif audio_format == "aac": |
| 111 | + cntr = 0 |
| 112 | + for aud_file in files: |
| 113 | + convert_from_aac(aud_file) |
| 114 | + cntr += 1 |
| 115 | + print( |
| 116 | + "{} {} files converted to .wav and saved in {}".format( |
| 117 | + cntr, audio_format, dir_path |
| 118 | + ) |
| 119 | + ) |
| 120 | + elif audio_format == "mp4": |
| 121 | + cntr = 0 |
| 122 | + for aud_file in files: |
| 123 | + convert_from_mp4(aud_file) |
| 124 | + cntr += 1 |
| 125 | + print( |
| 126 | + "{} {} files converted to .wav and saved in {}".format( |
| 127 | + cntr, audio_format, dir_path |
| 128 | + ) |
| 129 | + ) |
| 130 | + else: |
| 131 | + print( |
| 132 | + "File format {} is not in supported types (mp3, mp4, m4a, aac)".format( |
| 133 | + audio_format |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + |
0 commit comments