__author__ = 'Rui'

from tkinter import *
from tkinter import filedialog
import xml.etree.ElementTree as xmlParser
from subprocess import *
import os

ffmpeg_path = "ffmpeg.exe"

root = Tk()

class FileChooser(object):

    def __init__(self):
        btn = Button(text="Open File", command=self.open_dialog)
        btn.pack()

        another = Button(text="Quit", command=self.quit_app)
        another.pack()

    def open_dialog(self):
        fn = filedialog.askopenfilename(filetypes=(("VO Playlist", "*.vopl"),
                                                   ("All Files", "*.*")),
                                        initialdir="C://Users//Rui//Documents//VideoObserver//Playlist"
                                        )
        self.parse_playlist(filename=fn)

    def quit_app(self):
        exit()

    def parse_playlist(self, filename):
        print("INfile>> ", filename)
        tree = xmlParser.parse(filename)
        base = tree.getroot()
        video_path = base.get("video_path")
        video_path = video_path[8:]
        print("VPath>> ", video_path)

        print("")

        cut_number = 0
        # start parsing each item
        for child in base.findall('.items/item'):
            item_type = child.find("type").text
            print("ItemType>> ", item_type)

            time_start = ""
            time_end = ""
            comments = ""

            if item_type == "ga":
                time_start = int(child.find("game_action").find("video_time_start").text)
                time_end = int(child.find("game_action").find("video_time_end").text)
                comments = child.find("game_action").find("comments").text

            print("TimeStart>> ", time_start)
            print("TimeEnd>> ", time_end)
            print("Comments>> ", comments)

            print("")

            duration = time_end - time_start
            tmp_out = os.path.dirname(os.path.realpath(__file__)) + "\\" + str(cut_number) + ".mp4"
            cut_number += 1

            codec = "-c copy -bsf:v h264_mp4toannexb -f mpegts"

            try:
                # out = check_output([ffmpeg_path, args], shell=False)
                out = check_output([
                    # path to ffmpeg
                    ffmpeg_path,
                    # overwrite
                    "-y",
                    # start time
                    "-ss",
                    str(time_start),
                    # input file
                    "-i",
                    video_path,
                    # duration
                    "-t",
                    str(duration),
                    # codec
                    "-c",
                    "copy",
                    "-bsf:v",
                    "h264_mp4toannexb",
                    "-f",
                    "mpegts",
                    # output file
                    tmp_out
                ], shell=False)

            except CalledProcessError as cpe:
                print("ERROR>> ", cpe.output)

        # JOIN THE THINGS
        join_args = []
        # path to ffmpeg
        join_args.append(ffmpeg_path)
        # overwrite
        join_args.append("-y")
        # input
        join_args.append("-i")
        # the concat files
        concat = "concat:"
        for x in range(0, cut_number):
            concat += os.path.dirname(os.path.realpath(__file__)) + "\\" + str(x) + ".mp4" + "|"
        concat = concat[:-1]
        join_args.append(concat)
        # outfile
        join_args.append(os.path.dirname(os.path.realpath(__file__)) + "\\" + "final" + ".mp4")

        try:
            out = check_output(join_args, shell=False)
        except CalledProcessError as cpe:
            print("ERROR>>", cpe.output)

        # DEBUG
        exit()

if __name__ == '__main__':
    fc = FileChooser()
    mainloop()

@@ -142,16 +142,32 @@
 mport os
+%0Aimport tempfile
 %0A%0Affmpeg
@@ -442,32 +442,113 @@
 n_dialog(self):%0A
+%0A        initial_dir = os.path.expanduser(%22~/Documents/VideoObserver/Playlist%22)%0A%0A
         fn = fil
@@ -738,60 +738,19 @@
 dir=
-%22C://Users//Rui//Documents//VideoObserver//Playlist%22
+initial_dir
 %0A   
@@ -910,16 +910,240 @@
 ename):%0A
+%0A        # to keep the cut files%0A        temp_dir = tempfile.TemporaryDirectory()%0A        # we have a name so make sure we create the dir%0A        if not os.path.exists(temp_dir.name):%0A            os.makedirs(temp_dir.name)%0A%0A
         
@@ -1947,16 +1947,294 @@
 ).text%0A%0A
+            if item_type == %22cue%22:%0A                time_start = int(child.find(%22action_cue%22).find(%22starting_time%22).text)%0A                time_end = int(child.find(%22action_cue%22).find(%22ending_time%22).text)%0A                comments = child.find(%22action_cue%22).find(%22comments%22).text%0A%0A
         
@@ -2451,51 +2451,21 @@
 t = 
-os.path.
+temp_
 dir
+.
 name
-(os.path.realpath(__file__))
  + %22
@@ -2528,73 +2528,8 @@
  1%0A%0A
-            codec = %22-c copy -bsf:v h264_mp4toannexb -f mpegts%22%0A%0A
     
@@ -3782,51 +3782,21 @@
  += 
-os.path.
+temp_
 dir
+.
 name
-(os.path.realpath(__file__))
  + %22
@@ -3915,68 +3915,127 @@
     
-join_args.append(os.path.dirname(os.path.realpath(__file__))
+# put it on desktop for now%0A        desktop_dir = os.path.expanduser(%22~/Desktop/%22)%0A        join_args.append(desktop_dir
  + %22
@@ -4203,32 +4203,78 @@
 %22, cpe.output)%0A%0A
+        # Cleanup%0A        temp_dir.cleanup()%0A%0A
         # DEBUG%0A

use temp dirs and no hardcoded pats