#!/usr/bin/python

import argparse
import os
import glob

# https://trac.ffmpeg.org/wiki/Encode/MP3
# lame option    Average kbit/s    Bitrate range kbit/s    ffmpeg/avconv option
#       -V 0        245                  220-260            	-q:a 0
#       -V 1        225                  190-250                -q:a 1
#       -V 2        190                  170-210            	-q:a 2
#       -V 3        175                  150-195            	-q:a 3
#       -V 4        165                  140-185            	-q:a 4
#       -V 5        130                  120-150            	-q:a 5
#       -V 6        115                  100-130            	-q:a 6
#       -V 7        100                   80-120            	-q:a 7
#       -V 8        85                    70-105            	-q:a 8
#       -V 9        65                     45-85              	-q:a 9


def is_valid_file(arg):
    if not os.path.isfile(arg):
        parser.error('{} does not exist.'.format(arg))
    else:
        return arg


def is_valid_folder(arg):
    if not os.path.isdir(arg):
        parser.error('{} does not exist.'.format(arg))
    else:
        return arg


def get_command(infile, outdir, quality=0):
    # avconv -i "infile" -loglevel panic -threads auto -vn -qscale:a 0 "$OUTDIR/filename.mp3"
    out_basename = os.path.splitext(os.path.basename(infile))[0]
    cmd = []
    cmd.append('avconv')
    cmd.append('-i')
    cmd.append('"' + str(infile) + '"')
    cmd.append('-loglevel')
    cmd.append('panic')
    cmd.append('-threads')
    cmd.append('auto')
    cmd.append('-vn')
    cmd.append('-qscale:a')
    cmd.append(quality)
    cmd.append('"' + os.path.join(outdir, out_basename) + '.mp3"')
    cmd = ' '.join(cmd)
    return cmd


def process_folder(indir):
    files_grabbed = []
    types = ('*.avi', '*.mkv', '*.mp4', '*.flac')
    for files in types:
        files_grabbed.extend(glob.glob(os.path.join(indir, files)))

    for media in files_grabbed:
        os.system(get_command(media, args.output_folder, args.quality))


if __name__ == '__main__':
    default_output_folder = os.path.join(os.path.expanduser("~"), "Downloads")

    parser = argparse.ArgumentParser()
    parser.add_argument('-o', action='store', dest='output_folder',
                        help='folder to dump output mp3s', nargs='?',
                        default=default_output_folder, type=is_valid_folder)

    parser.add_argument('-f', action='store', dest='source_file',
                        help='file to process', nargs='?', type=is_valid_file)

    parser.add_argument('-d', action='store', dest='source_folder',
                        help='folder to process', nargs='?', type=is_valid_folder)

    parser.add_argument('-q', action='store', dest='quality',
                        help='quality setting [0 (high) - 9 (low)]', nargs='?',
                        default='0', metavar='[0-9]')

    args = parser.parse_args()

    if args.source_file:
        os.system(get_command(args.source_file, args.output_folder, args.quality))
    if args.source_folder:
        process_folder(args.source_folder)
