View HDHR from a Web Browser
Introduction
This document shows you how to set up a system so you can view your web channels from a web browser.
Prerequisites
- Linux Web Server (in this case we will use Rocky Linux 8.5)
- Browser that supports HTML5
- Configured and working HD Homerun
- Linux and HDHR device should be on same network (vlan/subnet).
If you are going to access the web server remotely, it is assumed that port 80 and 443 are opened on the internet.
The linux web server is based on this
https://my.apolonio.tech/?p=221
It needs to have enough horsepower to run ffmpeg
Step #1 Install 3rd party repos so you can install ffmpeg and vlc
Do these as root technically you don’t need vlc, its in here due to some other things I have planned, there are other ways to install ffmpeg and you after you compile the hdhomerun utility you can delete it.
wget https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-8.noarch.rpm
dnf -y localinstall rpmfusion-nonfree-release-8.noarch.rpm
dnf -y install epel-release
dnf -y install dnf-plugins-core
dnf config-manager --set-enabled epel-testing
dnf config-manager --set-enabled powertools
dnf -y makecache
dnf -y install ffmpeg vlc gcc
Step #2 Configure firewall rules
Do these as root
firewall-cmd --permanent --add-port=1935/tcp
firewall-cmd --permanent --add-port=1935/udp
firewall-cmd --permanent --add-port=5000/tcp
firewall-cmd --permanent --add-port=5000/udp
firewall-cmd --permanent --add-source-port=65001/udp
firewall-cmd --permanent --add-service={http,https,samba,nfs,nfs3,mountd,rpc-bind}
firewall-cmd --reload
For the lazy with bad security habits, commented out so not to encourage simple copy and pasting.
#systemctl stop firewalld
#systemctl disable firewalld
Step #3 Install hdhomerun utility
Do these as a normal user with sudo rights
cd
mkdir src
cd src
wget https://download.silicondust.com/hdhomerun/libhdhomerun_20220822.tgz
tar zxvf libhdhomerun_20220822.tgz
cd libhdhomerun/
make
sudo cp -p hdhomerun_config /usr/local/bin/
sudo cp -p libhdhomerun.so /usr/local/lib64/
Test it
hdhomerun_config discover
Step #4 Setup script thto transcode hdhr
Do these as a normal user with sudo rights
Instructions are based from this guys page, so credit is due where credit is due
https://www.rickmakes.com/streaming-live-tv-from-a-hdhomerun-to-a-web-browser-using-ffmpeg/
And his video https://www.youtube.com/watch?v=Nc7qx2Go2MY
Step #4A create a location for transcoded files to stream from
You will need a location to store the transcoded files, I recommend /run which is RAM, but sometimes you want it on a disk filesystem, in this example I am using myself.
udo mkdir /var/lib/stream/
sudo chown larry:larry /var/lib/stream/
Step #4B Create the transcoding script
This is the script that actually does the transcoding, you should run this as a normal user. First create a bin directory in the user’s home directory to create the script.
cd
mkdir bin
touch ~/bin/stream.sh
chmod +x ~/bin/stream.sh
Here is the script itself, place it in the stream.sh file created above
#!/bin/bash
export HDHRID=10XXXXXX
# The following may work as well
#export HDHRID=`hdhomerun_config discover | cut -d\ -f3`
export TUNER=tuner1
export MYIP=`hostname -i`
export CHANNEL=23
export PROGRAM=1
export DEST="/var/lib/stream/mystream.m3u8"
# Choose the desired AUDIO Options
AUDIO_OPTS="-c:a aac -b:a 160000 -ac 2"
# Choose the desired VIDEO Options
VIDEO_OPTS="-vcodec libx264 -vprofile high -preset slow -b:v 2500000 -vf scale=-1:720"
VIDEO_OPTS="-vcodec libx264 -vprofile main -preset fast -b:v 500000 -threads 0"
VIDEO_OPTS="-vcodec libx264 -vprofile baseline -preset fast -b:v 500000 -threads 0"
VIDEO_OPTS="-vcodec libx264 -preset veryfast -threads 0"
OUTPUT_HLS="-hls_time 10 -hls_list_size 10 -start_number 1"
hdhomerun_config $HDHRID set /${TUNER}/channel auto:${CHANNEL}
hdhomerun_config $HDHRID set /${TUNER}/program ${PROGRAM}
hdhomerun_config $HDHRID save /${TUNER} - | ffmpeg -i - -y $AUDIO_OPTS $VIDEO_OPTS \
$OUTPUT_HLS $DEST
Once and a while you will have to clean up the stream folder
find /var/lib/stream -type f -mmin +5 -exec rm -f {} \;
Better to put this in cron
0,30 * * * * find /var/lib/stream -type f -mmin +5 -exec rm -f {} \;
Step #5 Configure Apache
Now you need to configure apache, do these as root, you can use these config files to password protect the site as well.
/etc/httpd/conf.d/stream.conf
Alias /stream /var/lib/stream <Directory "/var/lib/stream"> Options None AllowOverride None <IfVersion >= 2.3> <RequireAll> Require all granted </RequireAll> </IfVersion> <IfVersion < 2.3> Order allow,deny Allow from all </IfVersion> </Directory>
Here is the actual html file, will have it go under /var/www/html/livetv so you will access it via http://myserver/livetv
The magic is the hls.js java script, you can compile it and run it locally, but it is just javascript.
sudo mkdir /var/www/html/livetv sudo chown larry /var/www/html/livetv
Then as the user create the web page at /var/www/html/livetv/index.html
<html> <head> <title>Live TV</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script> <center> <h1>PRIVATE USE ONLY</h1> <video height="360" id="video" controls></video> </center> <script> var video = document.getElementById('video'); if (Hls.isSupported()) { var hls = new Hls({ debug: true, }); hls.loadSource('/stream/mystream.m3u8'); hls.attachMedia(video); hls.on(Hls.Events.MEDIA_ATTACHED, function () { video.muted = false; video.play(); }); } // hls.js is not supported on platforms that do not have Media Source Extensions (MSE) enabled. // When the browser has built-in HLS support (check using `canPlayType`), we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video element through the `src` property. // This is using the built-in support of the plain video element, without using hls.js. else if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = '/stream/mystream.m3u8'; video.addEventListener('canplay', function () { video.play(); }); } </script> <!-- injected in netlify post processing step --> </body> </html>
Restart the web server enable it on reboot
sudo systemctl enable --now httpd
Done
Just go to there server web page http://whatever/livetv
You can make it ssl, enable authentication, make it auto start on reboot, etc.
I feel like I am missing something. I get the page with a spinning circle.
I set the HDHRID in the script. What else should I look at?