Skip to content

Instantly share code, notes, and snippets.

@Nannigalaxy
Last active July 6, 2021 15:46
Show Gist options
  • Save Nannigalaxy/991e4f7be7405eafa974816c2559ca67 to your computer and use it in GitHub Desktop.
Save Nannigalaxy/991e4f7be7405eafa974816c2559ca67 to your computer and use it in GitHub Desktop.
Convert audio file format using ffmpeg in ubuntu
''' convert file format using ffmpeg in ubuntu
windows install: https://video.stackexchange.com/questions/20495/how-do-i-set-up-and-use-ffmpeg-in-windows
or https://www.wikihow.com/Install-FFmpeg-on-Windows
Usage:
$ python ./format_convert.py --input <dir/path> --output <dir/path>
'''
import os
import argparse
arg = argparse.ArgumentParser()
arg.add_argument("--input",default='./tf')
arg.add_argument("--output",default="./wav_dataset")
opt = arg.parse_args()
INPUT_FORMAT = ('aac','mp4','m4a','ogg','mp3','opus')
OUTPUT_FORMAT = 'wav'
PATH = opt.input+'/'
SAMPLE_RATE = 16000
OUT_DIR = opt.output
if not os.path.isdir(OUT_DIR):
os.mkdir(OUT_DIR)
dirs = os.listdir(PATH)
dirs.sort()
print(dirs)
for folder in dirs:
c=0
if not os.path.isdir(OUT_DIR+'/'+folder):
os.mkdir(OUT_DIR+'/'+folder)
for filename in os.listdir(PATH+folder):
if filename.endswith(INPUT_FORMAT):
c+=1
output = OUT_DIR+'/'+folder+'/'+filename.split('.')[0]+".wav"
if not os.path.exists(output):
print(output)
os.system("ffmpeg -i '{0}' -ar {1} '{4}/{2}.{3}' -y".format(PATH+folder+'/'+filename, SAMPLE_RATE, filename.split('.')[0], OUTPUT_FORMAT, OUT_DIR+'/'+folder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment