Skip to content

Commit

Permalink
remove console popup caused by popen
Browse files Browse the repository at this point in the history
- better message on ffmpeg check
  • Loading branch information
Dadangdut33 committed Nov 7, 2023
1 parent a1c11ed commit 255fa9e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 25 deletions.
13 changes: 13 additions & 0 deletions speech_translate/globals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import copy
import subprocess
from ast import literal_eval
from platform import system
from shlex import quote
Expand Down Expand Up @@ -27,6 +28,18 @@
from .utils.custom.queue import MyQueue as Queue
import pyaudio # type: ignore


# monkey patch subprocess.run
class NoConsolePopen(subprocess.Popen):
def __init__(self, args, **kwargs):
if 'startupinfo' not in kwargs:
kwargs['startupinfo'] = subprocess.STARTUPINFO()
kwargs['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW
super().__init__(args, **kwargs)


subprocess.Popen = NoConsolePopen

# remove numba warnings
simplefilter("ignore", category=NumbaDeprecationWarning)
simplefilter("ignore", category=NumbaPendingDeprecationWarning)
Expand Down
81 changes: 62 additions & 19 deletions speech_translate/ui/window/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,8 @@ def first_open():
self.root.after(2000, self.check_ffmpeg, gc.has_ffmpeg)

def check_ffmpeg(self, has_ffmpeg: bool):
ffmpeg_installed = False
user_cancel = False
if not has_ffmpeg:
# prompt to install ffmpeg
if mbox(
Expand All @@ -777,9 +779,14 @@ def check_ffmpeg(self, has_ffmpeg: bool):
mbox("Error", msg, 2)

gc.has_ffmpeg = True
return success
ffmpeg_installed = success
else:
ffmpeg_installed = False
user_cancel = True
else:
return True
ffmpeg_installed = True

return ffmpeg_installed, user_cancel

# mic
def cb_input_device_init(self):
Expand Down Expand Up @@ -1417,16 +1424,25 @@ def rec(self):
return

# check ffmpeg
success = self.check_ffmpeg(check_ffmpeg_in_path()[0])
success, user_cancel = self.check_ffmpeg(check_ffmpeg_in_path()[0])
if not success:
# ask if user want to continue processing
if not mbox(
"Fail to install ffmpeg",
"The program fail to install and add ffmpeg to path. Do you still want to continue regardless of it?", 3,
self.root
"FFMpeg is not installed!",
"The program needs ffmpeg to process files and will probably not work without it. Do you still want to continue regardless of it?",
3, self.root
):
return

if user_cancel:
mbox(
"Cancelled",
"The program needs ffmpeg to process files and will probably not work without it. Please install it first.",
2,
)

return

# ui changes
self.tb_clear()
self.start_loadBar()
Expand Down Expand Up @@ -1492,15 +1508,24 @@ def do_process(m_key, engine, source, target, tc, tl, files):
return False

# check ffmpeg
success = self.check_ffmpeg(check_ffmpeg_in_path()[0])
success, user_cancel = self.check_ffmpeg(check_ffmpeg_in_path()[0])
if not success:
# ask if user want to continue processing
if not mbox(
"Fail to install ffmpeg",
"The program fail to install and add ffmpeg to path. Do you still want to continue regardless of it?", 3,
self.root
"FFMpeg is not installed!",
"The program needs ffmpeg to process files and will probably not work without it. Do you still want to continue regardless of it?",
3, self.root
):
return False # use choose to cancel
return False

if user_cancel:
mbox(
"Cancelled",
"The program needs ffmpeg to process files and will probably not work without it. Please install it first.",
2,
)

return False

# ui changes
self.tb_clear()
Expand Down Expand Up @@ -1584,16 +1609,25 @@ def do_process(m_key, files):
return False

# check ffmpeg
success = self.check_ffmpeg(check_ffmpeg_in_path()[0])
success, user_cancel = self.check_ffmpeg(check_ffmpeg_in_path()[0])
if not success:
# ask if user want to continue processing
if not mbox(
"Fail to install ffmpeg",
"The program fail to install and add ffmpeg to path. Do you still want to continue regardless of it?", 3,
self.root
"FFMpeg is not installed!",
"The program needs ffmpeg to process files and will probably not work without it. Do you still want to continue regardless of it?",
3, self.root
):
return False

if user_cancel:
mbox(
"Cancelled",
"The program needs ffmpeg to process files and will probably not work without it. Please install it first.",
2,
)

return False

# ui changes
self.tb_clear()
self.start_loadBar()
Expand Down Expand Up @@ -1660,16 +1694,25 @@ def do_process(m_key, files):
return False

# check ffmpeg
success = self.check_ffmpeg(check_ffmpeg_in_path()[0])
success, user_cancel = self.check_ffmpeg(check_ffmpeg_in_path()[0])
if not success:
# ask if user want to continue processing
if not mbox(
"Fail to install ffmpeg",
"The program fail to install and add ffmpeg to path. Do you still want to continue regardless of it?", 3,
self.root
"FFMpeg is not installed!",
"The program needs ffmpeg to process files and will probably not work without it. Do you still want to continue regardless of it?",
3, self.root
):
return False

if user_cancel:
mbox(
"Cancelled",
"The program needs ffmpeg to process files and will probably not work without it. Please install it first.",
2,
)

return False

# ui changes
self.tb_clear()
self.start_loadBar()
Expand Down
21 changes: 15 additions & 6 deletions speech_translate/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from os import path, startfile
from platform import system
from random import choice
from subprocess import Popen
from tkinter import colorchooser, ttk
from typing import Dict, List, Union
from webbrowser import open_new
Expand All @@ -26,6 +25,13 @@
from speech_translate.utils.types import ToInsert


def launchWithoutConsole(command):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen(command, startupinfo=startupinfo).wait()


def kill_thread(thread: Thread) -> bool:
''' Attempt to kill thread, credits: https://github.com/JingheLee/KillThread
Expand All @@ -41,7 +47,10 @@ def kill_thread(thread: Thread) -> bool:
'''
try:
if isinstance(thread, Thread):
return ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread.ident), ctypes.py_object(SystemExit)) == 1
return ctypes.pythonapi.PyThreadState_SetAsyncExc(
ctypes.c_long(thread.ident), # type: ignore
ctypes.py_object(SystemExit)
) == 1
else:
return False
except Exception as e:
Expand Down Expand Up @@ -253,7 +262,7 @@ def check_ffmpeg_in_path():
success = True
msg = ""
try:
Popen(["ffmpeg", "-version"])
launchWithoutConsole(["ffmpeg", "-version"])
msg = "ffmpeg is in the path."
except FileNotFoundError:
success = False
Expand Down Expand Up @@ -338,7 +347,7 @@ def install_ffmpeg_windows():
)
logger.debug("Running ps script...")
# run the script
p = Popen(
p = subprocess.Popen(
[
"powershell", "-ExecutionPolicy", "-noprofile", "-c",
rf"""Start-Process -Verb RunAs -Wait powershell.exe -Args "-noprofile -c Set-Location \`"$PWD\`"; & {ffmpeg_ps_script}"
Expand All @@ -360,7 +369,7 @@ def install_ffmpeg_linux():
"""
Install ffmpeg on linux
"""
p = Popen(["sudo", "apt", "install", "ffmpeg"])
p = subprocess.Popen(["sudo", "apt", "install", "ffmpeg"])
status = p.wait()
if status != 0:
success = False
Expand All @@ -376,7 +385,7 @@ def install_ffmpeg_macos():
"""
Install ffmpeg on macos
"""
p = Popen(["brew", "install", "ffmpeg"])
p = subprocess.Popen(["brew", "install", "ffmpeg"])
status = p.wait()
if status != 0:
success = False
Expand Down

0 comments on commit 255fa9e

Please sign in to comment.