Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Real-ESRGAN, plus various fixes (hangs, async video, etc.) #1133

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

arximboldi
Copy link

@arximboldi arximboldi commented Jun 29, 2024

This picks up the work in #1102 by @aa-ko to introduce Real-ESRGAN support, adding support for all models exposed by realesrgan-ncnn-py and testing it outside of a Docker container.

Additionally, I have fixed a few issues that I've found. I wasn't sure whether you prefer to have multiple smaller PR, or to just review all at once. The work is separated into multiple commits so I can still split into multiple PR's if you prefer.

These are the highlights from this PR:

There are also some smaller changes that I'm not sure you agree with, but that I think are quite convenient:

  • A shell.nix environment that allows installing all the system dependencies in an isolated environment, by simply running nix-shell. This was crucial for me to be able to run and test the program locally outside of a Docker container
  • Minor improvements to the logging system (proper acknowledgement of the -l flag, and introduction of a new -L flag).

Thank you @aa-ko for starting the work on integrating Real-ESRGAN. It's model realesr-animevideov3 is giving me incredible results, with image quality comparable or better for anime than realcugan, yet much faster!

Thank you @k4yt3x for this incredible tool. Looking into the code has shown me how much love there is into it and it is working super well for me now!

aa-ko and others added 4 commits February 21, 2024 15:50
Choosing realesrgan ignores noise flag for now and always uses the realesrgan-x4plus model.
A few scalers output a lot of crap like:
   10%
   25%
   50%
   ...

That messes with the progress bar indicator.
@arximboldi arximboldi changed the title Add support for Real-ESRGAN, and fix hang and async videos, other minor improvements Add support for Real-ESRGAN, plus various fixes (hangs, async video, etc.) Jun 29, 2024
This allows a reproducible way to get all the needed dependencies and
allows running the software in NixOS environments.
This solves a couple of issues:

1. The log level passed with -l was not properly applied to loguru, it
   was always using `debug`, the default.

2. When actually passing `-l debug` ffmpeg would flood us with too
   much information, it is better to have a separate option to debug
   ffmpeg issues.
This can actually lead to corrupted videos, or with bad
synchronization or without skipping support.  This option seems to do
the right thing for this and is inocuous in our case.
...this would cause the whole application to hang during the teardown
sequence of the application.
Since now these are only printed when passing -l debug, I think this
is an acceptable compromise and can help debug the various problems
that have existed with the application hanging
This problem used to be more severe, but has become less frequent with
our fix for k4yt3x#1132

The problem happens because we used to take `frame_count` as an
absolute truth, and we would iterate until we had processed that many
frames.  However, as we've learnt, this is just an estimate: it can
happen that the `Decoder` thread is done before we hit that frame
count.

With this change, we detect that scenario, and gracefully finish the
rendering process, making sure that all pending frames have been read
and processed and get written in the final stream.
Some anime files in particular like to include custom fonts and stuff
like that in these streams. I think it is useful to keep them as to
keep the generated file as close to the original as possible.
@twardoch
Copy link

twardoch commented Sep 1, 2024

@arximboldi

class KillableListener(Listener, KillableThreadMixin):
    """
    A killable version of pyinput.keyboard.Listener, as joining()
    seems to hang on some systems even after properly calling close().
    """
    pass

in https://github.com/arximboldi/video2x/blob/realesrgan/video2x/video2x.py#L131C1-L136C9 causes a fail where pynput is not available. Because Python tries to subclass from Listener. You should define that class in a scoped way.

@twardoch
Copy link

twardoch commented Sep 1, 2024

@arximboldi Also when I try running with realesrgan on Google Colab, I get

Unrecognized option 'fps_mode'. Error splitting the argument list: Option not found

(which seems to be referenced in https://github.com/arximboldi/video2x/blob/realesrgan/video2x/decoder.py )

@twardoch
Copy link

twardoch commented Sep 1, 2024

Turns out that 'fps_mode' is a relatively new option in ffmpeg, and the standard Google Colab environment installs an older ffmpeg. But I was able to work around it with:

# Install system dependencies
!apt-get update
!apt-get install -y --no-install-recommends \
    python3-pip libvulkan-dev glslang-dev glslang-tools \
    build-essential swig ninja-build nvidia-driver-535 \
    mpv xserver-xorg-video-dummy xvfb libomp5
!sudo curl -L https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz -o /usr/local/bin/ffmpeg.tar.xz

%cd /usr/local/bin/
!7z e /usr/local/bin/ffmpeg.tar.xz -aoa
!7z e /usr/local/bin/ffmpeg.tar -aoa
!sudo chmod a+rx /usr/local/bin/ffmpeg

@twardoch
Copy link

twardoch commented Sep 1, 2024

Similarly, to work around the poor integration of Listener, I was able to work around it in Google Colab by providing:

%env DISPLAY=:0
class Listener:
    pass

from video2x import Video2X
video2x = Video2X()

But that’s not how it should work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants