Play YouTube video from terminal to Chromecast
When my daughter wants to see a cartoon (like the Smurfs Series) sometimes I don't want to open a youtube to send it to our Chromecast. Chromecast have a bunch of HTTP endpoint, so I made a zsh function to do it. The only I need is the ID of the video.
# zshrc
ccyt() {
curl -H "Content-Type: application/json" \
http://192.168.1.103:8008/apps/YouTube \
-X POST \
-d "v=$1";
}
# bachrc
function ccyt {
curl -H "Content-Type: application/json" \
http://192.168.1.103:8008/apps/YouTube \
-X POST \
-d "v=$1";
}
Put it into your .zshrc (or bashrc) and than:
$ ccyt TRbNUu2LUOA
Extra: Youtube Search
A little plus. So that function needs a videoID as parameter. How can I get the ID? With a ytsearch
function.
ytsearch() {
curl -s https://www.youtube.com/results\?search_query\=$@ | \
grep -o 'watch?v=[^"]*"[^>]*title="[^"]*' | \
sed -e 's/^watch\?v=\([^"]*\)".*title="\(.*\)/\1 \2/g'
}
So I search and play:
(clockwork) Ξ ~ → ytsearch hupikék törpikék
BtW1LRfdio4 Hupikék törpikék 8. 2008 - TELJES
zA0gCbI9bXk Hupikék törpikék 2. 2007 - TELJES
TRbNUu2LUOA HUPIKÉK TÖRPIKÉK 3 TELJES RAJZFILM
[ ... snip ... ]
pXvpVFBpj8k Hupikék Torpikék / DS / Modra 2.resz
(clockwork) Ξ ~ → ccyt TRbNUu2LUOA
(clockwork) Ξ ~ →
Written by Balazs Nadasdi
Related protips
10 Responses
awesome
What is your intent with using grep -o
? My bash is telling me that's not a valid option.
The -o
flag tells grep thet it needs to return only the matched section:
(clockwork) Ξ ~ → echo -en "Number: 123141221\nNot number: ajhdkjasd\n"
Number: 123141221
Not number: ajhdkjasd
(clockwork) Ξ ~ → echo -en "Number: 123141221\nNot number: ajhdkjasd\n" | grep "[0-9]\+"
Number: 123141221
(clockwork) Ξ ~ → echo -en "Number: 123141221\nNot number: ajhdkjasd\n" | grep -o "[0-9]\+"
123141221
(clockwork) Ξ ~ →
Section from the man
: unixhelp.ed.ac.uk
-o, --only-matching Prints only the matching part of the lines.
I dont see your answer only I got a mail about it. Same issue with npm
on windows. https://github.com/creationix/nvm/issues/75. There is a comment that says:
Since this is one of the first google results for git bash's seemingly terribly outdated grep problem, I'll post a temporary workaround here.
Open <Git install directory>/bin and overwrite grep.exe with a more up to date version. I found two alternatives that provide -o support: GnuWin32's grep 2.5.4
Sorry, I deleted it as after posting I found that exact download. Now, though, sed
seems to be doing nothing...
I don't have a Windows box so I can't test or figure out what would be the problem. If you find please tell us. If someone meets the same problem then (s)he can find the solution here :)
Maybe. Try to do not escape the ?
and parentheses. Just a guess and maybe it works :)
Yeah, I had to add -r
as the first flag to sed
and then treat the pattern like normal regex - as in, only escape characters you want as literals. So it now looks like this:
curl -s https://www.youtube.com/results\?search_query\=$3 | \
grep -o 'watch?v=[^"]*"[^>]*title="[^"]*' | \
sed -r -e 's/^watch\?v=([^"]*)".*title="(.*)/\1 \2/g'
You still have to escape the ?
because it's a text character you want to match, you don't want it to be evaluated as a regex operator.
I hope you don't mind, but I've done a bit of work expanding on the code you've posted here. Of course, I've attributed you as the original author.
I turned it into a shell script for easier development, and I've combined the search output and URL generation under -s
and -w
flags. My intention is to make it so you can do a search, then pick a video and even specify how to handle launching the video.
So far I've only tested on Windows using Git Bash, but here's what I've got:
https://gist.github.com/Ultrabenosaurus/8974206b2cba0f1c615a
Nice :D (if someone asks me next time about Windows bash I will know the answer).
The script is awesome. My first usage was a script with readline
and after the search the user (aka me) could select with a simple numeric answer to what to do. I use it for a while but after that I made a few small command instead of big script. It's more comfortable for me now :)