---
title: 'How to run 24/7 YouTube live stream on linux | Zomro'
description: 'Today we will look at how you can easily start a round-the-clock YouTube broadcast on linux by broadcasting your videos.'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a54@server-panel.net/2022/06/01/1656415834_screenshot_1325.png'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [FAQ](https://zomro.com/blog/faq/)
  * How to run 24/7 YouTube live stream on linux 



# How to run 24/7 YouTube live stream on linux

17-06-2022

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.  
  
  
**Installation:**  
**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
    
    
    _ffmpeg -version_

If ffmpeg is installed, there will be the following result as in the picture below.  
  
![How to run 24/7 YouTube live stream on linux - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a54@server-panel.net/2022/06/01/1656415834_screenshot_1325.png)  
  
**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
    
    
    _vim /root/YouTube/YouTube.py_

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.

* * *

  
**Options** :  
  
-_threads_ \- number of threads that ffmpeg can handle  
-_preset_ \- how much CPU will be loaded  
There are the following options:

  * ultrafast
  * super fast
  * very fast
  * faster
  * fast
  * medium
  * slow
  * slower
  * 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_

  
![How to run 24/7 YouTube live stream on linux - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a54@server-panel.net/2022/08/01/1661427926_screenshot_1555.png)  
  
After installing this package, we switch to the "screen" mode. To do this, write the following command:
    
    
    _screen_

  
After writing this command, you need to run this script. To do this, we write the following:
    
    
    _python /root/YouTube/YouTube.py_

  
We can see that the script is running. An example can be seen in the following screenshot.  
  
![How to run 24/7 YouTube live stream on linux - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a54@server-panel.net/2022/08/01/1661441902_screenshot_1559.png)  
  
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
    
    
    _screen -r_

  
After entering this command, our script will open. You can stop it using the key combination CTRL + Z

Similar articles:

[How to Install Docker on Centos 7](https://zomro.com/blog/faq/287-kak-ustanovit-docker-na-centos-7) [How to Install Docker on Ubuntu 20.04](https://zomro.com/blog/faq/286-kak-ustanovit-docker-na-ubuntu-2004) [Installation of PostgreSQL and pgAdmin4 on HestiaCP panel](https://zomro.com/blog/faq/326-ustanovka-postgresql-i-pgadmin4-na-panel-hestiacp) [How to connect to different Windows servers through one IP address](https://zomro.com/blog/faq/341-kak-podklyuchatsya-k-raznym-serveram-windows-cherez-odin-ip-adres) [How to track SSH connections to the server using a Telegram bot](https://zomro.com/blog/faq/329-kak-otslezhivat-ssh-podklyucheniya-k-serveru-s-pomoshyu-telegram-bota)

[ Forex Server from €7.95/mon ](/server-for-forex)
