# Made by Adriano Farina, 2018
# www.adrianofarina.it
# Licensed under GPL3.0

import os, os.path, sys, time

from subprocess import PIPE
import subprocess

try:
    from Tkinter import *
    import tkFileDialog
    import tkMessageBox
except ImportError:
    print("Tkinter library is not available.")
    exit(0)

start = time.time()
class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.title = "AVJoiner"
        self.vin = ""
        self.ain = ""

        self.videobutton = Button(frame, text="Open Video", command=self.openvideo)
        self.videolabel = Label(frame, text="No Video File Selected")
        self.audiobutton = Button(frame, text="Open Audio", command=self.openaudio)
        self.audiolabel = Label(frame, text="No Audio File Selected")
        self.joinbutton = Button(frame, text="Join", command=self.join)
        
        self.videolabel.pack()
        self.videobutton.pack()
        self.audiolabel.pack()
        self.audiobutton.pack()
        self.joinbutton.pack()

    def openvideo(self):
        #print "openvideo"
        self.vin = tkFileDialog.askopenfilename()
        if self.vin:
        	self.videolabel.configure(text=self.vin)

    def openaudio(self):
        #print "openaudio"
        self.ain = tkFileDialog.askopenfilename()
        if self.ain:
        	self.audiolabel.configure(text=self.ain)
    
    def join(self):
        #print "join"
        vout = os.path.splitext(self.vin)[0]+"_joined.mov"
        exists = os.path.isfile(vout)
        if exists:
            if tkMessageBox.askyesno("File Exists!", "File " + vout + " already exists, do you want to delete it?"):
                os.remove(vout)
            else:
                tkMessageBox.showinfo("EXITING", "In that case we need to exit! Sorry!")
                sys.exit()


        cmd = "ffmpeg -i \"" + self.vin + "\" -i \"" + self.ain + "\" -channel_layout 4.0 -c:a copy -c:v copy -shortest \"" + vout +"\""
        print cmd
        cmd = cmd.split()
        child = subprocess.Popen(cmd, shell=False, stderr=subprocess.PIPE)
        out, err = child.communicate()
        if out == None:
            tkMessageBox.showinfo("Done", "Done! Exiting")
            sys.exit()
        #print "out = " + out +"\t err = " + err

root = Tk()
app = App(root)
root.mainloop()

#the following block is to be replaces with a UI open file dialog
#ain = "R0010127_bformat.wav" #audio input file
#vin = "R0010127_senza_audio.MP4" #video input file
#vout = os.path.splitext(vin)[0]+"_joined.mp4"
#exists = os.path.isfile(vout)
#if exists:
#    deletion = str(raw_input("File already exists, want to delete it? Y/N? "))
#    if deletion == "Y" or deletion == "y":
#        os.remove(vout)
#    else:
#        print("In that case we need to exit! Sorry!")
#        sys.exit()
#
#ff = FFmpeg(
#    inputs={vin: None, ain: None},
#    outputs={vout: "-channel_layout 4.0 -c:a aac -b:a 128k -strict -2 -c:v copy -shortest"}
#)
#ff.cmd
#print ff.cmd
#ff.run()