~boringcactus/vidslice

ref: 9045791ade39f464b6fdb42483490526ad439475 vidslice/vidslice.py -rw-r--r-- 4.1 KiB
9045791a — Melody Horn cycle out the api key oops 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
import subprocess
import urllib.request
from tkinter import *
from tkinter import messagebox
from tkinter import ttk

from options import OptionsPanel
from output import OutputPanel
from sources import SourcesPanel, update_ytdl

VERSION = "1.6"


def check_update(parent):
    latest_release_api_url = 'https://api.github.com/repos/boringcactus/vidslice/releases/latest'
    with urllib.request.urlopen(latest_release_api_url) as latest_release_response:
        latest_release_obj = json.load(latest_release_response)
    newest_version = latest_release_obj['tag_name'].lstrip('v')
    if VERSION != newest_version:
        open_update = messagebox.askyesno(message="vidslice update available. download?", title="Update",
                                          parent=parent)
        if open_update:
            import webbrowser

            webbrowser.open("https://github.com/boringcactus/vidslice/releases/latest")


def has_ffmpeg():
    try:
        subprocess.run(["ffmpeg", "-version"],
                       stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW)
        return True
    except FileNotFoundError:
        return False


class VidsliceFrame:
    def __init__(self, root: Tk):
        self.root = root
        root.title('vidslice')

        mainframe = ttk.Frame(root, padding="3 3 12 12")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        root.columnconfigure(0, weight=1)
        root.rowconfigure(0, weight=1)

        # set up sources panel
        self.sources_panel = SourcesPanel(mainframe)
        self.sources_panel.grid(column=0, row=0, columnspan=2, sticky=(W, E), padx=5, pady=5)

        # set up options panel
        self.options_panel = OptionsPanel(mainframe)
        self.options_panel.grid(column=0, row=1, sticky=(W, E, N), padx=5, pady=5)
        mainframe.rowconfigure(1, weight=1)
        self.sources_panel.on_update(self.options_panel.update_info)

        # set up output panel
        self.output_panel = OutputPanel(mainframe, get_ffmpeg_args=self.options_panel.ffmpeg_opts,
                                        get_frame_count=self.options_panel.frame_count)
        self.output_panel.grid(column=1, row=1, sticky=(W, E, N, S), padx=5, pady=5)
        mainframe.columnconfigure(1, weight=1)
        self.sources_panel.on_update(lambda data: self.output_panel.set_input_path(self.sources_panel.get_file(), data))

        # create a menu bar
        self.make_menu_bar(root)

        if len(sys.argv) > 1:
            self.sources_panel.file_text.SetValue(sys.argv[1])

    def make_menu_bar(self, root):
        root.option_add('*tearOff', FALSE)

        menubar = Menu(root)
        root['menu'] = menubar

        file_menu = Menu(menubar)
        menubar.add_cascade(menu=file_menu, label='File', underline=0)
        file_menu.add_command(label="Update youtube-dl", command=self.on_update, underline=0)
        file_menu.add_separator()
        file_menu.add_command(label='Exit', command=self.on_exit, underline=1)

        help_menu = Menu(menubar)
        menubar.add_cascade(menu=help_menu, label='Help', underline=0)
        help_menu.add_command(label='About', command=self.on_about, underline=0)

    def on_exit(self, *args):
        self.root.destroy()

    def on_update(self, *args):
        import threading
        threading.Thread(target=update_ytdl, args=(self.root,)).start()

    def on_about(self, *args):
        messagebox.showinfo(message=f"vidslice {VERSION}")


def check_ffmpeg(root: Tk):
    if not has_ffmpeg():
        open_readme = messagebox.askyesno(message="Could not find ffmpeg. Open vidslice README?", title="Error",
                                          icon='error', parent=root)
        if open_readme:
            import webbrowser

            webbrowser.open("https://github.com/boringcactus/vidslice/blob/master/README.md")
            root.after(1000, root.destroy)


def main():
    root = Tk()
    frame = VidsliceFrame(root)
    root.after_idle(check_ffmpeg, root)
    root.after(1000, check_update, root)
    root.mainloop()


if __name__ == '__main__':
    main()