How can you easily start a 24/7 YouTube stream on linux by streaming your videos? Why Linux? Why can't you just install OBS studio and use it to start streaming? One of the reasons is that OBS studio requires more power to run and stream, and there may not be a stable and smooth live stream. If you can run it, then not on the weakest server, and at the same time, FPS can be no more than 20 frames / s. For this, we need a server with OS Linux.
What will be required:
Server with Linux system. Today we will consider installation on a server running CentOS 7.
1. Connect to the server via SSH. If you do not know how to connect to the server, you can use the instructions
2. Install additional software to start broadcasting.
To do this, we connect an additional repository so that you can install software for broadcasting. To do this, write the following command in the console
yum install epel-release -y yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm -y
3. We install ffmpeg. To install, write the command in the console:
yum install ffmpeg ffmpeg-devel -y
4. To check if ffmpeg is installed, write the command
If ffmpeg is installed, there will be the following result as in the picture below.
5. After installing ffmpeg, we create a directory in which we will store videos. In our case, the directory that will store the video is /root/YouTube/video. To create this directory, write the following command:
mkdir -p /root/YouTube/video
6. The next step is to create and edit a file / script that will be responsible for the operation of the live broadcast on YouTube. Let's write a command
After writing, the YouTube.py file will open in which you need to write the following code:
import os dirname = "/root/YouTube/video/" key = 'rtmp://a.rtmp.youtube.com/live2/0xx0-xxxx-x000-x0x0-xxxx' while True: files = os.listdir(dirname) for f in files: if ".mp4" in f: cmd = "ffmpeg -threads 3 -re -i " + dirname + f + " -c:v libx264 -preset ultrafast -crf 24 -g 3 -f flv " + key os.system(cmd)
The dirname parameter has the path to the video directory that will be broadcast in the live broadcast.
If you want to set your own directory where videos are located, replace "/root/YouTube/video/" with your full path.
To set your broadcast key, change "rtmp://a.rtmp.youtube.com/live2/0xx0-xxxx-x000-x0x0-xxxx" to the value of your key.
-threads - number of threads that ffmpeg can handle
-preset - how much CPU will be loaded
There are the following options:
- super fast
- very fast
- very slow
Depending on the power of the CPU, set the desired parameter. For the best result, we will check each parameter, how the translation is performed with it, and depending on the speed of work, we set one or another parameter.
-crf - coefficient, which is responsible for the image quality. The default is 23. This coefficient can have values from 0 to 51. The larger the value, the better the quality. Value 0 - the quality is equal to the quality of the video itself. The value 51 is the worst quality.
-g is a factor that specifies the explicit number of keyframes.
-re Specifies that the file should be converted to a stream.
-i indicates that the file should be played.
-f specifies what format the file should be converted to. YouTube needs "flv" format
If all the necessary settings have been completed, you can start running the script.
7. To run the script in the background, you need to install an additional package screen. To do this, write the following command:
yum install screen -y
After installing this package, we switch to the "screen" mode. To do this, write the following command:
After writing this command, you need to run this script. To do this, we write the following:
We can see that the script is running. An example can be seen in the following screenshot.
To disconnect from the server safely, while leaving the live broadcast running, you need to press CTRL + A, and then press the D key. The script will go into the background, and the broadcast will continue to work. After that, you can disconnect from the server. Or you can close the ssh client window - through the window close button (cross), the session will not be interrupted.
If you want to stop the script or check its work, you need to write the following command in the server console
After entering this command, our script will open. You can stop it using the key combination CTRL + Z