Skip to content

Instantly share code, notes, and snippets.

@drojf
Last active September 30, 2016 15:05
Show Gist options
  • Save drojf/7ee63088b1d0751f64ae8d557d60e77c to your computer and use it in GitHub Desktop.
Save drojf/7ee63088b1d0751f64ae8d557d60e77c to your computer and use it in GitHub Desktop.
script to get the audio lengths of files in a folder
#http://stackoverflow.com/questions/16953842/using-os-walk-to-recursively-traverse-directories-in-python
#http://stackoverflow.com/questions/6239350/how-to-extract-duration-time-from-ffmpeg-output
import os, subprocess, math
#!!!!!!!!!!!!!!! SET THESE TWO PATHS ACCORDING TO YOUR FFMPEG / GAME LOCATION !!!!!!!!!!!!!!!
search_dir=r'C:\games\Steam\steamapps\common\Umineko\voice'
ffprobe_path = r'c:\prog\ffmpeg\ffprobe.exe' #download ffmpeg exe pack from here: https://ffmpeg.zeranoe.com/builds/
file_extensions=[r'.ogg']
def get_partial_path(path, depth):
total_path = ''
for i in range(depth):
rest_of_path, containing_folder = os.path.split(path)
total_path = os.path.join(containing_folder, total_path)
path = rest_of_path
return total_path
FNULL = open(os.devnull, 'w')
for root, dirs, files in os.walk(search_dir):
file_partial_path = get_partial_path(root, 2) #eg voice/06/
for file in files:
_, file_ext = os.path.splitext(file)
if file_ext not in file_extensions:
break
full_file_path = os.path.join(root, file)
result_as_bytes = subprocess.check_output([ffprobe_path, '-i', full_file_path, '-show_format'], stderr=FNULL) #stderr=FNULL hides output
result_as_string = result_as_bytes.decode('utf-8')
#note: possible to have more than one matching line here, or no matching lines
matching_lines = [l for l in result_as_string.splitlines() if "duration=" in l]
tokens = (matching_lines[0]).split('=')
duration = tokens[1]
#filename includes partial path, delay is in milliseconds. You may want to add some extra time after each audio plays in your script
print('{}, {}'.format(os.path.join(file_partial_path,file), math.ceil(float(duration) * 1000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment