Skip to content

Instantly share code, notes, and snippets.

@punkyoon
Created May 3, 2020 09:13
Show Gist options
  • Select an option

  • Save punkyoon/e7fea38af4cce3c2e75c0e55e63633da to your computer and use it in GitHub Desktop.

Select an option

Save punkyoon/e7fea38af4cce3c2e75c0e55e63633da to your computer and use it in GitHub Desktop.
WAV to MP3
import click
import os
from glob import glob
from pydub import AudioSegment
@click.command()
@click.argument('src')
@click.argument('dst')
def wav2mp3(src, dst):
src = glob(src)
for wav_file in src:
# Get file name
file_name = os.path.basename(wav_file)
file_name = os.path.splitext(file_name)[0]
# Convert to mp3
print(f'[INFO] Start to convert {file_name}')
try:
AudioSegment.from_file(wav_file) \
.export(os.path.join(dst, f'{file_name}.mp3'), format='mp3')
except Exception as e:
print(f'[Error] Fail to convert {file_name}\n -----> {e}')
else:
print(f'[SUCCESS] Success to convert {file_name}')
if __name__ == '__main__':
wav2mp3()
@punkyoon
Copy link
Copy Markdown
Author

punkyoon commented May 3, 2020

How to use

Requiring ffmpeg

$ pip install click pydub
$ python wav_to_mp3.py "Hello World Series_*/*.wav" "temp"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment