<commit_before>#!/usr/bin/env python

import sys
import os
import subprocess


def validate_arguments():

    if len(sys.argv) != (2 + 1):
        print 'Two arguments required, input and output directories'
        sys.exit(1)

    input_dir  = sys.argv[1]
    output_dir = sys.argv[2]

    print 'Input  directory is "{0}"'.format(input_dir)
    print 'Output directory is "{0}"'.format(output_dir)

    if not os.path.isdir(input_dir):
        print 'Input directory does not exist and it should'
        sys.exit(1)

    if os.path.isdir(output_dir):
        print 'Output directory exists and it should not'
        sys.exit(1)

    return input_dir, output_dir

def process_directory(input_dir, output_dir):

    for dirpath, dirnames, filenames in os.walk(input_dir):
        print 'Directory "{0}"'.format(dirpath)
        if dirpath == input_dir: 
            os.makedirs(output_dir)
        else:
            base_path = os.path.basename(dirpath)
            output_subdir = os.path.join(output_dir, base_path)
            os.makedirs(output_subdir)

        for name in filenames:
            input_file = os.path.join(dirpath, name)
            print '\tInput  file "{0}"'.format(input_file)
            relative_path = os.path.relpath(input_file, input_dir)
            (root, ext) = os.path.splitext(relative_path)
            output_file = os.path.join(output_dir, root + '.mp3')
            print '\tOutput file "{0}"'.format(output_file)
            extract_audio(input_file, output_file)

def extract_audio(input_file, output_file):
    command = [ 'ffmpeg',
                '-loglevel', 'panic',
                '-i', input_file,
                '-map', '0:a',
                '-c', 'libmp3lame',
                '-q:a', '2',
                '-f', 'mp3',
                output_file
                ]
    print '\tStarting extraction ...',
    sys.stdout.flush()
    pipe = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=10**8)
    print 'started ...', 
    sys.stdout.flush()
    pipe.wait()
    print 'done.'
    sys.stdout.flush()

if __name__ == '__main__':

    print 'MOOC audio extractor'
    input_dir, output_dir = validate_arguments()
    process_directory(input_dir, output_dir)<commit_msg>slightly more readable<commit_after>#!/usr/bin/env python

import sys
import os
import subprocess


def validate_arguments():

    args = sys.argv[1:]
    if len(args) != 2:
        print 'Two arguments required, input and output directories'
        sys.exit(1)

    input_dir  = sys.argv[1]
    output_dir = sys.argv[2]

    print 'Input  directory is "{0}"'.format(input_dir)
    print 'Output directory is "{0}"'.format(output_dir)

    if not os.path.isdir(input_dir):
        print 'Input directory does not exist and it should'
        sys.exit(1)

    if os.path.isdir(output_dir):
        print 'Output directory exists and it should not'
        sys.exit(1)

    return input_dir, output_dir

def process_directory(input_dir, output_dir):

    for dirpath, dirnames, filenames in os.walk(input_dir):
        print 'Directory "{0}"'.format(dirpath)
        if dirpath == input_dir: 
            os.makedirs(output_dir)
        else:
            base_path = os.path.basename(dirpath)
            output_subdir = os.path.join(output_dir, base_path)
            os.makedirs(output_subdir)

        for name in filenames:
            input_file = os.path.join(dirpath, name)
            print '\tInput  file "{0}"'.format(input_file)
            relative_path = os.path.relpath(input_file, input_dir)
            (root, ext) = os.path.splitext(relative_path)
            output_file = os.path.join(output_dir, root + '.mp3')
            print '\tOutput file "{0}"'.format(output_file)
            extract_audio(input_file, output_file)

def extract_audio(input_file, output_file):
    command = [ 'ffmpeg',
                '-loglevel', 'panic',
                '-i', input_file,
                '-map', '0:a',
                '-c', 'libmp3lame',
                '-q:a', '2',
                '-f', 'mp3',
                output_file
                ]
    print '\tStarting extraction ...',
    sys.stdout.flush()
    pipe = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=10**8)
    print 'working ...', 
    sys.stdout.flush()
    pipe.wait()
    print 'done.'
    sys.stdout.flush()

if __name__ == '__main__':

    print 'MOOC audio extractor'
    input_dir, output_dir = validate_arguments()
    process_directory(input_dir, output_dir)
{'fraction_non_alphanumeric': 0.12285395763656633, 'fraction_numerical': 0.008918617614269788, 'mean_word_length': 2.2744525547445256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1800599', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1399, 'n_words': 320}