Allow rffmpeg to determine if a host is "bad", i.e. if the SSH connection times out or fails, for instance due to an unreachable host down for maintenance. In such a case, instead of exiting abruptly, we will mark the host as "bad" in the current state file, and then retry the whole process, skipping the bad hosts. If other rffmpeg processes start while the current one is still running, they will also treat the host as "bad", until the original process ends at which point the statefile marking it bad will be removed and it will be retried by future processes. This helps ensure that redundancy of transcode hosts can actually be achieved, as before if the first host was down, the process would simply fail, retry, and then fail again if the down host was first in the list. This required some major refactoring of the code, including functionalizing various elements of the process as well as adding an infinite loop to the main execution in order to keep looping through hosts after marking one as bad. |
||
|---|---|---|
| LICENSE | ||
| README.md | ||
| rffmpeg.py | ||
| rffmpeg.yml.sample | ||
rffmpeg
rffmpeg is a remote FFmpeg wrapper used to execute FFmpeg commands on a remote server via SSH. It is most useful in situations involving media servers such as Jellyfin (our reference user), where one might want to perform transcoding actions with FFmpeg on a remote machine or set of machines to better handle the load.
Usage
-
Install the required Python 3 dependencies:
yamlandsubprocess. -
Create the directory
/etc/rffmpeg. -
Copy the
rffmpeg.yml.samplefile to/etc/rffmpeg/rffmpeg.ymland edit it to suit your needs. -
Install
rffmpeg.pysomewhere useful, for instance at/usr/local/bin/rffmpeg.py. -
Create symlinks for the command names
ffmpegandffprobetorffmpeg.py, for instance/usr/local/bin/ffmpeg -> /usr/local/bin/rffmpeg.pyand/usr/local/bin/ffprobe -> /usr/local/bin/rffmpeg.py. -
Edit your media program to use the
rffmpeg.pybinary (via the symlink names) instead of the standardffmpegbinary. -
Profit!
rffmpeg options and caveats
Remote hosts
rffmpeg supports setting multiple hosts. It keeps state in /run/shm/rffmpeg, of all running processes. These state files are used during rffmpeg's initialization in order to determine the optimal target host. rffmpeg will run through these hosts sequentially, choosing the one with the fewest running rffmpeg jobs. This helps distribute the transcoding load across multiple servers, and can also provide redundancy if one of the servers is offline - rffmpeg will detect if a host is unreachable and set it "bad" for the remainder of the run, thus skipping it until the process completes.
Terminating rffmpeg
When running rffmpeg manually, do not exit it with Ctrl+C. Doing so will likely leave the ffmpeg process running on the remote machine. Instead, enter q and a newline ("Enter") into the rffmpeg process, and this will terminate the entire command cleanly. This is the method that Jellyfin uses to communicate the termination of an ffmpeg process.
Full setup guide
This example setup is the one I use for rffmpeg, involving a media server (jf1) and a remote transcode server (gpu1). Both systems run Debian GNU/Linux, though the commands below should also work on Ubuntu. Note that Docker is not officially supported with rffmpeg due to the complexity of exporting Docker volumes with NFS, the path differences, and the fact that I don't use Docker, but if you do figure it out a PR is welcome.
-
Prepare the media server (
jf1) with Jellyfin using the standard.debinstall. Make note of main Jellyfin data path; it's usually/var/lib/jellyfinunless you change it. Note that if you change this path, or put the various subdirectories such as thetranscodesordata/subtitlesdirectories elsewhere, you may need to alter the NFS steps below to accommodate this. -
On the media server, create an SSH keypair owned by the Jellyfin service user; save this SSH key somewhere readable to the service user:
sudo -u jellyfin mkdir -p /var/lib/jellyfin/.ssh && sudo -u jellyfin ssh-keygen -t rsa -f /var/lib/jellyfin/.ssh/id_rsa. -
Copy (or symlink) the new SSH public key created in the previous step to
authorized_keys; this will be used later when the Jellyfin data directory is mounted on the transcode server:sudo -u jellyfin cp -a /var/lib/jellyfin/.ssh/id_rsa.pub /var/lib/jellyfin/.ssh/authorized_keys -
Install the rffmpeg program as detailed in the above section, including creating the
/etc/rffmpeg/rffmpeg.ymlconfiguration file and symlinks. -
Install the NFS kernel server:
sudo apt -y install nfs-kernel-server -
Export your Jellyfin data path found in step 1 with NFS; you will need to know the local IP address of the transcode server(s) (e.g.
10.0.0.100) to lock this down; alternatively, use your entire local network range (e.g.10.0.0.0/24), though this is not recommended for security reasons:echo '/var/lib/jellyfin 10.0.0.100/32(rw,sync,no_subtree_check)' | sudo tee -a /etc/exports && sudo systemctl restart nfs-kernel-server -
On the transcode server, install any required tools or programs to make use of hardware transcoding; this is optional if you only use software (i.e. CPU) transcoding.
-
Install the
jellyfin-ffmpegpackage to provide an FFmpeg binary; follow the Jellyfin installation instructions for details on setting up the Jellyfin repository, though install onlyjellyfin-ffmpeg. -
Install the NFS client utilities:
sudo apt install -y nfs-common -
Create a user for rffmpeg to SSH into the server as. This user should match the
jellyfinuser on the media server in every way, including UID (id jellyfinon the media server), home path, and groups. -
Ensure that the Jellyfin data directory exists at the same location as on the media server; create it if required, and set it immutable to prevent unintended writes:
sudo mkdir -p /var/lib/jellyfin && sudo chattr +i /var/lib/jellyfin -
Mount the media server NFS data share at the same directory on the transcode server:
echo 'jf1:/var/lib/jellyfin /var/lib/jellyfin nfs defaults,vers=3,sync 0 0' | sudo tee -a /etc/fstab && sudo mount -a -
Mount your media directory on the transcode server at the same location as on the media server and using the same method; if your media is local to the media server, export it with NFS similarly to the data directory.
-
On the media server, attempt to SSH to the transcode server as the
jellyfinuser using the key from step 2; this both tests the connection as well as saves the transcode server SSH host key locally:sudo -u jellyfin ssh -i /var/lib/jellyfin/.ssh/id_rsa jellyfin@gpu1 -
Verify that rffmpeg itself works by calling its
ffmpegalias with the-versionoption:sudo -u jellyfin /usr/local/bin/ffmpeg -version -
In Jellyfin, set the rffmpeg binary, via its
ffmpegsymlink, as your "FFmpeg path" in the Playback settings; optionally, enable any hardware encoding you configured in step 7. -
Try running a transcode and verifying that the
rffmpegprogram works as expected. The flow should be:-
Jellyfin calls rffmpeg with the expected arguments.
-
FFmpeg begins running on the transcode server.
-
The FFmpeg process writes the output files to the NFS-mounted temporary transcoding directory.
-
Jellyfin reads the output files from the NFS-exported temporary transcoding directory and plays back normally.
-
-
rffmpegwill also be used during other tasks in Jellyfin that requireffmpeg, for instance image extraction during library scans and subtitle extraction.