I just want to stream (whitout downloading) a youtube video. Is there an easy way to do this with python or exists a script to do that? Or i must download in a temporany file the video, convert it and play?
4 Answers
pafy
is a Python library that can be used to retrieve URLs to pass to VLC or mplayer, etc. You can specify audio only. It's well documented here and can be installed with sudo apt-get install python-pafy
.
Note it comes with a command line tool called ytdl
which seems similar to youtube-dl
and, as such, won't really do what you want. You'll have to write some python. I assume by the nature of your question this is no problem, but let me know if you need further help.
-
1This is so cool, just one file which do what i want. Thank you! :) Commented Sep 2, 2014 at 12:29
If you have vlc and the text-based browser lynx (both available from Ubuntu's repositories), there is a neat trick you can use to stream video from a commandline. The command I use will do a search and create a playlist of the first page of results which will play in vlc.
I created a simple script to handle the tricky syntax. The script is:
#!/bin/bash
lynx -dump "https://www.youtube.com/results?search_query=\"$1\"" | egrep -o "http.*watch.*" | vlc -
So, if you name the script, say stream_youtube
, you can just enter "stream_youtube "search term"
I just tried it out after not using it in a while, and noticed a couple of errors popping up in a window that I never saw before, but in spite of that annoyance, it still worked. Someone might find it useful, or at least interesting. :-)
This is not the perfect solution for your problem, but it will maybe help you.
youtube-dl
There is a command line tool called youtube-dl
, which offers many options. I think it will first download the full video and then convert to audio. I'm not sure whether there is a possibility to only download an audio stream, but I guess not. Also, youtube-dl
is written in python, but it's not only a simple script.
If you take a look at the manpage, look for the post-processing options. --extract-audio
for example will convert the video file to an audio file. You can also set the --audio-format
and the --audio-quality
. You can also run any custom command after downloading with --exec CMD
.
- Get it from the Ubuntu Software Center:
sudo apt install youtube-dl
- Get it from Github: Then update it with
sudo youtube-dl -U
VLC steaming
Alternatively, you can use VLC to stream the video and just ignore or turn off the video function.
It should work to copy the url, open vlc and press CTRL+V or open a network steam (CTRL+N) and paste the url. Then click on Video->Videotrack->deactivate.
Note that this will still stream the whole video, but it will not display it.
-
A useful alias:
alias youtube-dl-audio='youtube-dl --ignore-errors --output "%(title)s.%(ext)s" --extract-audio --audio-format mp3 --audio-quality 0'
Commented Mar 10, 2019 at 4:10
You can pipe the output of youtube-dl with your preferred format into ffplay without showing any windows.
example:
youtube-dl -f 251 https://www.youtube.com/watch?v=vdRJMuOFO88 -o - | ffplay -nodisp -autoexit -i -
this is done via shell but you can do it programmatically if needed since both apps have python bindings.