Coder Social home page Coder Social logo

codehaven9ja / hls-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from t-mullen/hls-server

0.0 2.0 0.0 9.95 MB

Simple HTTP middleware for serving HTTP Live Streaming (HLS) compatible media streams.

License: MIT License

JavaScript 90.42% Shell 9.58%

hls-server's Introduction

hls-server

Standard - JavaScript Style Guide

Simple HTTP middleware for serving HTTP Live Streaming (HLS) compatible media streams.

Usage

First you need a compatible media stream (see Producing Streams)

Fast way:

require('hls-server')(8000)

Detailed way:

var HLSServer = require('hls-server')
var http = require('http')

var server = http.createServer()
var hls = new HLSServer(server, {
  path: '/streams',     // Base URI to output HLS streams
  dir: 'public/videos'  // Directory that input files are stored
})
server.listen(8000)

Producing Streams

HLS can only stream files that have been properly encoded and segmented. FFMPEG is great for this.
Here is how to do it with node-fluent-ffmpeg.

var ffmpeg = require('fluent-ffmpeg')

function callback() { // do something when encoding is done }

fmpeg('input.mp4', { timeout: 432000 }).addOptions([
    '-profile:v baseline',
    '-level 3.0',
    '-s 640x360',
    '-start_number 0',
    '-hls_time 10',
    '-hls_list_size 0',
    '-f hls'
  ]).output('public/videos/output.m3u8').on('end', callback).run()

To create segments from an existing RTMP stream, use the following node-fluent-ffmpeg command. You can expect 20-50 seconds of latency, depending on hardware.

var ffmpeg = require('fluent-ffmpeg')

// host, port and path to the RTMP stream
var host = 'localhost'
var port = '1935'
var path = '/live/test'

function callback() { // do something when stream ends and encoding finshes }

fmpeg('rtmp://'+host+':'+port+path, { timeout: 432000 }).addOptions([
    '-c:v libx264',
    '-c:a aac',
    '-ac 1',
    '-strict -2',
    '-crf 18',
    '-profile:v baseline',
    '-maxrate 400k',
    '-bufsize 1835k',
    '-pix_fmt yuv420p',
    '-hls_time 10',
    '-hls_list_size 6',
    '-hls_wrap 10',
    '-start_number 1'
  ]).output('public/videos/output.m3u8').on('end', callback).run()

To publish from an RTMP client like OBS, use a RTMP server like rtmp-server-nodejs to echo the stream.

NOTE: Transcoding live streams is very CPU-intensive. Most consumer hardware won't be able to handle transcoding more than a few streams.

CLI Tool

This package includes a CLI tool that can be installed globally with npm install -g hls-server.

To use, navigate to the directory where your .ts files are stored and run hlsserver in a command prompt. This will start a server on port 8000. (Use hlsserver --help to see additional options.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.