import re
import subprocess
import sys
from pathlib import Path
import os
import xml.etree.ElementTree as ET


output_folder_name = "/out_sub_audio/"
regex_subtitle = re.compile("Stream #0:\d+(\(\w*\))?: Subtitle:.*")
regex_audio = re.compile("Stream #0:\d(\(\w*\))?: Audio:.*")
nr_files = 0
files_processed = []
#droppedFile = sys.argv[1]
#droppedName = Path(droppedFile).name
#originalfiles = sys.orig_argv[2:]
audio_list = []
sub_list = []
stream_index_modify = []
channels_index_modify = []
stream_index = []
channels_index = []
target_bps_per_channel=96000
audio_stringmap = ""
test_string=""
m_index=int()


# To fix I need to create symlinks to files with long paths and use the links instead. And then remove said symlinks once


#droppedFile= ['X:\\test\\longpathtest\\longerpathlongerpathlongerpathlongerpathlongerpathlongerpathlongerpath\\longpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpath\\fourteen\\[Kira-Fansub]_Choujuushin Gravion_Episode 06_(BD 1280x960 h264 JP AAC EN AAC) [E7D92968].mkv', '', ' X:\\test\\longpathtest\\longerpathlongerpathlongerpathlongerpathlongerpathlongerpathlongerpath\\longpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpathlongpath\\fourteen\\[Kira-Fansub]_Choujuushin Gravion_Episode 09_(BD 1280x960 h264 JP AAC EN AAC) [A2727604].mkv']
#originalfiles = ['E:\\torrents\\anime_works\\Tower of God S01 (BD 1080p HEVC) [Dual-Audio] [Vodes]\\[Vodes] Tower of God - S01E01 (BD 1080p).mkv']
originalfiles = ['E:\\out\\ORIGINAL_COMP_0Muv-Luv Alternative Total Eclipse S01E01 2012 Bluray 1080p DTS-HD MA 2.0 AVC REMUX Dual Audio-ZR-.mkv']



joined = (' '.join(originalfiles))
re.sub (' +', ' ', joined)
listed = re.split(r'(.+?mkv)', joined)
listed = list(filter(None, listed))
listed = [i.lstrip() for i in listed]




#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]

#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
    os.makedirs(output_path+output_folder_name)

#droppedFile = "test.mkv"


for i, file in enumerate(listed):
    arg_list = '.\\ffprobe.exe -i "{file}" -show_entries format:streams -of xml'.format(file=file)
    print(arg_list)

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

    #print(subprocess_err)

    root = ET.fromstring(subprocess_err)

    #print(root)
    streams = root[1].findall("stream")

    #print(streams)

#    for stream in streams:
#        if stream.get("codec_type") == "audio":
#            index = stream.get("index")
#            print("index:", stream.get("index"))
#            print("channels:",stream.get("channels"))
#            print("printing index variable:" ,index )
#                        
#            for tag in stream.findall("tag"):
#                if tag.get("key") == "title":
#                    print("name:", tag.get("value"))
#                if tag.get("key") == "BPS":
#                    print("BPS:",tag.get("value"))


    for index in streams:
        if index.get("codec_type") == "audio":
            stream_channels = int(index.get("channels"))
            
            for tag in index.findall("tag"):
                if tag.get("key") == "BPS":
                    BPS_Value = int(tag.get("value"))
                    if BPS_Value >= target_bps_per_channel*stream_channels*1.2:
                        #stream_index_modify.append(int(index.get("index"))-1)
                        #channels_index_modify.append(int(index.get("channels")))
                        m_index = int(index.get("index"))
                        m_index -= 1
                        c_index = int(index.get("channels"))
                        print(m_index)
                        bitrate = int(stream_channels*target_bps_per_channel/1000)
                        test_map = "-map 0:a:{m_index} -c:a:{m_index} libopus -ac:a:{c_index} {stream_channels} -b:a:{m_index} {bitrate}k ".format(m_index=m_index,c_index=c_index, bitrate=bitrate,stream_channels=stream_channels)
                        test_string += test_map
                    else:
                        #stream_index.append(int(index.get("index"))-1)
                        #channels_index.append(int(index.get("channels")))
                        m_index = int(index.get("index"))
                        m_index -= 1
                        c_index = int(index.get("channels"))
                        bitrate = int(stream_channels*target_bps_per_channel/1000)
                        test_map = "-map 0:a:{m_index} -c:a:{m_index} libopus -ac:a:{c_index} {stream_channels} -b:a:{m_index} {bitrate}k ".format(m_index=m_index,c_index=c_index, bitrate=bitrate,stream_channels=stream_channels)
                        audio_stringmap += test_map

print("streams to transcode: ", stream_index_modify)
print("channels to modify", channels_index_modify)
print("streams to copy: ", stream_index)
print("channels to copy: ", channels_index)
print("test map: ", test_string)
#create ffmpeg string
x=0
for each in stream_index_modify:
    
    channel_layout = channels_index_modify[x]
    bitrate = int(channel_layout*target_bps_per_channel/1000)
    audio_stringmap += "-map 0:a:{each} -c:a libopus -ac {channel_layout} -b:a {bitrate}k ".format(each=each,bitrate=bitrate,channel_layout=channel_layout)
    x+=1
    #print(audio_stringmap)
x=0
for each in stream_index:
    
    channel_layout = channels_index[x]
    bitrate = int(channel_layout*target_bps_per_channel/1000)
    audio_stringmap += "-map 0:a:{each} -c:a copy ".format(each=each,bitrate=bitrate,channel_layout=channel_layout)
    x+=1
print(audio_stringmap)



#def gen_audio_string(index, channels):
#    x=0
#    stringmap=""
#    for each in index:
#        channel_layout = channels[x]
#        bitrate = int(channel_layout*target_bps_per_channel/1000)
#        stringmap += "-map 0:a:{each} -c:a libopus -ac {channel_layout} -b:a {bitrate}k ".format(each=each,bitrate=bitrate,channel_layout=channel_layout)
#        x+=1
#    return(stringmap)

#print(gen_audio_string(stream_index_modify, channels_index_modify))
#print(gen_audio_string(stream_index, channels_index))

#audio_stringmap=(gen_audio_string(stream_index_modify, channels_index_modify))+(gen_audio_string(stream_index, channels_index))

print(audio_stringmap)
