Youtube file streaming



I want to tell you, how to build your own video channel using Youtube files.

For this project, I will use Ubuntu 16.04 LTS machine. First of all, we will need to install additional utility: Youtube-dl (https://github.com/rg3/youtube-dl/blob/master/README.md#readme)

Typo: This utility will help to download files from Youtube without ads in mp4/acc codec.

Also, we will need the Streaming Server which can help us to share our channel for a broad audience.
I chose the Flussonic Media Server for this part.

You can discover it at https://flussonic.com/ and request the trial key at http://flussonic.com/trial.

Typo: Flussonic builds with ffmpeg package, which will be helpful for our project.

All preparations are finished, let’s start.

Part 1. Files download.
Youtube-dl can download exact video and whole playlists. For example, we will try to download GoPro Awards playlist from Youtube (https://www.youtube.com/playlist?list=PLSSPBo7OVSZsljlGMHtOnWm8HXdUGl2Xt) We will need mp4/acc codec for downloaded files. For this case we will need the next commands:

To chose the directory, where we want to download our files run:

sudo cd /mnt/sda/Youtube/
To download playlist run:
sudo youtube-dl -Acitf 22 https://www.youtube.com/playlist?list=PLSSPBo7OVSZsljlGMHtOnWm8HXdUGl2Xt
--A Auto-numbering
--с Youtube-dl will continue download in case of connection problems or any errors
--i Errors ignoring
--t The same file-name as at Youtube
--f 22 The downloaded files format (to view all available formats place
-F without argument and chose ID you want to download)

Typo: The whole command list you can find at https://github.com/rg3/youtube-dl/blob/master/README.md#readme

After that, we will have the directory with downloaded files at /mnt/sda/Youtube.

Part 2. Files renaming
We will need to replace spaces in names with downline "_"

Open the directory with downloaded files

sudo cd /mnt/sda/Youtube/
Create the shell file
sudo touch rename.sh
Open the file
sudo nano rename.sh
Place the next code inside and save file
find . -name '* *' -exec rename 's/ /_/g' {} \;
Able the file to be runned
sudo chmod +x ./rename.sh
Run the shell script:
sudo ./rename.sh
Part 3. Stream building
To build the looping stream we will need to create the shell script:

Open the directory with downloaded files

sudo cd /mnt/sda/Youtube/
Create the shell file
sudo touch streaming.sh
Open the file
sudo nano streaming.sh
Place the next code inside and save file
while true; do for file in $(ls -1); do ffmpeg -re -i "$file" -vcodec copy -bsf:v h264_mp4toannexb -acodec copy -f flv rtmp://localhost/mylive/Youtube done done
Typo: We are using ffmpeg package to build our stream from files. The script will read the file list in every loop and will update it with the new files or missing (deleted) files. We will use RTMP for our stream up to the server, but you can push your stream everywhere you want.

Able the file to be runned

sudo chmod +x ./streaming.sh
Part 5. Media Server.
As I described earlier I've chosen Flussonic Media Server for my project. Flussonic Media server is a powerful server-side software for capturing, transcoding and delivering streams.

Run this command to install Flussonic:

sudo curl -sSf https://flussonic.com/raw/install.sh | sh
Run Flussonic:
sudo /etc/init.d/flussonic start
Open in browser Flussonic admin web interface at the page http://flussonic-ip:8080/ and paste there license key that you have received (change "flussonic-ip" to the real address of the server). On this page you can change administrator login and password: License key stored in /etc/flussonic/license.txt file, you can put the key there before start. You can check whether your Flussonic installation is correct by visiting http://flussonic-ip:8080/ where flussonic-ip is the address of the hosting server to which you installed the software.

Open the Flussonic admin interface and create new location at “Publishing locations” named “mylive”

Part 6. Run
To stream your channel to the Flussonic run:
sudo cd /mnt/sda/Youtube/
sudo ./streaming.sh
Or you can run this process at background:
sudo nohup ./streaming.sh &
After that you will see something like that: This means that you successfully run your channel and its pushed to the Flussonic.

In Flussonic you will find your stream

You can get the embed-code and place it to your website by clicking the stream name /Youtube.
Also, you can protect your stream with securelinks or tokens.


thank you for sharing!


Thanks for the good topic!
I wanna add some additions.
1.YouTube-DL installation (Don’t install via apt!):
sudo wget https://yt-dl.org/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+x /usr/local/bin/youtube-dl
2. If would be error, when ran streaming.sh:
Add to 0 line: #!/bin/sh


Hello, I have done as described. But alwyas, I have got error:
./streaming.sh: 2: ./streaming.sh: Syntax error: end of file unexpected (expecting “done”)
even I use “Add to 0 line: #!/bin/sh” or not. Where could be error? Best regards.