Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Created February 25, 2017 18:05
Show Gist options
  • Save KyleJamesWalker/64ce7bdef7cc7e20700f0eaaa0e4a297 to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/64ce7bdef7cc7e20700f0eaaa0e4a297 to your computer and use it in GitHub Desktop.
Convert directory of files with ffmpeg

Converts a directory of low bitrate mp4 audio files to window media audio with ffmpeg.

This is a quick conversion for my car's CD player as it doesn't support mp4.

import json
import os
import re
rename_command = \
"ffmpeg -nostats -loglevel 0 -i {mp4} -codec:a wmav2 -b:a 64k {wma}"
root_path = "."
dest_path = "converts"
root_re = re.escape(root_path)
dest_re = re.escape(dest_path)
for root, dirs, files in os.walk(root_path):
path = root.split(os.sep)
for f in files:
if(f.endswith('.mp4')):
mp4_fn = os.path.join(root, f)
wma_fn = re.sub(r"^{}(.*)(\.mp4)$".format(root_re),
r"{}\1.wma".format(dest_re),
mp4_fn)
if(not os.path.isfile(wma_fn)):
cmd = rename_command.format(
mp4=json.dumps(mp4_fn),
wma=json.dumps(wma_fn),
)
if not os.path.isdir(os.path.dirname(wma_fn)):
os.makedirs(os.path.dirname(wma_fn))
print("generating {}".format(wma_fn))
os.system(cmd)
else:
print("wma exists skipping {}".format(wma_fn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment