Skip to content

Commit

Permalink
general improvements (#195)
Browse files Browse the repository at this point in the history
* added print statements for waifu2x-ncnn-vulkan failing, added try / catch exception on abstracted class call

* added hwaccel support for re-encoding and piping

Leaving both on as an experiment

* better and new logging

* replaced print statement with log
  • Loading branch information
akai-katto committed Feb 16, 2021
1 parent 6011a55 commit b659aa8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/config_files/output_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ realsr_ncnn_vulkan:
-verbose: null

ffmpeg:
-hwaccel: true
pre_process_video:
-hwaccel: auto
output_options:
-pix_fmt: rgba64le
-vsync: "cfr"
Expand All @@ -22,6 +22,7 @@ ffmpeg:
-r: 15

pipe_video:
-hwaccel: auto
output_options:
-loglevel: panic
-y: -f
Expand Down
1 change: 1 addition & 0 deletions src/dandere2x/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from threading import Thread
from typing import Type

from dandere2x.dandere2x_logger import set_dandere2x_logger
from dandere2x.dandere2x_service.service_types.dandere2x_service_interface import Dandere2xServiceInterface
from dandere2x.dandere2x_service_request import Dandere2xServiceRequest, ProcessingType

Expand Down
1 change: 1 addition & 0 deletions src/dandere2x/dandere2x_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def set_dandere2x_logger(input_file_path: str) -> None:
handler.setFormatter(formatter)

logger = colorlog.getLogger(name=input_file_path)
logger.propagate = False
logger.setLevel(logging.INFO)
logger.addHandler(handler)
logger.info("Dandere2x Console Logger Set")
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
====================================================================="""
import logging
import os
import sys
import time
from abc import ABC, abstractmethod
from threading import Thread
Expand Down Expand Up @@ -60,7 +61,13 @@ def verify_upscaling_works(self) -> None:
self.log.info("Attempting to upscale file %s into %s to ensure waifu2x is working..."
% (test_file, test_file_upscaled))

self.upscale_file(test_file, test_file_upscaled)
try:
self.upscale_file(test_file, test_file_upscaled)

except:
e = sys.exc_info()[0]
self.log.info("Caught exception %s" % str(e))
raise Exception("Could not upscale first frame, see stacktrace for more details. ")

if not file_exists(test_file_upscaled):
self.log.error("Your computer could not upscale a test image, which is required for dandere2x to work.")
Expand Down
47 changes: 29 additions & 18 deletions src/dandere2x/dandere2x_service/core/waifu2x/waifu2x_ncnn_vulkan.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,41 @@ def repeated_call(self) -> None:
# override
def upscale_file(self, input_image: str, output_image: str) -> None:
exec_command = copy.copy(self.upscale_command)
console_output = open(self.context.console_output_dir + "vulkan_upscale_frames.txt", "w")
console_output_path = self.context.console_output_dir + "vulkan_upscale_frames.txt"

"""
note:
so waifu2x-ncnn-vulkan actually doesn't allow jpg outputs. We have to work around this by
simply renaming it as png here, then changing it to jpg (for consistency elsewhere) """
with open(console_output_path, "w") as console_output:
"""
note:
so waifu2x-ncnn-vulkan actually doesn't allow jpg outputs. We have to work around this by
simply renaming it as png here, then changing it to jpg (for consistency elsewhere)
"""

output_image = output_image.replace(".jpg", ".png")
output_image = output_image.replace(".jpg", ".png")

# replace the exec command with the files we're concerned with
for x in range(len(exec_command)):
if exec_command[x] == "[input_file]":
exec_command[x] = input_image
# replace the exec command with the files we're concerned with
for x in range(len(exec_command)):
if exec_command[x] == "[input_file]":
exec_command[x] = input_image

if exec_command[x] == "[output_file]":
exec_command[x] = output_image
if exec_command[x] == "[output_file]":
exec_command[x] = output_image

console_output.write(str(exec_command))
self.active_waifu2x_subprocess = subprocess.Popen(exec_command,
shell=False, stderr=console_output, stdout=console_output,
cwd=os.path.dirname(self.waifu2x_vulkan_path))
self.active_waifu2x_subprocess.wait()
console_output.write(str(exec_command))
self.active_waifu2x_subprocess = subprocess.Popen(exec_command,
shell=False, stderr=console_output, stdout=console_output,
cwd=os.path.dirname(self.waifu2x_vulkan_path))
self.active_waifu2x_subprocess.wait()

if not os.path.exists(output_image):
self.log.info("Could not upscale first frame: printing %s console log" % __name__)

with open(console_output_path) as f:
for line in f:
self.log.critical("%s", str(line))

raise Exception("Could not upscale file %s" % input_image)

rename_file_wait(output_image, output_image.replace(".png", ".jpg"))
rename_file_wait(output_image, output_image.replace(".png", ".jpg"))

# override
def _construct_upscale_command(self) -> list:
Expand Down
20 changes: 15 additions & 5 deletions src/dandere2x/dandere2xlib/wrappers/ffmpeg/ffmpeg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import subprocess
import sys

from dandere2x.dandere2xlib.utils.dandere2x_utils import get_a_valid_input_resolution, get_operating_system
from dandere2x.dandere2xlib.utils.yaml_utils import get_options_from_section
Expand All @@ -17,12 +18,18 @@ def re_encode_video(ffmpeg_dir: str, ffprobe_dir: str, output_options: dict, inp
if console_output:
assert type(console_output) == str

logger = logging.getLogger(__name__)
logger = logging.getLogger("root")
video_settings = VideoSettings(ffprobe_dir=ffprobe_dir, video_file=input_file)
frame_rate = video_settings.frame_rate

extract_frames_command = [ffmpeg_dir,
"-i", input_file]
extract_frames_command = [ffmpeg_dir]

# walrus operator go brrrr
if (hw_accel := output_options["ffmpeg"]["pre_process_video"]["-hwaccel"]) is not None:
extract_frames_command.append("-hwaccel")
extract_frames_command.append(hw_accel)

extract_frames_command.extend(["-i", input_file])

extract_frames_options = \
get_options_from_section(output_options["ffmpeg"]['pre_process_video']['output_options'],
Expand All @@ -35,7 +42,8 @@ def re_encode_video(ffmpeg_dir: str, ffprobe_dir: str, output_options: dict, inp
extract_frames_command.append(str(frame_rate))
extract_frames_command.extend([output_file])

process = subprocess.Popen(extract_frames_command, stdout=open(os.devnull, 'w'), stderr=subprocess.PIPE,
logger.warning("Re-encoding your video, this may take some time.")
process = subprocess.Popen(extract_frames_command, stdout=sys.stdout, stderr=sys.stdout,
stdin=subprocess.PIPE, shell=False)

stdout, stderr = process.communicate()
Expand Down Expand Up @@ -243,7 +251,7 @@ def migrate_tracks_contextless(ffmpeg_dir: str, no_audio: str, file_dir: str, ou
Add the audio tracks from the original video to the output video.
"""

print("migrate tracks called")
log = logging.getLogger("root")

# to remove
def convert(lst):
Expand All @@ -269,6 +277,8 @@ def convert(lst):

migrate_tracks_command.extend([str(output_file)])

log.info("Migrating tracks %s " % convert(migrate_tracks_command))

console_output = get_console_output(__name__, console_output_dir)

log.info("Writing files to %s" % str(console_output_dir))
Expand Down
9 changes: 8 additions & 1 deletion src/dandere2x/dandere2xlib/wrappers/ffmpeg/pipe_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ def _setup_pipe(self) -> None:
dar = self.context.video_settings.dar

# constructing the pipe command...
ffmpeg_pipe_command = [ffmpeg_dir, "-r", frame_rate]
ffmpeg_pipe_command = [ffmpeg_dir]

# walrus operator go brrrr
if (hw_accel := self.context.service_request.output_options["ffmpeg"]["pipe_video"]["-hwaccel"]) is not None:
ffmpeg_pipe_command.append("-hwaccel")
ffmpeg_pipe_command.append(hw_accel)

ffmpeg_pipe_command.extend(["-r", frame_rate])

options = get_options_from_section(
self.context.service_request.output_options["ffmpeg"]["pipe_video"]['output_options'],
Expand Down
7 changes: 6 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import time

from dandere2x import Dandere2x
from dandere2x import Dandere2x, set_dandere2x_logger
from dandere2x.dandere2x_service_request import Dandere2xServiceRequest
from dandere2x.dandere2xlib.utils.dandere2x_utils import show_exception_and_exit

Expand Down Expand Up @@ -31,6 +32,10 @@ def main():
import sys
sys.excepthook = show_exception_and_exit

# set the master logger at the highest level
set_dandere2x_logger("root")
logging.propagate = False

start = time.time()

if len(sys.argv) == 1:
Expand Down

0 comments on commit b659aa8

Please sign in to comment.