initial commit

This commit is contained in:
Denis Lehmann 2021-04-25 02:06:26 +02:00
commit 8c91d525aa
5 changed files with 299 additions and 0 deletions

76
README.org Normal file
View file

@ -0,0 +1,76 @@
* strm
*strm* is a small Bash script that lets you stream media files over SSH.
No need for mounting via SSHFS and navigating through folders anymore.
Just provide a query and the media files are played locally via [[https://mpv.io/][mpv]].
** Features
- List remote files by query
- Play remote flies by query via mpv
** Installation
Make sure, mpv is installed and that you have access to a remote machine via SSH.
strm is just a script, you can execute it directly.
Or grab it and place it in you =$PATH=.
This project is also a [[https://nixos.wiki/wiki/Flakes][Nix flake]].
You can execute it with the following command if you have a recent version of [[https://nixos.org/][Nix]] installed and flakes are enabled:
#+begin_src sh
nix run github:Deleh/strm -- --help
#+end_src
If you use it this way, mpv is started with *MPRIS* support by default.
** Usage
#+begin_src text
Usage: strm [OPTIONS] QUERY ... [OPTIONS]
Stream media over SSH.
OPTIONS
-h, --help Show this help message
-c, --config CONFIG_FILE Path to config file
-l, --list List files instead of playing
-s, --shuffle Play files in random order
EXAMPLES
strm -l . # List all available files
strm Elephants Dream # Play files whose path contain 'elephants' and 'dream' in order
strm e*phants # Play files whose path matches the wildcard 'e*phants'
#+end_src
** Configuration
The scripts expects a config file with the following content:
#+begin_src sh
# SSH connection string
# Examples:
# remote # Current user at hostname 'remote' on port 22
# user@10.0.0.1 # 'user' at address '10.0.0.1' on port 22
# user@10.0.0.1:600 # 'user' at address '10.0.0.1' on port 600
connection_string=""
# Absolute path to media directory on remote machine
# Example:
# /home/<user>/video
media_directory=""
#+end_src
An example configuration file can be found in =./strm.config=.
The default path of the configuration is =$HOME/.config/strm/strm.config=.
You can use the =--config= flag to set another configuration path.
This is useful if you are using multiple remote machines or different media folders.
One can set e.g. aliases for different streaming resources:
#+begin_src sh
alias mstrm="strm -c <path_to_music_config>"
alias vstrm="strm -c <path_to_video_config>"
#+end_src

41
flake.lock generated Normal file
View file

@ -0,0 +1,41 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1618868421,
"narHash": "sha256-vyoJhLV6cJ8/tWz+l9HZLIkb9Rd9esE7p+0RL6zDR6Y=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "eed214942bcfb3a8cc09eb3b28ca7d7221e44a94",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1619304191,
"narHash": "sha256-m9Yhl86aMYjwjA1obKFoyCVZygWme/kVOd29GfHFB3A=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9839cda8dc0eae5fd2af4ffc970c95ebaa59b92c",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

49
flake.nix Normal file
View file

@ -0,0 +1,49 @@
{
description = "Stream media over SSH.";
nixConfig.bash-prompt = "\[strm-develop\]$ ";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
# Use mpv with scripts
mpv = (pkgs.mpv-with-scripts.override {
scripts = [
pkgs.mpvScripts.mpris
];
});
in
{
# Package
packages.strm =
pkgs.stdenv.mkDerivation {
name = "strm";
src = self;
patchPhase = with pkgs; ''
substituteInPlace strm \
--replace mpv ${mpv}/bin/mpv \
'';
installPhase = ''
install -m 755 -D strm $out/bin/strm
'';
};
defaultPackage = self.packages.${system}.strm;
# Development shell
devShell = pkgs.mkShell {
buildInputs = [
mpv
];
};
}
);
}

122
strm Executable file
View file

@ -0,0 +1,122 @@
#!/usr/bin/env bash
function print_usage {
echo "Usage: strm [OPTIONS] QUERY ... [OPTIONS]"
echo
echo "Stream media over SSH."
echo
echo "OPTIONS"
echo " -h, --help Show this help message"
echo " -c, --config CONFIG_FILE Path to config file"
echo " -l, --list List files instead of playing"
echo " -s, --shuffle Play files in random order"
echo
echo "EXAMPLES"
echo " strm -l . # List all available files"
echo " strm Elephants Dream # Play files whose path contain 'elephants' and 'dream' in order"
echo " strm e*phants # Play files whose path matches the wildcard 'e*phants'"
exit 0
}
# Check if mpv is installed
if ! command -v mpv &> /dev/null
then
echo "mpv was not found, please install it"
exit 1
fi
# Set default config path
config="$HOME/.config/strm/strm.config"
shuffle=false
# Parse arguments
list=false
for arg in "$@"; do
case $arg in
-c|--config)
config="$1"
shift
;;
-h|--help)
print_usage
;;
-l|--list)
list=true
shift
;;
-s|--shuffle)
shuffle=true
shift
;;
*)
query+=("$1")
shift
;;
esac
done
# Read config file
if test -f "$config"; then
. "$config"
else
echo "Config file not found ($config)"
echo "Please create it or set one with the --config flag"
exit 1
fi
# Check validity of variables
config_valid=true
if [ "$connection_string" == "" ]; then
echo "Connection string is missing"
config_valid=false
fi
if [ "$media_directory" == "" ]; then
echo "Media directory is missing"
config_valid=false
else
# Check if media directory has trailing slash and add it if missing
[[ "$media_directory" != */ ]] && media_directory="$media_directory/"
fi
if [ "$config_valid" == false ]; then
echo "Please check your config file ($config)"
exit 1
fi
if [ "${#query[@]}" == 0 ]; then
print_usage
fi
# Build search string from input arguments
query="\*$( IFS=$'*'; echo "${query[*]}" )\*"
# Get search results from remote
# Look for paths matching given queries in visible directories, listing only filenames and links
mapfile -t results < <(ssh "$connection_string" find "$media_directory" -not -path \"*/\.*\" -type l,f -ipath "$query" | sort)
# List files
if [ "$list" == false ]; then
if [ "$shuffle" == true ]; then
echo -e "Playing the following files in random order:\n"
else
echo -e "Playing the following files:\n"
fi
fi
for i in "${!results[@]}"; do
res="${results[$i]}"
echo "${res/$media_directory/}"
done
# Play results if --list flag not set
if [ "$list" == false ]; then
# Build SFTP strings
for i in "${!results[@]}"; do
results["$i"]="sftp://$connection_string${results[$i]}"
done
# Play all remote files
if [ "$shuffle" == true ]; then
mpv --no-terminal --shuffle "${results[@]}"
else
mpv --no-terminal "${results[@]}"
fi
fi

11
strm.config Normal file
View file

@ -0,0 +1,11 @@
# SSH connection string
# Examples:
# remote # Current user at hostname 'remote' on port 22
# user@10.0.0.1 # 'user' at address '10.0.0.1' on port 22
# user@10.0.0.1:600 # 'user' at address '10.0.0.1' on port 600
connection_string="green-velvet.home"
# Absolute path to media directory on remote machine
# Example:
# /home/<user>/video
media_directory="/home/denis/video"