import re
import subprocess
import sys
from pathlib import Path
import os
regex = re.compile("Stream #0:\d(\(\w*\))?: Audio:.*")

droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name

skipto = " -ss "

#droppedFile = "test.mkv"



#skip first part of sys.argv since it points to script location
arguments = sys.argv[1:]

for i, each in enumerate(arguments):
    
    arg_list = 'ffprobe.exe -i "{each}"'.format(each=each)

    cmd = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')
        
    subprocess_err = cmd.communicate()[1]
    
    print("file", i+1)

    for r, match in enumerate(regex.finditer(subprocess_err)):
        print("Track", r, match.group(0))

    print('\n')



x = (int(x) for x in input ("Enter tracks to keep: ").split())


track = "".join(" -map 0:a:{}".format(y) for y in x)

for each in arguments:
 
    path = each
    basename = os.path.basename(path)

    arg_list = 'ffmpeg.exe -i "{each}" -map 0:v {track} -c copy -map 0:s "E:/Projects/ffmpeg_tests/audio_tracks/out/{basename}"'.format(each=each, track=track, basename=basename)

    print(arg_list)

    subprocess.run(arg_list)


input("press enter to exit") 
