zorchenhimer / MovieNight

Single instance video streaming server with integrated chat.

Home Page:https://discord.gg/F2VSgjJ

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migrate to HLS Streaming

hadichaudhri opened this issue · comments

This would allow for playback of x265 footage as well as everything that's currently supported.

commented

Supporting HLS on the server is surprisingly easy. I've been able to do it locally by swapping out the video.js flv.js file with one that's aimed at HLS.

The trouble is generating the HLS stream in the first place. During my testing I was manually running ffmpeg and streaming through that to a locally running MovieNight server. I'd need a way to automate the process and host it on a separate server from the one running MovieNight. It should be possible, but I haven't put enough work into a solution yet.

commented

iirc, this was what i used: https://github.com/video-dev/hls.js/ It replaces flv.js.

You'll probably need to tweak the initPlayer() function in video.js.

commented

+1

Do you mind sharing your tweaked initPlayer() for HLS?

/// <reference path="./both.js" />

function initPlayer() {
    if (navigator.userAgent.match(/(iPhone|iPod|iPad)/i)) {
        var videoElement = document.getElementById("videoElement");
        videoElement.src = "https://YOUR_SERVER_HERE/master.m3u8";
        videoElement.autoplay = true;
    }
    else if(Hls.isSupported()) {
        var videoElement = document.getElementById("videoElement");
        var hls = new Hls();
        hls.loadSource("https://YOUR_SERVER_HERE/master.m3u8");
        hls.attachMedia(videoElement);
        hls.on(Hls.Events.MANIFEST_PARSED,function() {
          videoElement.play();
        });
    }
}

window.addEventListener("load", in
itPlayer);

commented

During my testing I was manually running ffmpeg and streaming through that to a locally running MovieNight server.

Did anyone get this to work? and could possibly provide a tweaked ffmpeg command for HLS? I too am running ffmpeg on the same server as MovieNight. Streaming FLV to MovieNight, I was using an ffmpeg command similar to:
ffmpeg -re -i inputfile.mp4 -c:v libx264 -c:a aac -f flv "rtmp://127.0.0.1/live/streamkey"

I have tried using the initPlayer() function as provided by @docwho76 but I'm not understanding something about all the moving parts when trying to livestream HLS rather than FLV using ffmpeg.

Hey @rnyn,

It's a long time sine I didn't try HLS with MN.
As far as I remember, when you run an ffmpep hls transformation, you get an entry flux, wich can a file or anything and you have a directory as transformation out. After you might configure a Nginx (or any equivalent) to handle the providing of the hls stream.

All that to say, as far as I remember, you can't push the hls flux directly into MovieNight. Am I right @zorchenhimer ?

commented

Currently, the HLS stream completely circumvents the MovieNight server due to how HLS is structured vs RTMP.

When streaming via FLV (RMTP) it's a single, constant data stream from the host to the server. This stream of data gets io.Copy()'d to each client that's connected.

HLS is a whole different ball game. It's all coordinated by a playlist file on the server. This playlist file is constantly updated with short (~4sec) video files. These video files are what's downloaded by the client's player. All of this stuff is hosted by a very basic file serve mechanism. In my current setup, i just point nginx to the folder with all the video data and the client's player figures out the rest.

What I'd like to get rolled into the MovieNight server eventually is the management of the playlist and the serving of the video files. I currently jump through some hoops to get ffmpeg to cooperate and output the right stuff in the playlist, but it would be a lot nicer if i could just point it to my server and not have to worry about fixing the filenames or paths or anything.

As a side note, ffmpeg is not running on the same server that hosts MovieNight. Video encoding is extremely expensive and will bring the hosting server to its knees without even trying. My current setup involves three machines: the Host with OBS, an encoding server running ffmpeg, and a remote server hosting MovieNight and the encoded files.

Anyway, after all that, I might as well share my ffmpeg script/command. This splits the source stream into 640x360@500k and 720x1280@4000k streams. (I think I've got some unused or extra options in here)

#!/bin/sh

ffmpeg  \
	-loglevel repeat+level+verbose \
	-listen 1 \
	-i rtmp://0.0.0.0/stream1/1234 \
	-preset veryfast \
	-filter_complex "[v:0]split=2[vtemp001][vtemp002];[vtemp001]scale=w=640:h=360[vout001];[vtemp002]scale=w=1280:h=720[vout002]" \
	-g 30 -sc_threshold 0 \
	-map [vout001] -c:v:0 libx264 -b:v:0 500k \
	-map [vout002] -c:v:1 libx264 -b:v:1 4000k \
	-map a:0 -map a:0 -c:a aac -b:a 128k -ac 2 \
	-f hls -hls_time 4 \
	-hls_flags delete_segments \
	-hls_delete_threshold 1 \
	-hls_list_size 5 \
	-hls_allow_cache 0 \
	-hls_flags independent_segments \
	-hls_base_url https://movienight.zorchenhimer.com/ \
	-master_pl_name master.m3u8 \
	-hls_segment_filename hls/stream_%v/data%06d.ts \
	-strftime_mkdir 1 \
	-var_stream_map "v:0,a:0 v:1,a:1" \
	-method PUT -multiple_requests 0 \
	-chunked_post 1 \
	-reconnect_at_eof -reconnect_streamed  \
	hls/master_%v.m3u8

The output for this command is the hls folder in the same directory. This folder is mounted to my remote server hosting MovieNight with sshfs (I'm using public key auth for this):

#!/bin/sh

sshfs glutton@akatsuki.zorchenhimer.com:/home/glutton/hls/ /home/nick/strem/hls/

The last piece of the puzzle is OBS sending a stream over to ffmpeg:
image