hack33r.com

Using youtube-dl and ffmpeg to forward streams

Sep
16

youtube-dl is a handy tool for anyone wanting to download or stream videos from youtube and many other sites on the internet. They have pretty decent documentation on how to download videos. You can also use youtube-dl and ffmpeg together to forward video streams and transcode them on the fly. Here is an example of using youtube-dl’s `–exec` option and ffmpeg to stream a video from youtube and forward it to some rtmp location.

youtube-dl -f best \
  https://www.youtube.com/watch?v=gHG9FRSlPxw \
  --exec "ffmpeg -re -i {} -f flv rtmp://localhost:81/test/test";

The previous example works great with youtube but I found issues trying to use youtube-dl’s `–exec` option. Another way of streaming video and audio from youtube-dl to ffmpeg is using a pipe. You can send the contents from youtube-dl to stdout and pipe that to ffmpeg. The following is an example of streaming an mp4 using a pipe.

youtube-dl -f best \
  https://mp4sonly.org/video/foovid.mp4 \
  -o - | ffmpeg -re -f mp4 -i pipe:0 -ar 44100 -f flv rtmp://localhost:1935/test/test'