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

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
    • pay attention qtdemux has dynamic number of outputs, so you can extract with it both audio and video streams
  • 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 😉

14 Comments

Add a Comment

Your email address will not be published. Required fields are marked *