Skip to content

Instantly share code, notes, and snippets.

@ivannp
Created February 12, 2017 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivannp/ddc501f62f0c159d13202b111fb19721 to your computer and use it in GitHub Desktop.
Save ivannp/ddc501f62f0c159d13202b111fb19721 to your computer and use it in GitHub Desktop.
Converting files (.m4b to .mp3) in python
import glob
import os
import subprocess
def main():
base_dir = os.path.abspath('d:/abooks')
ffmpeg_path = os.path.abspath('C:/tools/ffmpeg/bin/ffmpeg.exe')
pattern = os.path.join(base_dir, '**', '*.m4b')
file_list = [file for file in glob.glob(pattern, recursive = True)]
for file in file_list:
dir_path, file_name = os.path.split(file)
# Drop the file extension
file_name = os.path.splitext(file_name)[0]
output_path = os.path.join(dir_path, file_name + '.mp3')
if not os.path.exists(output_path):
# Convert
print('Converting: ' + file)
subprocess.call([ffmpeg_path, '-i', file, '-acodec', 'libmp3lame', output_path])
else:
print('Skipping: ' + file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment