ISS Art company logo
Go back
How we launched video streaming on Raspbery Pi
Categories: General

by Evgeny Zhiryakov

no tags
How we decided to launch a system of video streaming

As in any other company, in ours there is some outdated equipment. It is quite workable, but cannot be used by employees. So, there are monitors, system units, and many other little computer parts like old-fashioned PS/2 mice and keyboards lying useless.

In this article we are going to talk about monitors we decided to use as an announcement board.

As the source of videos it was decided to use a Raspberry Pi.

Raspberry Pi is quite a weak hardware, but it suits our purpose well.

The Broadcom chip installed in the Raspberry Pi has hardware video acceleration support that allows you to play H.264 video with a bit rate of 40 MBits/s.

It's rather good for such a small board.

The first experience

Let's start with the story of the first experience of video streaming.

Well, not exactly video streaming, first it was just one monitor and one Raspberry Pi board.

There was nothing complicated: we installed a cross-platform media player, saved the video files on a flash drive and started their cyclic playback, perfect!

The screens displayed employees' current month's birthdays, and such information is quite useful at a company of our size.

There were also announcements of the upcoming events and photo and video records of them. In general, everyone liked the idea.

But there was a screen only on one floor of the office, the matter was we wanted to cover the entire audience. That's why a second monitor was bought and a second Raspberry board, and then we went through all the same steps again.

Everything worked fine, but we wanted more, besides, updating files on each screen separately was rather tiresome.

The improvement of the system

The idea was the following:

We set up the streaming from the server to all the screens, as a result, we get a centralized streaming, and on all screens the same information is always displayed.

In the previous version, there was mal-synchronization, because there was no simultaneous start, it was not very important while the screens were on different floors.

But at some point, there were multiple screens, and mal-synchronization started to be too obvious, which a perfectionist couldn't bear.

We had had experience setting streaming using VideoLAN, and we wanted to use it.

Thus, we set up the server on Debian Linux, installed VLC, placed video files on the server and started streaming.


However, not everything started going smoothly right away, and in the process of introducing the video streaming system, we faced various challenges.

1. The cross-platform media player was not very convenient, we wanted a real Linux Way

The media player we use is a multimedia processor, it does a lot, but at the same time it consumes more resources, and we only need one function — to reproduce the video stream.

Since the media player turned out to be too bulky for the task, it was decided to use another media player.

For this purpose, the basic version of Debian Linux was installed on the Raspberry Pi.

2. Finding a suitable media player

To take full advantage of the video hardware acceleration, we took the OMXPlayer, because it is a console media player with OpenMAX hardware acceleration.

On the Raspberry Pi, the installation does not cause any problem, because there is a package in the default repository.

apt-get install omxplayer

After the installation, you can run "omxplayer –live udp://@:1234" and the player is ready to accept the stream.

3. Optimization of parameters

Since most of the RAM was freed up, there appeared a possibility to reallocate the physical memory to increase graphics memory.

For this you need to edit the file /boot/config.txt

Having added gpu_mem=256 to it

4. OMXPlayer is very sensitive to the parameters of the video stream

For example, it used to fall displaying a cheerful phrase "have a nice day", whenever the bit rate of the video stream was changed.

And those days were really "nice", because now we know the cause, but then it was a mystery.

We started preparing to stream video-files with the same settings, and it helped, streamings stopped interrupting.

But sometimes the human factor intervened, and the playlist had at least one file with parameters different from the others, and then all went wrong.

The final version of the system

Once again I wanted to change things for the better and prevent unpleasant surprises and the human factor.

The solution was found quickly, but getting from the idea to realization was not at all easy.

The solution itself was to make the video files with the same parameters converted completely automatically, without any human participation.

No, a human is still required, but only to put the playlist together and place the video files in the network folder.

Starting from this point on everything is done automatically.

At midnight when everyone is asleep, video-files start being converted and after the completion of the process the streaming restarts.

The streaming started, and the image appeared on the screen. It's probably the same feeling that the first television watchers experienced.

To implement the plan, the Bash script was written:


### Path to FFmpeg

### Path to logo

### Work dirs without trailing slash

