Created
March 3, 2024 16:00
-
-
Save ergosteur/df4e5bcf7ce7265294f56cf7733e088e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# dockerized-ffmpeg-drop-in | |
# version 0.3 | |
# https://gist.github.com/ergosteur | |
# ** Partially AI-generated code via ChatGPT | |
# | |
# | |
# This is a shell script to use dockerized ffmpeg (linuxserver/ffmpeg) as a drop-in replacement for the 'ffmpeg' command | |
# Place the script somewhere in your PATH (e.g. ~/.local/bin) and rename it 'ffmpeg' | |
# Tested on Synology DSM 7.2.1 and Ubuntu 22.04/Docker CE 25.0.3 | |
# Check https://github.com/linuxserver/docker-ffmpeg for usage information, ffmpeg version, and options for hardware acceleration. | |
# | |
# This script will pull the latest linuxserver/ffmpeg docker image upon first run if it is not found locally, | |
# however it will not automatically update the image, as per default docker behaviour. | |
# | |
# To update the image manually so it can be used by this script, run `docker pull linuxserver/ffmpeg`. | |
# | |
# ALLOWED_PATH_PREFIX variable: | |
# Set this to the base directory you want to allow for absolute paths. | |
# SECURITY WARNING: the path specified here will be mounted into the Docker container. The container will have full access to all files in the path. | |
# | |
# Note: On Synology, you must also allow Docker read/write access to the desired Shared Folders in the DSM Control Panel | |
# | |
ALLOWED_PATH_PREFIX="/volume1" | |
# Function to check if the path is valid | |
check_path() { | |
# Check for absolute paths not starting with the allowed prefix | |
if [ "${1#$ALLOWED_PATH_PREFIX}" = "$1" ] && [ "${1#/}" != "$1" ]; then | |
echo "Invalid path: $1. Absolute paths must start with $ALLOWED_PATH_PREFIX." | |
exit 1 | |
fi | |
# Check for relative paths starting with .. | |
if [ "${1#..}" != "$1" ]; then | |
echo "Invalid path: $1. Relative paths must not start with '..'." | |
exit 1 | |
fi | |
} | |
# Loop through all arguments to check for invalid paths | |
for arg in "$@"; do | |
if [ "$arg" = "-i" ]; then | |
next_is_file=true | |
elif [ "$next_is_file" = true ]; then | |
check_path "$arg" | |
next_is_file=false | |
else | |
check_path "$arg" | |
fi | |
done | |
# Determine whether to mount the current working directory or the allowed absolute path | |
if [ "${1#/}" = "$1" ]; then | |
MOUNT_PATH="$(pwd)" | |
else | |
MOUNT_PATH=$ALLOWED_PATH_PREFIX | |
fi | |
# Print Mount path used | |
echo "Mounting $MOUNT_PATH in Docker container..." | |
# Execute the Dockerized ffmpeg command | |
docker run --rm -e LANG=C.UTF-8 -v "$MOUNT_PATH:$MOUNT_PATH" -w "$MOUNT_PATH" linuxserver/ffmpeg "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment