How to watch Youtube videos with Gstreamer
3 min. read |
Youtube is a great platform with useful video content. With Gstreamer capabilities and your creativity you can watch same Youtube videos but with original filters (blurring, sharpening), transformations (style transfer), enhancements (coloring, super resolution).
Requirements
- Ubuntu 18
- Python 3.6
- Gstreamer
- youtube-dl
Learn how to?
- use souphttpsrc plugin to stream video via HTTP/HTTPS protocols
- convert youtube link to HTTP/HTTPS URI
- play both video and audio within Gstreamer pipeline
Guide
Get HTTP/HTTPS URL
Youtube-dl is a useful package that allows user to download video from Youtube. It can be installed as pip-package and used with Python.
Let create own virtual environment and install youtube-dl library there
python3 -m venv venv
source venv/bin/activate
pip install --upgrade wheel pip setuptools
pip install youtube-dl
Use the next command in order to get URL from youtube link
youtube-dl --format mp4 --get-url youtube_link
For example, lets take a sample video from Youtube (The Witcher | Netflix) and get best
youtube-dl --format "video[ext=mp4][protocol=https]" \
--get-url https://www.youtube.com/watch?v=ndl1W4ltcmg
Note: In following case we requesting HTTPS URL for best quality video in MPEG-4 media container. To explore more options check official documentation
Previous command produces similar output
https://r2---sn-vhxb5uxax03g-px8e.googlevideo.com/videoplayback?\
expire=1579217056&;ei=QJwgXosxmuzJBfffo8AL&;ip=185.151.85.125&;...
Video streaming using HTTPS
Souphttpsrc is a plugin that allows to read buffers from HTTP/HTTPS stream specified by URL. Let’s build a simple pipeline and run it from command line:
gst-launch-1.0 souphttpsrc is-live=true location="$(youtube-dl --format "best[ext=mp4][protocol=https]" --get-url https://www.youtube.com/watch?v=ndl1W4ltcmg)" ! decodebin ! videoconvert ! autovideosink
You should see something like this
Note: To play both video and audio use next command.
gst-launch-1.0 souphttpsrc is-live=true location="$(youtube-dl --format "best[ext=mp4][protocol=https]" --get-url https://www.youtube.com/watch?v=ndl1W4ltcmg)" ! qtdemux name=demuxer demuxer. ! queue ! avdec_h264 ! autovideosink demuxer.audio_0 ! queue ! avdec_aac ! audioconvert ! audioresample ! autoaudiosink
- qtdemux parses MPEG-4 video container
- avdec_h264 decodes video stream from H264 compression format
- avdec_aac decodes audio stream from Advanced Audio Conding (AAC) format
Explore gstreamer commands cheatsheet so you can play with different pipeline and check different effects on Youtube videos.
Additional
Record video from Youtube
- use filesink to record video into a single video file.
gst-launch-1.0 souphttpsrc is-live=true location="$(youtube-dl --format "best[ext=mp4][protocol=https]" --get-url https://www.youtube.com/watch?v=ndl1W4ltcmg)" ! filesink location=video.mp4 -e
Note: Add -e flag so the video can be closed correctly in case of any interruption (system or user)
List available formats
- for a specific Youtube video
youtube-dl --list-formats https://www.youtube.com/watch?v=ndl1W4ltcmg
Conclusion
- Now, you can play video from Youtube using Gstreamer souphttpsrc plugin.
- Also, we checked both audio and video play with Gstreamer
Hope the information was useful and everything worked as expected 😉
Hi Taras,
Great tutorial! I am able to run gstreamer for a variety of cases. I am trying to create a filter chain with youtube-dl to stream a video from it is not recognising the souphttpsrc plugin.I followed this tutorial to install gstreamer in Linux.
https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp/blob/master/install-instructions-linux.md
This is the start of the sommand.
gst-launch-1.0 -v souphttpsrc location=…
Thanks in advance!
SP
This the exact message I am getting.
WARNING: erroneous pipeline: no element “souphttpsrc”
Hi SP,
Let see. Plugin “souphttpsrc” is a part of gst-plugins-good. So check if this package installed. If not install with next command:
apt install gstreamer1.0-plugins-good
Also check out Google Colab with commands I use to setup gstreamer. There I tried to check “souphttpsrc”.
Hope this helps 😉
Hi Taras,
Thanks for a quick response and really appreciate going the extra mile to set up the collab net.In my case gstreamer1.0-plugins-good is already installed, still gst-inspect-1.0 souphttpsrc gives this message “No such element or plugin ‘souphttpsrc”.
I tried removing and installing gstreamer1.0-plugins-good with no luck.:-(
PS: This a customised version of gstreamer which is suggested by AWS kinesis video streams.Still the library installation process is a standard one with apt install … I am following this tutorial https://github.com/awslabs/amazon-kinesis-video-streams-producer-sdk-cpp/blob/master/install-instructions-linux.md
It installs gstreamer1.0-plugins-good as part of an install script but surprisingly it is missing souphttpsrc .
Warm regards
SP
Hi,
Try to find the root of problem with building gst-plugins-good from sources:
And when configuring check the output. I have something like this:
configure: *** checking feature: soup http client plugin (2.4) ***
configure: *** for plug-ins: souphttpsrc ***
checking for SOUP… yes
configure: *** These plugins will be built: souphttpsrc
configure: *** Plug-ins with dependencies that will be built: souphttpsrc
Otherwise there should be an error, so you can install missing dependencies.
Best regards,
Thanks taras, the stuff I was working went into backburner for a while.I will update how it went once I pick this thread again.
Amazing site mate,keep up the great work!Really appreciate the quick response and the effort you have taken to look into this issue.I found this to be one of the best on Gstreamer with practical examples.
Thank you a lot 🙂
Good luck with Gstreamer exploration 🙂
Great! Just what I was looking for.
By the way, playing with your code (and many other things from the Internet), I was also able to put my tablet and my Android phone to work as webcams. This uses an app, IP Webcam that broadcast the image on the internal network:
ffmpeg -loglevel quiet -stats -re -i http://192.168.1.110:8080/videofeed -map 0:v -vcodec rawvideo -vf format=yuv420p -f v4l2 /dev/video0
This allowed the use of my cell phone as camera in Google Meet and Skype. I know almost nothing about ffmpeg, and I know the command line above probably sucks (but it works!). Maybe if you have the time and interest you could do an article on this topic.
Best regards, Antonio
Hi Antonio,
Thank you a lot for your feedback 😉
Interesting. I’ll definitely check it with my cellphone too!
Good luck,
Taras
Hi Taras. Yes, please do it, if you have time. This saved me of buying two cameras for my desktops. Less electronic trash…
I tried this script bellow. It did not work for me (I got only one static image), but gave me the basic ideas and some commands:
https://github.com/bluezio/ipwebcam-gst
Poking here and there, I managed to run the thing over USB (using that ADB stuff):
## Simple(ton) script to use Android devices as Webcam over ADB
## using the App “IP WEBCAM” in the device and v4l2loopback/ffmpeg
## Edit the ‘http://xxx.xxx.xxx.xxx:xxxx/videofeed’ below as needed
##
## This will run over TCP
## ffmpeg -loglevel quiet -stats -re -i http://192.168.1.110:8080/videofeed -map 0:v -vcodec rawvideo -vf
## format=yuv420p -f v4l2 /dev/video0
# Star the v4l2 module
sudo modprobe v4l2loopback exclusive_caps=1
# Start ADB server
/usr/bin/adb start-server
# Forward the DEVICE port to the LOCAL (127.0.0.1) loopback
/usr/bin/adb forward tcp:8080 tcp:8080
echo “————— press ‘Q’ to quit ————— ”
ffmpeg -loglevel quiet -stats -re -i http://127.0.0.1:8080/videofeed -map 0:v -vcodec rawvideo -vf format=yuv420p -f v4l2 /dev/video0
# Remove the v4l2 module and disable the video0 device
sudo modprobe -r v4l2loopback
Probably, a comand line horror show, but works for me.
Best wishes, Antonio
Hey Antonio,
Thank you for details 🙂
Hi ,
Great tutorial.
Can we do the same with rtspsrc plugin ? Is there any way to get the rtsp link ?
Hi Nishitha,
Yes, it is possible according to youtube-dl documentation. But not all youtube video links provides rtsp protocol to stream video.
This is taken from youtube-dl documentation:
Example:
youtube-dl --format "bestvideo[ext=mp4][protocol=rtsp]" --get-url https://www.youtube.com/watch?v=i6AzgAeNwyA
Best regards,
Taras
Hello Taras,
Thanks to your article, very helpful understanding gstreamer and youtube.
However, I have tried this with my library, meant not use gst-launch-1.0
Result is failed with Error parsing URL at souphttpsrc.
I think this is only possible with gst-launch-1.0 command.
If possible, your comment would be helpful.
From HungRok.