Livestream from Twitch to YouTube using FFmpeg

FFmpeg is a collection of free software for processing audio and video streams. The tool is really powerful. Capable of taking in many formats. It is possible to define audio and video transformations and then save the result as a file or send the content via RTMP.

We already have on the internet Github Gist and Stackoverflow explaining how livestream to YouTube only using FFmpeg. But I got some issue when I was trying to restream a livestream from Twitch and send it to YouTube Live. I will explain the issue in this post.

Get a Twitch stream

Getting the content of a Twitch livestream is easy thanks to Streamlink.

$ export streamer_name=twitch
$ streamlink twitch.tv/$streamer_name best -O 2>/dev/null

The output (video content) is send to stdout. We can send it to a file like bellow. But the goal is to pipe the content to FFmpeg.

$ streamlink twitch.tv/$streamer_name best -O 2>/dev/null > /tmp/live.mp4

Livestreaming to YouTube using FFmpeg

We can receive the content from stdout with a pipe which will redirect the content to stdin.

$ streamlink twitch.tv/$streamer_name best -O 2>/dev/null | ffmpeg -i -

We use the -i parameter to and the character - to take the input from stdin. I need to add some more information to FFmpeg because we have to specify how the tool need to process the video content. This is my final FFmpeg parameters:

$ streamlink twitch.tv/$streamer_name best -O 2>/dev/null | ffmpeg 
    -re 
    -i - 
    -ar 44100 
    -acodec aac 
    -vcodec copy 
    -f flv rtmp://a.rtmp.youtube.com/live2/$youtube_stream_key >/dev/null 2>&1
  • re: Read input at the native frame rate. This is optional because the Twitch livestream is already read at native frame rate.
  • i: input file, sdtin
  • ar: audio rate, it must be 44100 for youtube live*
  • acodec: audio codec: must not be "copy" to force audio rate. Use of "aac", same Twitch audio Codec
  • vcodec: video codec: simply copy the video part
  • f: packaging format

The issue

You could see in the FFmpeg settings that I added a modification to the input audio stream. Indeed, it is important for YouTube to receive an aac audio stream of 44100Hz. The stream coming from Twitch is encoded in 48000Hz.
I had problems when I did not change the video input. This causes the audio to crackle.

The video stream is identical. It is important not to modify it because here the simple copy does not consume any CPU processing.
In terms of overall performance, the audio transcoding is very light even for a small processor. This process can be run on a small machine (VPS, RaspberryPI...)


Source code, installation and script can be found on my Github:

GitHub - arfevrier/autoTwitchToYouTube: Automatically download Twitch broadcasts and upload it to YouTube. Broadcasts are downloaded in best quality, no transcoding, the stream is directly sent back to YouTube. No video is store in disk.
Automatically download Twitch broadcasts and upload it to YouTube. Broadcasts are downloaded in best quality, no transcoding, the stream is directly sent back to YouTube. No video is store in disk.…