Heres how to listen to music on Youtube using the music player daemon,
the w3m browser and a little shell script i wrote called pinch
We use the w3m command line browser and set the pinch script as an external browser,
which we can use run using a keyboard combination in w3m
the pinch script accepts a youtube url and uses youtube-dl to grab the best audio stream,
then uses mpc to send the audio stream to mpd and play it
I know there are projects like mpsyt
but unfortunately you now need to create your own google api key to use with mpsyt
Video demo
You need the latest version of youtube-dl installed with pip,
because the freebsd pkg isnt as up to date
Current version of youtube-dl installed with pip:
2020.05.03
Freebsd pkg:
youtube_dl-2020.01.24
You dont need to uninstall youtube-dl package if you have it installed,
when we set up youtube-dl with pip programs that use it will use the pip version
Install required packages
+ Install music player daemon, mpc and ncmpc and python37 and py37-pip and w3m
install youtube_dl with pip
add the python bin to your shell path
add the following code to your ~/.bashrc if uisng bash,
or to your ~/.zshrc file if your are using the zsh shell
+ source your ~/.bashrc if you are using the bash shell
+ or source your ~/.zshrc file in using the zsh shell
+ install youtube_dl with pip
when you need to upgrade youtube_dl you would run the following code
music player daemon set up
edit the music player config file and add the location of your music directory
replace vi the editor of your choice
edit the musicpd.conf and replace username with your username if the example below
+ Create the ~/.mpd and ~/.mpd/playlists directories
+ Create the database,log,pid,state,sticker.sql, and socket
I wrote a forum post here with a youtube video about setting up bit perfect audio here
Set up your soundcard
List audio devices
the output from my set up with pcm5 as the default output audio device
loader.conf
The next step is to load the audio kernel modules in our /boot/loader.conf
edit you /boot/loader.conf
replace vi with the editor of your choice
we need to load the audio kernel
by adding snd_hda_load="YES" to /boot/loader.conf
i have a usb audio dac so i need to add snd_uaudio_load="YES"
to /boot/loader.conf
you only need to add snd_uaudio_load="YES" if you are using a usb audio device
sysctl.conf
We set the default audio output device in /etc/sysctl.conf
check your audio devices by running
in my case the device i want to use as the default audio device is pcm5
edit your /etc/sysctl.conf
replace vi with the editor of your choice
set the default audio output device
and enable bitperfect audio which also requires setting the vchans to 0 on the audio device we are setting up
in the above example i was setting pcm5 as the default output device
if you wanted to set up pcm2 as the default the config would look like this
You can also set the default audio device and bitperfect audio with the sysctl command
rc.conf
Starting the music player daemon service
To start the music player daemon automatically at login we need to add
musicpd_enable="YES" to our /etc/rc.conf file
you can either edit the rc.conf file manually
or use the sysrc command
to manually edit the /etc/rc.conf
edit the file with vi or the text editor of your choice
and then add the following code and save the file
or you can use the sysrc command
ncmpc
Setting up the ncmpc mpd client
or you can use any other mpd of your choice
+ create the ncmpc directory in your home
+ copy the ncmpc sample config file to the ~/.ncmpc directory you just created
+ edit the ~/.ncmpc config file and add the location of the mpd socket, replace vi with whatever editor you want
set the mpd host to the location of socket file
replace username with your username
Create a bin directory in your home
The next step is to create a bin directory in your home if you dont have one
if you already have a bin directory set up and added to your shell path you can skip this step
next you have to add the path to the bin directory to your shell path,
by edting your shells config file like your ~/.bashrc for bash or ~/.zshrc file for the zsh shell
+ bash
+ zsh
replace vi with whatever editor you want in the above example
then add the following code to add the bin directory to your shell path and save the file
+ you then need to source your ~/.bashrc file if you are using bash
+ or if you are using the zsh shell then need to source your ~/.zshrc file
Pinch shell script
the pinch shell script takes a youtube url and use youtube-dl to grab best quality audio stream from the video,
and then use mpc to send the url to mpd and play it
pinch shell script on github
+ save the code below into a file called pinch into your ~/bin directory
+ make the script executable
you can run the pinch script on the command line
with the url on single quotes
w3m browser
The next step is to set up the pinch script as an external browser with the w3m browser,
this will let us browse youtube on the command line and press a keyboard short cut either on a link in a playlist,
or on the videos page and then send the audio to mpd and play it
+ edit the ~/.w3m/keymap file or create it to set the external browser shortcut key
replace vi with the editor of your choice in the above example
and then add the following code to the ~/.w3m/keymap file and save the file
+ edit the ~/.w3m/bookmark.html file or create it if it doesnt exist
replace vi with the editor of your choice in the above example
then add the following code to the ~/.w3m/bookmark.html file and save it
note i have added some search engines as well youtube,
and bbc iplayer and itv which can be played with the mpv video player without needing to login
+ edit the ~/.w3m/config file
replace vi with the editor of your choice in the above example
then set the external browsers as shown below and save the file
replace username with your username in the above example
starting the music player daemon
To start the music player daemon we start the service with onestart because we havent rebooted the machine
+ update the mpd database
update the music player daemon database using mpc update
Browsing youtube with w3m
+ start w3m with the bookmarks
then open youtube from the bookmarks link
Launching external browsers with w3m
+ when you are on a videos page
when you are on a videos "page" you can launch an external browser by pressing the number of the external browser and shift m
for example in the ~/.w3m/config we set the following external browsers
1 - mpv
2 - pinch
3 - youtube-dl
so when we are on a videos "page" and we want to send the audio to mpd we can run the pinch script by pressing
2 shift + m
thats 2 then hold shift and press m
that will run the pinch script and use youtube-dl to grab the best quality audio stream from the video and then use mpc to send the audio to mpd and play it
+ if you want to open the video with mpv player
press:
1 shift + m
or for youtube-dl press:
3 shift + m
+ when you are in the search results or in a playlist
to send the audio to mpd we can run the pinch script by pressing
2 esc shift + m
thats 2 then esc then hold shift and press m
+ if you want to open the video with mpv player
press:
1 esc shift + m
or for youtube-dl press:
3 esc shift + m
possible issues
when youtube is updated youtube-dl may stop working and you have to wait a few days for a new version of youtube-dl to fix the issue
the usual error you get with curl and ssl
you can check if a new version of youtube-dl is avaiable by running the following command
+ What to do when you get it working
After you manage to send audio from youtube to mpd dad - dancing is compulsory,
although armchair dancing is permitted if you knees have packed in like mine
For extra points posting a video of your dad dancing to the forum would give us all a good laugh
i for one am waiting for sirdice to appear from behind a cloud of bong smoke before saying
"Tonight Matthew im going to be Beyonce"
Then launching into a chorus of "Uh oh, uh oh, uh oh, oh, no, no"
while booty shaking in a full suit of armour
the w3m browser and a little shell script i wrote called pinch
We use the w3m command line browser and set the pinch script as an external browser,
which we can use run using a keyboard combination in w3m
the pinch script accepts a youtube url and uses youtube-dl to grab the best audio stream,
then uses mpc to send the audio stream to mpd and play it
I know there are projects like mpsyt
but unfortunately you now need to create your own google api key to use with mpsyt
Video demo
You need the latest version of youtube-dl installed with pip,
because the freebsd pkg isnt as up to date
Current version of youtube-dl installed with pip:
2020.05.03
Freebsd pkg:
youtube_dl-2020.01.24
You dont need to uninstall youtube-dl package if you have it installed,
when we set up youtube-dl with pip programs that use it will use the pip version
Install required packages
+ Install music player daemon, mpc and ncmpc and python37 and py37-pip and w3m
Bash:
# pkg install musicpd musicpc ncmpc python37 py37-pip w3m
install youtube_dl with pip
add the python bin to your shell path
add the following code to your ~/.bashrc if uisng bash,
or to your ~/.zshrc file if your are using the zsh shell
Bash:
# home local python bin
if [ -d "$HOME/.local/bin" ]; then
PATH="$HOME/.local/bin:$PATH"
fi
+ source your ~/.bashrc if you are using the bash shell
Bash:
source ~/.bashrc
+ or source your ~/.zshrc file in using the zsh shell
Bash:
source ~/.zshrc
+ install youtube_dl with pip
Bash:
pip-3.7 install --user youtube_dl
when you need to upgrade youtube_dl you would run the following code
Bash:
pip-3.7 install --upgrade --user youtube_dl
music player daemon set up
edit the music player config file and add the location of your music directory
replace vi the editor of your choice
Bash:
# vi /usr/local/etc/musicpd.conf
edit the musicpd.conf and replace username with your username if the example below
Bash:
music_directory "/home/username/music"
playlist_directory "/home/username/.mpd/playlists"
db_file "/home/username/.mpd/database"
log_file "/home/username/.mpd/log"
pid_file "/home/username/.mpd/pid"
state_file "/home/username/.mpd/state"
sticker_file "/home/username/.mpd/sticker.sql"
user "username"
group "username"
bind_to_address "/home/username/.mpd/socket"
audio_output {
type "oss"
name "My OSS Device"
# device "/dev/dsp" # optional
mixer_type "hardware" # optional
mixer_device "/dev/mixer" # optional
# mixer_control "PCM" # optional
mixer_control "vol" # optional
}
+ Create the ~/.mpd and ~/.mpd/playlists directories
Bash:
mkdir -p ~/.mpd/playlists
+ Create the database,log,pid,state,sticker.sql, and socket
Bash:
touch mpd/{database,log,pid,state,sticker.sql,socket}
I wrote a forum post here with a youtube video about setting up bit perfect audio here
Set up your soundcard
List audio devices
Bash:
cat /dev/sndstat
the output from my set up with pcm5 as the default output audio device
Bash:
pcm0: <Intel Haswell (HDMI/DP 8ch)> (play)
pcm1: <Cirrus Logic (0x4208) (Analog)> (play/rec)
pcm2: <Cirrus Logic (0x4208) (Analog Headphones)> (play)
pcm3: <Cirrus Logic (0x4208) (Digital)> (play)
pcm5: <USB audio> (play) default
No devices installed from userspace.
loader.conf
The next step is to load the audio kernel modules in our /boot/loader.conf
edit you /boot/loader.conf
replace vi with the editor of your choice
Bash:
vi /boot/loader.conf
we need to load the audio kernel
by adding snd_hda_load="YES" to /boot/loader.conf
Bash:
snd_hda_load="YES"
i have a usb audio dac so i need to add snd_uaudio_load="YES"
to /boot/loader.conf
Bash:
snd_uaudio_load="YES"
you only need to add snd_uaudio_load="YES" if you are using a usb audio device
sysctl.conf
We set the default audio output device in /etc/sysctl.conf
check your audio devices by running
Bash:
cat /dev/sndstat
in my case the device i want to use as the default audio device is pcm5
edit your /etc/sysctl.conf
replace vi with the editor of your choice
Bash:
# vi /etc/sysctl.conf
set the default audio output device
and enable bitperfect audio which also requires setting the vchans to 0 on the audio device we are setting up
Bash:
hw.snd.default_unit=5
dev.pcm.5.play.vchans=0
dev.pcm.5.bitperfect=1
in the above example i was setting pcm5 as the default output device
if you wanted to set up pcm2 as the default the config would look like this
Bash:
hw.snd.default_unit=2
dev.pcm.2.play.vchans=0
dev.pcm.2.bitperfect=1
You can also set the default audio device and bitperfect audio with the sysctl command
Bash:
# sysctl hw.snd.default_unit=2
# sysctl dev.pcm.2.play.vchans=0
# sysctl dev.pcm.2.bitperfect=1
rc.conf
Starting the music player daemon service
To start the music player daemon automatically at login we need to add
musicpd_enable="YES" to our /etc/rc.conf file
you can either edit the rc.conf file manually
or use the sysrc command
to manually edit the /etc/rc.conf
edit the file with vi or the text editor of your choice
Bash:
# vi /etc/rc.conf
and then add the following code and save the file
Bash:
musicpd_enable="YES"
or you can use the sysrc command
Bash:
# sysrc musicpd_enable="YES"
ncmpc
Setting up the ncmpc mpd client
or you can use any other mpd of your choice
+ create the ncmpc directory in your home
Bash:
mkdir -p ~/.ncmpc
+ copy the ncmpc sample config file to the ~/.ncmpc directory you just created
Bash:
cp /usr/local/share/doc/ncmpc/config.sample ~/.ncmpc/config
+ edit the ~/.ncmpc config file and add the location of the mpd socket, replace vi with whatever editor you want
Bash:
vi ~/.ncmpc
set the mpd host to the location of socket file
Bash:
host = "/home/username/.mpd/socket"
replace username with your username
Create a bin directory in your home
The next step is to create a bin directory in your home if you dont have one
if you already have a bin directory set up and added to your shell path you can skip this step
Bash:
mkdir -p ~/bin
next you have to add the path to the bin directory to your shell path,
by edting your shells config file like your ~/.bashrc for bash or ~/.zshrc file for the zsh shell
+ bash
Bash:
vi ~/.bashrc
+ zsh
Bash:
vi ~/.zshrc
replace vi with whatever editor you want in the above example
then add the following code to add the bin directory to your shell path and save the file
Bash:
if [ -d "$HOME/bin" ]; then
PATH="$HOME/bin:$PATH"
fi
+ you then need to source your ~/.bashrc file if you are using bash
Bash:
source ~/.bashrc
+ or if you are using the zsh shell then need to source your ~/.zshrc file
Bash:
source ~/.zshrc
Pinch shell script
the pinch shell script takes a youtube url and use youtube-dl to grab best quality audio stream from the video,
and then use mpc to send the url to mpd and play it
pinch shell script on github
+ save the code below into a file called pinch into your ~/bin directory
Bash:
#!/bin/sh
# script usage
usage()
{
echo "\
$(basename "$0") -i url"
exit 2
}
# error messages
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
# if script is run arguments pass and check the options with getopts,
# else display script usage and exit
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
# getopts check and validate options
while getopts ':i:h' opt
do
case ${opt} in
i) infile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
# get the audio url
url=$(youtube-dl --no-check-certificate --no-playlist -f bestaudio -g "${infile}")
# yt function
yt () {
if [ -z "$(mpc current)" ]; then # nothing playing
if [ -z "$(mpc playlist)" ]; then # playlist empty
mpc add "${url}"
mpc play
else # playlist not empty so clear add url and play
mpc clear
mpc add "${url}"
mpc play
fi
else # audio playing insert url and play next
mpc insert "${url}"
mpc next
fi
}
# run the yt function with the url
yt "${url}"
+ make the script executable
Bash:
chmod +x ~/bin/pinch
you can run the pinch script on the command line
with the url on single quotes
Bash:
pinch -i 'youtube-url-goes-here'
w3m browser
The next step is to set up the pinch script as an external browser with the w3m browser,
this will let us browse youtube on the command line and press a keyboard short cut either on a link in a playlist,
or on the videos page and then send the audio to mpd and play it
+ edit the ~/.w3m/keymap file or create it to set the external browser shortcut key
Bash:
vi ~/.w3m/keymap
replace vi with the editor of your choice in the above example
and then add the following code to the ~/.w3m/keymap file and save the file
Bash:
keymap ,-m EXTERN
+ edit the ~/.w3m/bookmark.html file or create it if it doesnt exist
Bash:
vi ~/.w3m/bookmark.html
replace vi with the editor of your choice in the above example
then add the following code to the ~/.w3m/bookmark.html file and save it
note i have added some search engines as well youtube,
and bbc iplayer and itv which can be played with the mpv video player without needing to login
HTML:
<html><head><title>Bookmarks</title></head>
<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<body>
<h1>Bookmarks</h1>
<ul>
<li><a href="https://duckduckgo.com/">DuckDuckgo</a></li>
<li><a href="https://google.com/">Google</a></li>
<li><a href="https://www.youtube.com/">YouTube</a></li>
<li><a href="https://www.bbc.co.uk/iplayer">BBC iPlayer</a>
<li><a href="https://www.itv.com">ITV Player</a>
</ul>
</body>
</html>
+ edit the ~/.w3m/config file
Bash:
vi ~/.w3m/config
replace vi with the editor of your choice in the above example
then set the external browsers as shown below and save the file
Bash:
extbrowser /usr/local/bin/mpv %s
extbrowser2 /usr/home/username/bin/pinch -i %s
extbrowser3 ~/.local/bin/youtube-dl %s &
replace username with your username in the above example
starting the music player daemon
To start the music player daemon we start the service with onestart because we havent rebooted the machine
Bash:
# service musicpd onestart
+ update the mpd database
update the music player daemon database using mpc update
Bash:
mpc update
Browsing youtube with w3m
+ start w3m with the bookmarks
Bash:
w3m -B
then open youtube from the bookmarks link
Launching external browsers with w3m
+ when you are on a videos page
when you are on a videos "page" you can launch an external browser by pressing the number of the external browser and shift m
for example in the ~/.w3m/config we set the following external browsers
1 - mpv
2 - pinch
3 - youtube-dl
so when we are on a videos "page" and we want to send the audio to mpd we can run the pinch script by pressing
2 shift + m
thats 2 then hold shift and press m
that will run the pinch script and use youtube-dl to grab the best quality audio stream from the video and then use mpc to send the audio to mpd and play it
+ if you want to open the video with mpv player
press:
1 shift + m
or for youtube-dl press:
3 shift + m
+ when you are in the search results or in a playlist
to send the audio to mpd we can run the pinch script by pressing
2 esc shift + m
thats 2 then esc then hold shift and press m
+ if you want to open the video with mpv player
press:
1 esc shift + m
or for youtube-dl press:
3 esc shift + m
possible issues
when youtube is updated youtube-dl may stop working and you have to wait a few days for a new version of youtube-dl to fix the issue
the usual error you get with curl and ssl
you can check if a new version of youtube-dl is avaiable by running the following command
Bash:
pip-3.7 install --upgrade --user youtube_dl
+ What to do when you get it working
After you manage to send audio from youtube to mpd dad - dancing is compulsory,
although armchair dancing is permitted if you knees have packed in like mine
For extra points posting a video of your dad dancing to the forum would give us all a good laugh
i for one am waiting for sirdice to appear from behind a cloud of bong smoke before saying
"Tonight Matthew im going to be Beyonce"
Then launching into a chorus of "Uh oh, uh oh, uh oh, oh, no, no"
while booty shaking in a full suit of armour