0

tl;dr how can I substitute a string (i.e. a youtube/streaming url) in an alias such as alias mpvyt='yt-dlp <URL> -o - | mpv - ' ?

I live in a mezzanine studio using a M1 macbook for a workstation (writing/editing) and my old laptop with openbsd as a local server. I play music from that obsd server upstairs, which thus fills the whole room down to my desktop through the plugged-in speakers. My hosted library plays fine with mpd and ncmpcpp, and I just figured out it's not so difficult to use mpv to play streamed youtube videos, since firefox in XQuartz streaming from xenocara is way too slow anyhow.

I use a variety of different alias**[1]** as shortcuts to frequent commands, and understand the basics of programming but am an absolute beginner when it comes to shell/ksh88 syntax. I would like to use a short command for streaming youtube videos from the obsd server which I connect to using ssh. The following : yt-dlp <URL> -o - | mpv - -force-seekable=yes works well but how can I do something like alias mpvyt='yt-dlp %s -o - | mpv -' <URL> so that I don't have to type the full command ?

I suppose it involves a basic string substitution using the ksh88 syntax which I am clueless about ... or perhaps I need to go as “far” as to use a little standalone shell script a.k.a snippet ? To go further, a great bonus would be if I could stream playlists, as well as separate the video and audio streams so I can play the video on the workstation screen, I will look into the mpv documentation for this.

[1] (you may skip this irrelevant block of code, I just copy-pasted my terminal output to brag about my network and aliases)

Last login: Tue Oct 31 06:54:34 on ttys002
sylvain@sylvainmac ~ % more /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

#192.168.1.254  box
192.168.1.1     srv

#192.168.1.11   lap
192.168.1.21    mac

192.168.1.51    droid
sylvain@sylvainmac ~ % grep alias .zprofile
alias u='brew update && brew upgrade ; sudo port selfupdate && sudo port upgrade outdated' #update command
alias z='ncmpcpp'
alias srvsh='ssh sylvain@srv'
alias srvx='ssh -X sylvain@srv' #used from xquartz, deprecated
alias srvm='sshfs sylvain@srv: ~/srv'
alias sic='sic -n sylvainsab'
alias l='ls'
alias la='ls -a'
alias ll='ls -l'
alias lla='ls -la'
sylvain@sylvainmac ~ % srvsh
sylvain@srv password: 
Last login: Tue Oct 31 06:56:06 2023 from 192.168.1.21
OpenBSD 7.4 (GENERIC.MP) #0: Sun Oct 22 12:13:42 MDT 2023

Welcome to OpenBSD: The proactively secure Unix-like operating system.

Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code.  With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.

$grep alias .kshrc  
alias l='ls -F'
alias ll='ls -l'
alias la='ls -a'
alias lla='ls -Al'
alias t='tmux attach'
alias z='ncmpcpp'
alias fxr='firefox-esr -P remote &' #used with xquartz, deprecated
alias n='doas umount /mnt'
alias u='doas pkg_add -Uu && doas syspatch && doas fw_update' #update command
alias macm='doas sshfs -o idmap=user,allow_other,uid=1000,gid=1000 sylvain@mac: ~/mac'
alias rmds='find . -iname *.DS_Store -exec rm {} \;'
alias mic_on='doas sysctl kern.audio.record=1'
alias mic_off='doas sysctl kern.audio.record=0'
alias cam_on='doas sysctl kern.video.record=1'
alias cam_off='doas sysctl kern.video.record=0'
alias ytdl='yt-dlp --prefer-free-formats -f bestvideo+bestaudio --audio-quality 0 -o "%(title)s-%(id)s.%(ext)s"'  
$^D
Connection to srv closed.
sylvain@sylvainmac ~ % 

Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Opération terminée]
4
  • 1
    You don't. You use functions instead.
    – muru
    Commented Oct 31, 2023 at 7:08
  • I does, thank you. I had already used this but wasn't sure about the syntax and whether alias could handle arguments (which the answer you linked says it doesn't). Worked with a simple line in ~/.kshrc
    – sylvainsab
    Commented Oct 31, 2023 at 7:34
  • Not a direct answer to the question itself, but I'm able to play YouTube videos (and from other "yt-dlp compatible" sites) simply by passing the URL as an argument to mpv (quoted, preferably, but not required for simple ?v=... URLs). mpv calls yt-dlp by itself, in the background. Have you tried it?
    – Zé Loff
    Commented Oct 31, 2023 at 8:42
  • ... indeed. Why make things simple when they can stay complicated 🙄 Thank you very much !
    – sylvainsab
    Commented Oct 31, 2023 at 12:55

1 Answer 1

0
$sed -n 9p ~/.kshrc
mpvyt () { yt-dlp "$1" -o - | mpv - --force-seekable=yes ; }
$mpvyt youtu.be/8I_vQHbmmaY 
...

Works like a charm ! It was simpler than I thought. Thank you, problem solved.

Not the answer you're looking for? Browse other questions tagged .