Skip to content

Commit

Permalink
added pynput as dependency; fixed hotkey in headless env; disabled FF…
Browse files Browse the repository at this point in the history
…mpeg stdin
  • Loading branch information
k4yt3x committed Mar 21, 2022
1 parent d72ecb3 commit 268460f
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 20 deletions.
120 changes: 119 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
"realsr-ncnn-vulkan-python>=1.0.4",
"rife-ncnn-vulkan-python>=1.1.2.post3",
"realcugan-ncnn-vulkan-python>=1.0.0",
"pynput>=1.7.6",
]
dynamic = ["version"]

Expand Down
4 changes: 3 additions & 1 deletion video2x/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Name: Video Decoder
Author: K4YT3X
Date Created: June 17, 2021
Last Modified: March 20, 2022
Last Modified: March 21, 2022
"""

import contextlib
Expand Down Expand Up @@ -83,6 +83,7 @@ def __init__(
.output("pipe:1", format="rawvideo", pix_fmt="rgb24", vsync="cfr")
.global_args("-hide_banner")
.global_args("-nostats")
.global_args("-nostdin")
.global_args(
"-loglevel",
LOGURU_FFMPEG_LOGLEVELS.get(
Expand All @@ -92,6 +93,7 @@ def __init__(
overwrite_output=True,
),
env={"AV_LOG_FORCE_COLOR": "TRUE"},
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
Expand Down
51 changes: 33 additions & 18 deletions video2x/video2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Name: Video2X
Creator: K4YT3X
Date Created: February 24, 2018
Last Modified: March 20, 2022
Last Modified: March 21, 2022
Editor: BrianPetkovsek
Last Modified: June 17, 2019
Expand All @@ -48,11 +48,9 @@
import signal
import sys
import time
from typing import Union

import cv2
import ffmpeg
import pynput
from loguru import logger
from rich import print
from rich.console import Console
Expand All @@ -73,6 +71,15 @@
from .interpolator import Interpolator
from .upscaler import Upscaler

# for desktop environments only
# if pynput can be loaded, enable global pause hotkey support
try:
import pynput
except ImportError:
ENABLE_HOTKEY = False
else:
ENABLE_HOTKEY = True

LEGAL_INFO = """Video2X\t\t{}
Author:\t\tK4YT3X
License:\tGNU AGPL v3
Expand Down Expand Up @@ -255,6 +262,7 @@ def _run(
"<",
TimeRemainingColumn(),
console=console,
speed_estimate_period=300.0,
disable=True,
)

Expand All @@ -264,21 +272,26 @@ def _run(
# allow sending SIGUSR1 to pause/resume processing
signal.signal(signal.SIGUSR1, self._toggle_pause)

# create global pause hotkey
pause_hotkey = pynput.keyboard.HotKey(
pynput.keyboard.HotKey.parse("<ctrl>+<alt>+v"), self._toggle_pause
)
# enable global pause hotkey if it's supported
if ENABLE_HOTKEY is True:

# create global keyboard input listener
keyboard_listener = pynput.keyboard.Listener(
on_press=(lambda key: pause_hotkey.press(keyboard_listener.canonical(key))),
on_release=(
lambda key: pause_hotkey.release(keyboard_listener.canonical(key))
),
)
# create global pause hotkey
pause_hotkey = pynput.keyboard.HotKey(
pynput.keyboard.HotKey.parse("<ctrl>+<alt>+v"), self._toggle_pause
)

# create global keyboard input listener
keyboard_listener = pynput.keyboard.Listener(
on_press=(
lambda key: pause_hotkey.press(keyboard_listener.canonical(key))
),
on_release=(
lambda key: pause_hotkey.release(keyboard_listener.canonical(key))
),
)

# start monitoring global key presses
keyboard_listener.start()
# start monitoring global key presses
keyboard_listener.start()

# a temporary variable that stores the exception
exception = []
Expand Down Expand Up @@ -329,9 +342,11 @@ def _run(
exception.append(e)

finally:

# stop keyboard listener
keyboard_listener.stop()
keyboard_listener.join()
if ENABLE_HOTKEY is True:
keyboard_listener.stop()
keyboard_listener.join()

# stop progress display
self.progress.stop()
Expand Down

0 comments on commit 268460f

Please sign in to comment.