### Cleaning filenames
rename "s/ /_/g" $INFILES/*
rename "s/'//g" $INFILES/*
rename "s/'//g" $INFILES/*
rename "s/,//g" $INFILES/*
rename "s/\!//g" $INFILES/*
rename "s/\№//g" $INFILES/*
rename "s/\050/_/g" $INFILES/*
rename "s/\051//g" $INFILES/*
rename "s/\.\././g" $INFILES/*
rename "s/\._/_/g" $INFILES/*
rename "s/__/_/g" $INFILES/*

function converting {

### We want HD Ready (1280x720)

$FFMPEG -i $file_to_convert -i $LOGO -r 25 -codec:v libx264 -profile:v high -level 3.2 -preset veryslow -g 15 -crf 18 -pix_fmt yuv420p -maxrate 4000k -bufsize 2000k \
-filter_complex "[0:v]scale=iw*min($frame_width/iw\,$frame_high/ih):ih*min($frame_width/iw\,$frame_high/ih),pad=$frame_width:$frame_high:($frame_width-iw)/2:($frame_high-ih)/2[v1];[v1][1:v]overlay=main_w-overlay_w-10:10" \
-threads 0 -af dynaudnorm -c:a aac -ac 2 -ar 44100 -b:a 128k -y $TEMPDIR/$converted_file
### Save some disk space
dd if=/dev/urandom bs=1 count=128 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev > $file
### Create MD5 hash for file
md5sum $file_to_convert | cut -d " " -f1 > $TEMPDIR/$converted_file.md5

function find_converted {

echo " Trying to find converted video..."
for md5_file in $MD5DIR/*
md5_search=`cat $md5_file`
if [ "$file_md5" = "$md5_search" ]
echo " Lucky day! We finded it in $(basename "$converted_file")."
tempfile=`echo $md5_file | awk -F/ '{print$(NF)}' | awk -F.md5 '{print($1)}'`
cp $OUTDIR/$tempfile $TEMPDIR/$converted_file
mv $md5_file $TEMPDIR/$converted_file.md5

### Take action on each file in INFILES path.

### Initializing variables

### $file_to_convert store current file name wit absolute path.
for file_to_convert in $INFILES/*
echo "Processing $(basename "$file_to_convert") file..."

### Does the file exist?
if [ ! -f $file_to_convert ]
echo "ERROR: $(basename "$file_to_convert") does not exist - aborting"
exit 1

### Does the file non zero lenght?
if [ -z $file_to_convert ]
echo "ERROR: $(basename "$file_to_convert") is empty - aborting"
exit 1

### Setting output filename format
### 001.mp4 002.mp4 003.mp4 etc.
converted_file=$(printf "%0.3d.mp4" $i)
i=$((i + 1))

### Path to file's saved md5sum

### Create the md5sum from the file to check in future
file_md5=`md5sum $file_to_convert | cut -d " " -f1`

### Do we have allready an saved fingerprint of this file?
if [ -f $fingerprintfile ]
### Get the saved md5
saved_md5=`cat $fingerprintfile`

### Check if it's empty
if [ -z $saved_md5 ]
echo "WARNING: The file is empty"

### Compare the saved md5 with the one we have now
if [ "$saved_md5" = "$file_md5" ]
echo " File has not been changed"
mv $fingerprintfile $TEMPDIR/$converted_file.md5
mv $OUTDIR/$converted_file $TEMPDIR/$converted_file
echo " File has been changed or renamed"
find_converted $file_md5 $converted_file

if [ $FINDED -ne 1 ]; then
converting "$file_to_convert" "$converted_file"
find_converted $file_md5 $converted_file

if [ $FINDED -ne 1 ]; then
converting $file_to_convert $converted_file


echo "Moving converted files and md5 files."
if [ "$(ls -A $MD5DIR)" ]; then
echo "Take action $MD5DIR is not Empty"
rm $MD5DIR/*.md5

if [ "$(ls -A $OUTDIR)" ]; then
echo "Take action $OUTDIR is not Empty"
rm $OUTDIR/*.mp4

mv $TEMPDIR/*.md5 $MD5DIR/
mv $TEMPDIR/*.mp4 $OUTDIR/

if [ $STREAMING_RESTART -eq 1 ]; then
echo "Concatating converted files."
for f in $OUTDIR/*; do echo "file '$file_to_convert'" >> $TEMPDIR/mylist.txt; done
$FFMPEG -f concat -i $TEMPDIR/mylist.txt -c copy -y /Converter_new/All_in_one.mp4
rm $TEMPDIR/mylist.txt

### Stop streaming
echo "Stopping streaming..."
VLC_pid=`cat /var/run/`
kill -9 $VLC_pid >> /dev/null 2>&1
rm -f /var/run/
sleep 5
### Start streaming
echo "Starting streaming..."
/usr/bin/vlc /Converter_new/All_in_one.mp4 --intf rc --rc-host --rc-fake-tty --loop --file-caching 0 --sout-keep --sout-mux-caching 0 \
--sout-udp-caching 0 --sout-udp-group 50000 --sout-ts-shaping 1000 --sout-ts-use-key-frames --no-drop-late-frames --no-skip-frames \
--sout "#gather:duplicate{\
dst=std{access=udp,mux=ts,dst=}}" >> /var/log/vlc.log 2>&1 &
sleep 5
ps ax | grep "/usr/bin/vlc" | head -1 | awk -F " " '{ print $1 }' > /var/run/


We hope you will find this post about our experience useful.

Have you had experience of launching a video streaming system? Feel free to tell us in the comments!