From 4e21253509a6c82e3107117141732d3fafbffac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:33:12 +0100 Subject: [PATCH 1/3] Postgresql Added optional support for Postgresql. SQLite is still supported and the default. --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7537c84..9f02858 100644 --- a/README.md +++ b/README.md @@ -18,21 +18,21 @@ ## Quick usage -1. Install the required Python 3 dependencies: `click`, `yaml` and `subprocess` (`sudo apt install python3-click python3-yaml python3-subprocess` in Debian). +1. Install the required Python 3 dependencies: `click`, `yaml` and `subprocess` (`sudo apt install python3-click python3-yaml python3-subprocess` in Debian) and optionally install `psycopg2` with `sudo apt install python3-psycopg2` for Postgresql support. -1. Create the directory `/etc/rffmpeg`. +2. Create the directory `/etc/rffmpeg`. -1. Copy the `rffmpeg.yml.sample` file to `/etc/rffmpeg/rffmpeg.yml` and edit it to suit your needs if required. +3. Copy the `rffmpeg.yml.sample` file to `/etc/rffmpeg/rffmpeg.yml` and edit it to suit your needs if required. -1. Install `rffmpeg` somewhere useful, for instance at `/usr/local/bin/rffmpeg`. +4. Install `rffmpeg` somewhere useful, for instance at `/usr/local/bin/rffmpeg`. -1. Create symlinks for the command names `ffmpeg` and `ffprobe` to `rffmpeg`, for example `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffmpeg` and `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffprobe`. +5. Create symlinks for the command names `ffmpeg` and `ffprobe` to `rffmpeg`, for example `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffmpeg` and `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffprobe`. -1. Initialize the database and add a target host, for example `sudo rffmpeg init && rffmpeg add myhost.domain.tld`. +6. Initialize the database and add a target host, for example `sudo rffmpeg init && rffmpeg add myhost.domain.tld`. -1. Set your media program to use `rffmpeg` via the `ffmpeg` symlink name created above, instead of any other `ffmpeg` binary. +7. Set your media program to use `rffmpeg` via the `ffmpeg` symlink name created above, instead of any other `ffmpeg` binary. -1. Profit! +8. Profit! `rffmpeg` does require a little bit more configuration to work properly however. For a comprehensive installation tutorial based on a reference setup, please see [the SETUP guide](SETUP.md). From 59456e502054c68cedf59682a8523215b0c1e9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:34:17 +0100 Subject: [PATCH 2/3] Postgresql Added optional support for Postgresql. SQLite is still supported and the default. --- rffmpeg | 70 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/rffmpeg b/rffmpeg index e532379..315f1c8 100755 --- a/rffmpeg +++ b/rffmpeg @@ -42,18 +42,50 @@ from datetime import datetime log = logging.getLogger("rffmpeg") +# Use Postgresql if specified, otherwise use SQLite +DB_TYPE = "SQLITE" +POSTGRES_HOST = os.getenv("POSTGRES_HOST") +POSTGRES_DB = os.getenv("POSTGRES_DB") +POSTGRES_USER = os.getenv("POSTGRES_USER") +POSTGRES_PASS = os.getenv("POSTGRES_PASS") +if POSTGRES_HOST != None and POSTGRES_DB != None and POSTGRES_USER != None: + DB_TYPE = "POSTGRES" + from psycopg2 import connect as postgres_connect + + # Open a database connection (context manager) @contextmanager def dbconn(config): """ Open a database connection. """ - conn = sqlite_connect(config["db_path"]) - conn.execute("PRAGMA foreign_keys = 1") - cur = conn.cursor() - yield cur - conn.commit() + if DB_TYPE == "SQLITE": + if not Path(config["db_path"]).is_file(): + fail( + "Failed to find database '{}' - did you forget to run 'rffmpeg init' or add all env vars for Postgresql?".format( + config["db_path"] + ) + ) + log.debug("Using SQLite as database.") + conn = sqlite_connect(config["db_path"]) + conn.execute("PRAGMA foreign_keys = 1") + cur = conn.cursor() + yield cur + conn.commit() + elif DB_TYPE == "POSTGRES": + try: + log.debug("Using Postgresql as database. Connecting...") + conn = postgres_connect() + cur = conn.cursor() + cur.execute('SELECT version()') + db_version = cur.fetchone() + log.debug("Connected to Postgresql version {}".format(db_version)) + yield cur + conn.commit() + except (Exception, psycopg2.DatabaseError) as error: + log.error(error) conn.close() + log.debug("Database connection closed.") def fail(msg): @@ -73,11 +105,22 @@ def load_config(): default_config_file = "/etc/rffmpeg/rffmpeg.yml" config_file = os.environ.get("RFFMPEG_CONFIG", default_config_file) - with open(config_file, "r") as cfgfh: - try: - o_config = yaml.load(cfgfh, Loader=yaml.SafeLoader) - except Exception as e: - fail("Failed to parse configuration file: {}".format(e)) + if not Path(config_file).is_file(): + log.info("No config found in {}. Using default settings.".format(config_file)) + o_config = { + "rffmpeg": { + "logging": {}, + "directories": {}, + "remote": {}, + "commands": {} + } + } + else: + with open(config_file, "r") as cfgfh: + try: + o_config = yaml.load(cfgfh, Loader=yaml.SafeLoader) + except Exception as e: + fail("Failed to parse configuration file: {}".format(e)) config = dict() @@ -839,13 +882,6 @@ if __name__ == "__main__": if "rffmpeg" in cmd_name: run_control(config) - else: - if not Path(config["db_path"]).is_file(): - fail( - "Failed to find database '{}' - did you forget to run 'rffmpeg init'?".format( - config["db_path"] - ) - ) ffmpeg_args = all_args[1:] run_ffmpeg(config, ffmpeg_args) From 933cfac6759e6593d9d3c2bc9fb97b86ea2287a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:57:17 +0100 Subject: [PATCH 3/3] Fix README --- README.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9f02858..89999ae 100644 --- a/README.md +++ b/README.md @@ -20,19 +20,19 @@ 1. Install the required Python 3 dependencies: `click`, `yaml` and `subprocess` (`sudo apt install python3-click python3-yaml python3-subprocess` in Debian) and optionally install `psycopg2` with `sudo apt install python3-psycopg2` for Postgresql support. -2. Create the directory `/etc/rffmpeg`. +1. Create the directory `/etc/rffmpeg`. -3. Copy the `rffmpeg.yml.sample` file to `/etc/rffmpeg/rffmpeg.yml` and edit it to suit your needs if required. +1. Copy the `rffmpeg.yml.sample` file to `/etc/rffmpeg/rffmpeg.yml` and edit it to suit your needs if required. -4. Install `rffmpeg` somewhere useful, for instance at `/usr/local/bin/rffmpeg`. +1. Install `rffmpeg` somewhere useful, for instance at `/usr/local/bin/rffmpeg`. -5. Create symlinks for the command names `ffmpeg` and `ffprobe` to `rffmpeg`, for example `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffmpeg` and `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffprobe`. +1. Create symlinks for the command names `ffmpeg` and `ffprobe` to `rffmpeg`, for example `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffmpeg` and `sudo ln -s /usr/local/bin/rffmpeg /usr/local/bin/ffprobe`. -6. Initialize the database and add a target host, for example `sudo rffmpeg init && rffmpeg add myhost.domain.tld`. +1. Initialize the database and add a target host, for example `sudo rffmpeg init && rffmpeg add myhost.domain.tld`. -7. Set your media program to use `rffmpeg` via the `ffmpeg` symlink name created above, instead of any other `ffmpeg` binary. +1. Set your media program to use `rffmpeg` via the `ffmpeg` symlink name created above, instead of any other `ffmpeg` binary. -8. Profit! +1. Profit! `rffmpeg` does require a little bit more configuration to work properly however. For a comprehensive installation tutorial based on a reference setup, please see [the SETUP guide](SETUP.md). @@ -128,8 +128,9 @@ This depends on what "layer" you're asking at. * Operating Systems (source): Debian and its derivatives (Ubuntu, Linux Mint, etc.) should all work perfectly; other Linux operating systems should work fine too as the principles are the same; MacOS should work since it has an SSH client built in; Windows will not work as `rffmpeg` depends on some POSIX assumptions internally. * Operating Systems (target): Any Linux system which [`jellyfin-ffmpeg`](https://github.com/jellyfin/jellyfin-ffmpeg) supports, which is currently just Debian and Ubuntu; Windows *might* work if you can get an SSH server running on it (see [Issue #17](https://github.com/joshuaboniface/rffmpeg/issues/17)). * Install Methods for Jellyfin: Native packages/installers/archives are recommended; a set of [Jellyfin Docker containers integrating `rffmpeg`](https://github.com/Shadowghost/jellyfin-rffmpeg) has been created by [@Shadowghost](https://github.com/Shadowghost) as well as [another](https://github.com/aleksasiriski/jellyfin-rffmpeg) by [@aleksasiriski](https://github.com/aleksasiriski). In addition to these special docker images you can use linuxserver's image with [this mod](https://github.com/linuxserver/docker-mods/tree/jellyfin-rffmpeg). -* Install Methods for `rffmpeg`: Direct installation is recommended; a [Docker container to act as an ffmpeg transcode target](https://github.com/BasixKOR/rffmpeg-docker) has been created by [@BasixKOR](https://github.com/BasixKOR) as well as [another](https://github.com/aleksasiriski/rffmpeg-node) by [@aleksasiriski](https://github.com/aleksasiriski). -* Cloud: [HCloud Rffmpeg](https://github.com/aleksasiriski/hcloud-rffmpeg) script made to read rffmpeg database and spin up more transcode nodes in Hetzner Cloud by [@aleksasiriski](https://github.com/aleksasiriski). +* Install Methods for `rffmpeg`: Direct installation is recommended; a [Docker container to act as an ffmpeg transcode target](https://github.com/aleksasiriski/rffmpeg-worker) has been created by [@aleksasiriski](https://github.com/aleksasiriski) as well as [another](https://github.com/BasixKOR/rffmpeg-docker) by [@BasixKOR](https://github.com/BasixKOR). +* Cloud: [HCloud Rffmpeg](https://github.com/aleksasiriski/hcloud-rffmpeg) script made to read rffmpeg database and spin up more transcode nodes in Hetzner Cloud. +* Kubernetes: A short guide and example yaml files are available [here](https://github.com/aleksasiriski/rffmpeg-worker/tree/main/Kubernetes). ### Can `rffmpeg` mangle/alter FFMPEG arguments? @@ -145,7 +146,7 @@ Thus it is imperative that you set up your entire system correctly for `rffmpeg` ### Can `rffmpeg` do Wake-On-LAN or other similar options to turn on a transcode server? -Right now, no. I've thought about implementing this more than once (most recently, in response to [Issue #21](https://github.com/joshuaboniface/rffmpeg/issues/21)) but ultimately I've never though this was worth the complexity and delays in spawning that it would add to the tool. That issue does provide one example of a workaround wrapper script that could accomplish this, but I don't see it being a part of the actual tool itself. +Right now, not officially. You can use the linuxserver.io [docker mod](https://github.com/linuxserver/docker-mods/tree/jellyfin-rffmpeg). I've thought about implementing this more than once (most recently, in response to [Issue #21](https://github.com/joshuaboniface/rffmpeg/issues/21)) but ultimately I've never though this was worth the complexity and delays in spawning that it would add to the tool. That issue does provide one example of a workaround wrapper script that could accomplish this, but I don't see it being a part of the actual tool itself. ### I'm getting an error, help!