├── .gitignore ├── .dockerignore ├── .travis.yml ├── support ├── systemd │ └── node-ffmpeg-mpegts-proxy.service ├── upstart │ └── node-ffmpeg-mpegts-proxy.conf └── sysvinit │ └── node-ffmpeg-mpegts-proxy ├── examples └── sources.json ├── Dockerfile ├── libs ├── avconv │ ├── avstream.js │ └── avconv.js ├── options.js └── sources.js ├── package.json ├── Vagrantfile ├── CHANGELOG.md ├── node-ffmpeg-mpegts-proxy.js ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vagrant 3 | node_modules 4 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | support 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: required 3 | dist: trusty 4 | services: 5 | - docker 6 | node_js: 7 | - "0.12" 8 | - "4" 9 | - "6" 10 | - "8" 11 | - "10" 12 | script: 13 | - docker build -t jalle19/node-ffmpeg-mpegts-proxy . 14 | -------------------------------------------------------------------------------- /support/systemd/node-ffmpeg-mpegts-proxy.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=node-ffmpeg-mpegts-proxy 3 | After=network.target 4 | After=syslog.target 5 | 6 | [Service] 7 | User=vagrant 8 | Group=vagrant 9 | Restart=always 10 | ExecStart=/usr/bin/nodejs ./node-ffmpeg-mpegts-proxy.js -p 9128 -s /etc/node-ffmpeg-mpegts-proxy/sources.json 11 | WorkingDirectory=/vagrant 12 | Environment=NODE_ENV=production 13 | 14 | [Install] 15 | WantedBy=multi-user.target 16 | -------------------------------------------------------------------------------- /support/upstart/node-ffmpeg-mpegts-proxy.conf: -------------------------------------------------------------------------------- 1 | description "Simple proxy for leveraging ffmpeg to convert any source URL into MPEG-TS over HTTP" 2 | author "Sam Stenvall" 3 | 4 | start on filesystem and started networking 5 | stop on runlevel [016] 6 | 7 | respawn 8 | 9 | setuid vagrant 10 | chdir /vagrant 11 | 12 | env NODE_ENV=production 13 | exec /usr/bin/nodejs ./node-ffmpeg-mpegts-proxy.js -p 9128 -s /etc/node-ffmpeg-mpegts-proxy/sources.json >> /var/log/node-ffmpeg-mpegts-proxy.log 2>&1 14 | -------------------------------------------------------------------------------- /examples/sources.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Channel One", 4 | "provider": "Provider One", 5 | "url": "/channel1", 6 | "source": "http://example.com/channel1", 7 | "prescript": "/tmp/pre.sh", 8 | "postscript": "/tmp/post.sh" 9 | }, 10 | { 11 | "name": "Channel Two", 12 | "provider": "Provider Two", 13 | "url": "/channel2", 14 | "source": "http://example.com/channel1" 15 | }, 16 | { 17 | "name": "Channel Three", 18 | "provider": "Provider Three", 19 | "url": "/channel3", 20 | "source": "http://example.com/channel1" 21 | } 22 | ] -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:8 2 | 3 | # Enable jessie-backports and install ffmpeg 4 | RUN echo 'deb http://deb.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/backports.list && \ 5 | apt-get -qy update && \ 6 | apt-get -qy install ffmpeg && \ 7 | rm -rf /var/lib/apt/lists/* 8 | 9 | # Install the application 10 | WORKDIR /home/node/node-ffmpeg-mpegts-proxy 11 | COPY package*.json ./ 12 | RUN npm install --production 13 | COPY . . 14 | 15 | # Configure the run environment 16 | EXPOSE 9128 17 | USER node 18 | CMD ["node", "node-ffmpeg-mpegts-proxy.js", "-p", "9128", "-a", "/usr/bin/ffmpeg", "-s", "/home/node/node-ffmpeg-mpegts-proxy/sources.json"] 19 | -------------------------------------------------------------------------------- /libs/avconv/avstream.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require('util'), 4 | Stream = require('stream'); 5 | 6 | function AvStream(options) { 7 | // Allow use without new operator 8 | if (!(this instanceof AvStream)) { 9 | return new AvStream(options); 10 | } 11 | 12 | Stream.Duplex.call(this, options); 13 | } 14 | 15 | // AvStream inherits the Duplex stream prototype 16 | util.inherits(AvStream, Stream.Duplex); 17 | 18 | AvStream.prototype._read = function _read(n) {}; 19 | 20 | AvStream.prototype._write = function _write(chunk, encoding, callback) { 21 | // Data written to the stream should be emitted back as 'inputData' 22 | this.emit('inputData', chunk); 23 | 24 | callback(); 25 | }; 26 | 27 | module.exports = AvStream; 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-ffmpeg-mpegts-proxy", 3 | "description": "Simple proxy for leveraging ffmpeg to convert any source URL into MPEG-TS over HTTP", 4 | "version": "0.8.0", 5 | "main": "./node-ffmpeg-mpegts-proxy.js", 6 | "author": { 7 | "name": "Sam Stenvall", 8 | "email": "neggelandia@gmail.com" 9 | }, 10 | "keywords": [], 11 | "license": "GPL-2.0", 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/Jalle19/node-ffmpeg-mpegts-proxy.git" 15 | }, 16 | "bugs": { 17 | "url": "http://github.com/Jalle19/node-ffmpeg-mpegts-proxy/issues" 18 | }, 19 | "dependencies": { 20 | "chokidar": "^1.6.1", 21 | "command-exists": "^1.0.2", 22 | "executable": "^4.1.0", 23 | "sleep": "^4.0.0", 24 | "winston": "~0.8.1", 25 | "yargs": "~1.3.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | Vagrant.configure("2") do |config| 5 | 6 | # sysvinit based 7 | config.vm.define "wheezy" do |wheezy| 8 | wheezy.vm.box = "debian/wheezy64" 9 | 10 | # install ffmpeg 11 | wheezy.vm.provision "shell", inline: <<-SHELL 12 | sudo apt-get install -y ffmpeg 13 | SHELL 14 | end 15 | 16 | # upstart based 17 | config.vm.define "trusty" do |trusty| 18 | trusty.vm.box = "geerlingguy/ubuntu1404" 19 | 20 | # install avconv 21 | trusty.vm.provision "shell", inline: <<-SHELL 22 | sudo apt-get install -y libav-tools 23 | SHELL 24 | end 25 | 26 | # systemd based 27 | config.vm.define "xenial" do |xenial| 28 | xenial.vm.box = "geerlingguy/ubuntu1604" 29 | 30 | # install ffmpeg 31 | xenial.vm.provision "shell", inline: <<-SHELL 32 | sudo apt-get install -y ffmpeg 33 | SHELL 34 | end 35 | 36 | # common provisioning (install nodejs) 37 | config.vm.provision "shell", inline: <<-SHELL 38 | sudo apt-get update 39 | sudo apt-get install -y curl ca-certificates apt-transport-https 40 | curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - 41 | sudo apt-get install -y nodejs 42 | SHELL 43 | 44 | end 45 | -------------------------------------------------------------------------------- /libs/options.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns the avconv options needed for the specified source 3 | * @param {type} source 4 | * @returns array the options array 5 | */ 6 | var getAvconvOptions = function(source) { 7 | var options = getInputAvconvOptions(source); 8 | return options.concat(getOutputAvconvOptions(source)); 9 | }; 10 | 11 | /** 12 | * Returns the input options (including -i) 13 | * @param {type} source 14 | * @returns array 15 | */ 16 | var getInputAvconvOptions = function(source) { 17 | var options = []; 18 | 19 | // Optionally disable realtime streaming 20 | if (source.realtime !== false) { 21 | options = [ 22 | '-re' 23 | ]; 24 | } 25 | 26 | if (source.avconvOptions !== undefined && source.avconvOptions.input !== undefined) 27 | options = options.concat(source.avconvOptions.input); 28 | 29 | return options.concat([ 30 | '-i', 31 | source.source 32 | ]); 33 | }; 34 | 35 | /** 36 | * Returns the output options 37 | * @param {type} source 38 | * @returns {options} 39 | */ 40 | var getOutputAvconvOptions = function(source) { 41 | var options = [ 42 | '-vcodec', 'copy', 43 | '-acodec', 'copy', 44 | '-metadata', 'service_provider=' + source.provider, 45 | '-metadata', 'service_name=' + source.name, 46 | '-f', 'mpegts' 47 | ]; 48 | 49 | if (source.avconvOptions !== undefined && source.avconvOptions.output !== undefined) 50 | options = options.concat(source.avconvOptions.output); 51 | 52 | return options.concat(['pipe:1']); 53 | }; 54 | 55 | var exports = module.exports = {}; 56 | exports.getAvconvOptions = getAvconvOptions; 57 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change log 2 | 3 | ## 0.8.0 4 | 5 | * add ability to disable the "-re" flag for specific sources 6 | 7 | ## 0.7.1 8 | 9 | * exit immediately if avconv is not found or not executable 10 | * fix various issues with source reloading 11 | 12 | ## 0.7.0 13 | 14 | * show an error if pre/post-scripts don't exist or are not executable 15 | * pass more parameters to pre/post-scripts 16 | * log when streaming has started successfully 17 | * throttle avconv restarts to once per 5 seconds 18 | * remove some unused code 19 | * add startup scripts for Upstart and Systemd in addition to SysVinit 20 | 21 | ## 0.6.0 22 | 23 | * support specifying the HTTP proxy to use per stream 24 | * don't log some good avconv exit codes as errors 25 | * better error message when there's something wrong with the source definitions 26 | * allow overridng the avconv path on a per-stream basis 27 | 28 | ## 0.5.0 29 | 30 | * add ability to run pre/post scripts before and after streaming begins 31 | * bump nodejs version requirements to >= 0.12 32 | 33 | ## 0.4.0 34 | 35 | * bundle the "avconv" dependency so we can more easily fix things in it 36 | * add an option to change the listen address 37 | 38 | ## 0.3.0 39 | 40 | * don't crash when the source definitions contain malformed JSON 41 | * allow specifying custom avconv options per source 42 | 43 | ## 0.2.3 44 | 45 | * reload the source definitions whenever the file changes 46 | 47 | ## 0.2.2 48 | 49 | * more logging improvements 50 | * handle leading slashes in source URL definitions 51 | 52 | ## 0.2.1 53 | 54 | * minor logging improvements 55 | 56 | ## 0.2 57 | 58 | First tagged release 59 | -------------------------------------------------------------------------------- /libs/avconv/avconv.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var spawn = require('child_process').spawn, 4 | AvStream = require('./avstream'); 5 | 6 | module.exports = function avconv(params, binaryPath, environment) { 7 | binaryPath = binaryPath || "avconv"; 8 | 9 | var stream = new AvStream(), 10 | // todo: use a queue to deal with the spawn EMFILE exception 11 | // see http://www.runtime-era.com/2012/10/quick-and-dirty-nodejs-exec-limit-queue.html 12 | // currently I have added a dirty workaround on the server by increasing 13 | // the file max descriptor with 'sudo sysctl -w fs.file-max=100000' 14 | avconv = spawn(binaryPath, params, environment); 15 | 16 | // General avconv output is always written into stderr 17 | if (avconv.stderr) { 18 | avconv.stderr.setEncoding('utf8'); 19 | 20 | avconv.stderr.on('data', function(data) { 21 | // Emit conversion information as messages 22 | stream.emit('message', data); 23 | }); 24 | } 25 | 26 | // When avconv outputs anything to stdout, it's probably converted data 27 | if (avconv.stdout) { 28 | avconv.stdout.on('data', function(data) { 29 | stream.push(data) 30 | }); 31 | } 32 | 33 | avconv.on('error', function(data) { 34 | stream.emit('error', data); 35 | }); 36 | 37 | // New stdio api introduced the exit event not waiting for open pipes 38 | var eventType = avconv.stdio ? 'close' : 'exit'; 39 | 40 | avconv.on(eventType, function(exitCode, signal) { 41 | stream.end(); 42 | stream.emit('exit', exitCode, signal); 43 | }); 44 | 45 | stream.kill = function() { 46 | avconv.kill(); 47 | }; 48 | 49 | return stream; 50 | }; 51 | -------------------------------------------------------------------------------- /libs/sources.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var chokidar = require('chokidar'); 3 | 4 | /** 5 | * The name of the file that contains the source definitions 6 | * @type _filename 7 | */ 8 | var filename; 9 | 10 | /** 11 | * Holds the currently valid source definitions 12 | * @type newSources 13 | */ 14 | var sources; 15 | 16 | /** 17 | * Callbacks 18 | * @type type 19 | */ 20 | var onSourceChange; 21 | var onParserError; 22 | var onLoad ; 23 | 24 | /** 25 | * Loads source definitions from the specified file and attach the callbacks 26 | * @param {type} _filename 27 | * @param callback 28 | * @param callback 29 | * @param callback 30 | * @returns {undefined} 31 | */ 32 | var load = function(_filename, cbonSourceChange, cbonParserError, cbonLoad) { 33 | filename = _filename; 34 | onSourceChange = cbonSourceChange; 35 | onParserError = cbonParserError; 36 | onLoad = cbonLoad; 37 | 38 | _load(); 39 | 40 | // Watch the file for changes and reload when changed 41 | var watch = chokidar.watch(filename); 42 | watch.on('change', function() { 43 | onSourceChange(); 44 | _load(); 45 | }); 46 | }; 47 | 48 | /** 49 | * Returns the source that has the specified URL, or null if no such source 50 | * exists 51 | * @param {type} url 52 | * @returns {newSources|sources} 53 | */ 54 | var getByUrl = function(url) { 55 | var source = null; 56 | 57 | for (var i = 0; i < sources.length; i++) 58 | { 59 | // Ensure there's a leading slash and no trailing slashes 60 | var sourceUrl = '/' + sources[i].url.replace(/^\/|\/$/g, ''); 61 | 62 | if (sourceUrl === url) 63 | { 64 | source = sources[i]; 65 | break; 66 | } 67 | } 68 | 69 | return source; 70 | }; 71 | 72 | /** 73 | * Attempts to load the source file, replacing the source definitiosn on success 74 | * @returns {undefined} 75 | */ 76 | var _load = function() { 77 | // Try to parse the file 78 | try { 79 | var newSources = JSON.parse(fs.readFileSync(filename, 'utf8')); 80 | } 81 | catch (e) { 82 | onParserError(e); 83 | return; 84 | } 85 | 86 | // Replace sources with the new ones 87 | onLoad(newSources.length); 88 | sources = newSources; 89 | }; 90 | 91 | // Export functions 92 | var exports = module.exports = {}; 93 | exports.getByUrl = getByUrl; 94 | exports.load = load; 95 | -------------------------------------------------------------------------------- /support/sysvinit/node-ffmpeg-mpegts-proxy: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: node-ffmpeg-mpegts-proxy 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: node-ffmpeg-mpegts-proxy 9 | # Description: node-ffmpeg-mpegts-proxy 10 | ### END INIT INFO 11 | 12 | # MODIFY THESE 13 | dir="/vagrant" 14 | user="nobody" 15 | cmd="/usr/bin/nodejs node-ffmpeg-mpegts-proxy.js -p 9128 -s /etc/node-ffmpeg-mpegts-proxy/sources.json >> /var/log/node-ffmpeg-mpegts-proxy.log 2>&1" 16 | 17 | name=`basename $0` 18 | pid_file="/var/run/$name.pid" 19 | stdout_log="/var/log/$name.log" 20 | 21 | get_pid() { 22 | cat "$pid_file" 23 | } 24 | 25 | is_running() { 26 | [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1 27 | } 28 | 29 | case "$1" in 30 | start) 31 | if is_running; then 32 | echo "Already started" 33 | else 34 | echo "Starting $name" 35 | cd "$dir" 36 | sudo -u "$user" $cmd >> "$stdout_log" 2>&1 & 37 | echo $! > "$pid_file" 38 | if ! is_running; then 39 | echo "Unable to start, see $stdout_log" 40 | exit 1 41 | fi 42 | fi 43 | ;; 44 | stop) 45 | if is_running; then 46 | echo -n "Stopping $name.." 47 | kill `get_pid` 48 | for i in {1..10} 49 | do 50 | if ! is_running; then 51 | break 52 | fi 53 | 54 | echo -n "." 55 | sleep 1 56 | done 57 | echo 58 | 59 | if is_running; then 60 | echo "Not stopped; may still be shutting down or shutdown may have failed" 61 | exit 1 62 | else 63 | echo "Stopped" 64 | if [ -f "$pid_file" ]; then 65 | rm "$pid_file" 66 | fi 67 | fi 68 | else 69 | echo "Not running" 70 | fi 71 | ;; 72 | restart) 73 | $0 stop 74 | if is_running; then 75 | echo "Unable to stop, will not attempt to start" 76 | exit 1 77 | fi 78 | $0 start 79 | ;; 80 | status) 81 | if is_running; then 82 | echo "Running" 83 | else 84 | echo "Stopped" 85 | exit 1 86 | fi 87 | ;; 88 | *) 89 | echo "Usage: $0 {start|stop|restart|status}" 90 | exit 1 91 | ;; 92 | esac 93 | 94 | exit 0 95 | -------------------------------------------------------------------------------- /node-ffmpeg-mpegts-proxy.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Require libraries 3 | */ 4 | var yargs = require('yargs'); 5 | var winston = require('winston'); 6 | var http = require("http"); 7 | var child_process = require('child_process'); 8 | var sleep = require('sleep'); 9 | var executable = require('executable'); 10 | var avconv = require('./libs/avconv/avconv'); 11 | var sources = require('./libs/sources'); 12 | var options = require('./libs/options'); 13 | var commandExists = require('command-exists'); 14 | 15 | /* 16 | * Define some global constants 17 | */ 18 | var STREAMING_RESTART_DELAY_SECONDS = 5; 19 | var MINIMUM_BYTES_RECEIVED_SUCCESS = 4096; 20 | 21 | /* 22 | * Read command line options 23 | */ 24 | var argv = yargs 25 | .usage('Usage: $0 -p -s [-a ] [-q | -v | -l]') 26 | .alias('p', 'port') 27 | .alias('l', 'listen') 28 | .alias('a', 'avconv') 29 | .alias('s', 'sources') 30 | .alias('q', 'quiet') 31 | .alias('v', 'verbose') 32 | .demand(['p', 's']) 33 | .default('a', 'avconv') 34 | .default('l', '::') 35 | .describe('p', 'The port the HTTP server should be listening on') 36 | .describe('l', 'The address to listen on') 37 | .describe('a', 'The path to avconv, defaults to just "avconv"') 38 | .describe('s', 'The path to sources.json, defaults to "data/sources.json"') 39 | .describe('q', 'Disable all logging to stdout') 40 | .describe('v', 'Enable verbose logging (shows the output from avconv)') 41 | .argv; 42 | 43 | /* 44 | * Configure logger 45 | */ 46 | winston.remove(winston.transports.Console); 47 | 48 | if (!argv.quiet) 49 | { 50 | winston.add(winston.transports.Console, { 51 | timestamp: true, 52 | colorize: true, 53 | level: argv.verbose ? 'silly' : 'debug' 54 | }); 55 | } 56 | 57 | /** 58 | * Configure the sources module 59 | */ 60 | var onSourceChange = function() { 61 | winston.info('Source definitions have changed, reloading ...'); 62 | }; 63 | 64 | var onParserError = function(error) { 65 | winston.info('Unable to read source definitions: %s', error.toString()); 66 | }; 67 | 68 | var onLoad = function(numSources) { 69 | winston.info('Loaded %d sources', numSources); 70 | }; 71 | 72 | sources.load(argv.sources, 73 | onSourceChange, 74 | onParserError, 75 | onLoad); 76 | 77 | /** 78 | * Check that the avconv is useable 79 | */ 80 | if (!argv.avconv) { 81 | argv.avconv = 'avconv'; 82 | } 83 | 84 | commandExists(argv.avconv, function(err, exists) { 85 | if (!exists) { 86 | winston.error('avconv not found or is not executable'); 87 | process.exit(); 88 | } 89 | }); 90 | 91 | /** 92 | * The main HTTP server process 93 | * @type @exp;http@call;createServer 94 | */ 95 | var server = http.createServer(function (request, response) { 96 | var remoteAddress = request.connection.remoteAddress; 97 | winston.debug('Got request for %s from %s', request.url, remoteAddress); 98 | 99 | // Find the source definition 100 | var source = sources.getByUrl(request.url); 101 | 102 | if (source === null) 103 | { 104 | winston.error('Unknown source %s', request.url); 105 | 106 | response.writeHead(404, {"Content-Type": "text/plain"}); 107 | response.write("404 Not Found\n"); 108 | response.end(); 109 | 110 | return; 111 | } 112 | 113 | // Run eventual pre-script 114 | if (source.prescript) 115 | { 116 | winston.debug("Executing pre-script %s", source.prescript); 117 | runPrePostScript(source.prescript, [source.source, source.url, source.provider, source.name]); 118 | } 119 | 120 | // Tell the client we're sending MPEG-TS data 121 | response.writeHead(200, { 122 | 'Content-Type': 'video/mp2t' 123 | }); 124 | 125 | // Define options for the child process 126 | var avconvOptions = options.getAvconvOptions(source); 127 | winston.silly("Options passed to avconv: " + avconvOptions); 128 | 129 | // Indicates whether avconv should be restarted on failure 130 | var shouldRestart = true; 131 | var stream = null; 132 | 133 | // Keep track of how much data has been pushed by avconv. We'll use this to determine whether streaming actually 134 | // started successfully 135 | var bytesRecieved = 0; 136 | var streamingStarted = false; 137 | 138 | /** 139 | * Spawns an avconv process and pipes its output to the response input 140 | * @returns {undefined} 141 | */ 142 | var streamingLoop = function() { 143 | // Add "http_proxy" to the avconv environment if it is defined 144 | var environment = process.env; 145 | 146 | if (source.http_proxy) { 147 | environment.http_proxy = source.http_proxy; 148 | } 149 | 150 | // Determine the avconv binary to use 151 | var avconvBinary = argv.a; 152 | 153 | if (source.avconv) { 154 | avconvBinary = source.avconv; 155 | } 156 | 157 | stream = avconv(avconvOptions, avconvBinary, environment); 158 | stream.pipe(response); 159 | 160 | // Kill the process on error 161 | stream.on('error', function() { 162 | stream.kill(); 163 | }); 164 | 165 | // Print avconv status messages 166 | stream.on('message', function(message) { 167 | winston.silly(message); 168 | bytesRecieved += message.length; 169 | 170 | // Check if streaming seems to have started 171 | if (bytesRecieved >= MINIMUM_BYTES_RECEIVED_SUCCESS && !streamingStarted) { 172 | winston.info('avconv started successfully'); 173 | streamingStarted = true; 174 | } 175 | }); 176 | 177 | // Respawn on exit 178 | stream.on('exit', function(code) { 179 | streamingStarted = false; 180 | var message = 'avconv exited with code %d'; 181 | 182 | // Don't log normal exits as errors. 255 happens when the client presses stop. 183 | if (code !== 0 && code !== 255) { 184 | winston.error(message, code); 185 | } else { 186 | winston.debug(message, code); 187 | } 188 | 189 | if (shouldRestart) 190 | { 191 | winston.info('%s still connected, restarting avconv after %d seconds ...', remoteAddress, 192 | STREAMING_RESTART_DELAY_SECONDS); 193 | 194 | // Throttle restart attempts, otherwise it will try to respawn as fast as possible 195 | sleep.sleep(STREAMING_RESTART_DELAY_SECONDS); 196 | streamingLoop(); 197 | } 198 | }); 199 | }; 200 | 201 | // Start serving data 202 | streamingLoop(); 203 | 204 | // Kill avconv when client closes the connection 205 | request.on('close', function () { 206 | winston.info('%s disconnected, stopping avconv', remoteAddress); 207 | 208 | shouldRestart = false; 209 | stream.kill(); 210 | 211 | // Run eventual post-script 212 | if (source.postscript) 213 | { 214 | winston.debug("Executing post-script %s", source.postscript); 215 | runPrePostScript(source.postscript, [source.source, source.url, source.provider, source.name]); 216 | } 217 | }); 218 | }); 219 | 220 | /** 221 | * Runs the specified script with the specified parameters. 222 | * 223 | * @param scriptPath 224 | * @param params 225 | */ 226 | var runPrePostScript = function(scriptPath, params) { 227 | try { 228 | if (executable.sync(scriptPath)) { 229 | child_process.spawnSync(scriptPath, params); 230 | } else { 231 | winston.error("The specified script is not executable"); 232 | } 233 | } 234 | catch (e) { 235 | winston.error("The specified script doesn't exist"); 236 | } 237 | }; 238 | 239 | // Start the server 240 | server.listen(argv.port, argv.l); 241 | winston.info('Server listening on port %d', argv.port); 242 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-ffmpeg-mpegts-proxy 2 | ======================== 3 | 4 | [![Build Status](https://travis-ci.org/Jalle19/node-ffmpeg-mpegts-proxy.svg?branch=master)](https://travis-ci.org/Jalle19/node-ffmpeg-mpegts-proxy) 5 | 6 | Simple proxy for leveraging ffmpeg to convert any source URL into MPEG-TS and serve it on demand over HTTP. It has been 7 | designed for proxying HLS streams for use as IPTV input in tvheadend, but it can be used with any source that can be 8 | handled by the `avconv` utility. Currently it simply remuxes the source stream into MPEG-TS and adds a service name 9 | (for automatic detection in tvheadend), no transcoding is performed. 10 | 11 | Since HLS input can be a bit unreliable, the converter process will be restarted automatically (without the HTTP 12 | response ending) until the client closes the connection (in which case the process is killed). 13 | 14 | ## Requirements 15 | 16 | * nodejs >= 0.12 17 | * avconv or ffmpeg 18 | 19 | ## Usage 20 | 21 | * Install the required libraries by running `npm install` in the project directory. You will have to run this command 22 | again if you update to a newer version. 23 | * Copy `examples/sources.json` someplace and modify it 24 | * Run the program using `nodejs node-ffmpeg-mpegts-proxy.js` 25 | 26 | ``` 27 | Usage: nodejs ./node-ffmpeg-mpegts-proxy.js -p [-a ] [-q | -v] [-s ] 28 | 29 | Options: 30 | -p, --port The port the HTTP server should be listening on [required] 31 | -l, --listen The address to listen on [default: "::"] 32 | -a, --avconv The path to avconv, defaults to just "avconv" [default: "avconv"] 33 | -s, --sources The path to sources.json [required] 34 | -q, --quiet Disable all logging to stdout 35 | -v, --verbose Enable verbose logging (shows the output from avconv) 36 | ``` 37 | 38 | Once the proxy is running, streams are available on the e.g. `http://localhost:9128/channel1`, assuming port 9128 is 39 | used and a source with the URL `/channel1` exists. 40 | 41 | ### Configuring sources 42 | 43 | Sources are read from the file specified when starting the program (use `examples/sources.json` as a starting point). 44 | The file contains an array of JSON objects with the following definition: 45 | 46 | * `name`: the service name 47 | * `provider`: the name of the service provider 48 | * `url`: the relative URL the stream will be available on when served 49 | * `source`: the source URL 50 | * `avconvOptions`: (optional) special avconv parameters for this source. This is an object containing two arrays, 51 | `input`and `output`. 52 | * `realtime`: (optional) whether to add the `-re` input flag to the input options. Normally this is what you want, but 53 | some sources may not work correctly without disabling this. Defaults to `true`. 54 | * `prescript`: (optional) script to run before transcoding starts. Useful if you need to bring up temporary VPN 55 | interfaces etc. Four arguments are passed to the script; the source URL, the relative stream URL, the provider name 56 | and the channel name. 57 | * `postscript`: (optional) same as `prescript` except it's run when streaming is stopped. 58 | * `http_proxy`: (optional) the HTTP proxy to use for the source (e.g. `http://proxy.example.com:8080`) 59 | * `avconv`: (optional) source-specific override of the avconv binary to use. This can be useful if you for some reason 60 | need to use a special version off ffmpeg just to play a specific source. 61 | 62 | The program listens to changes made to the source file and reloads it automatically whenever it is changed. The main 63 | idea behind this is to support source URLs that contain parameter that change frequently and need to be adapted for 64 | (e.g. session IDs). If the changes you make result in the file being unreadable (malformed JSON) it will complain 65 | about that and continue using the previous source definitions (if any). Below is an excerpt from the example source 66 | file. 67 | 68 | ``` 69 | [ 70 | { 71 | "name": "Channel One", 72 | "provider": "Provider One", 73 | "url": "/channel1", 74 | "source": "http://iptv.example.com/channel1.m3u8" 75 | }, 76 | ... 77 | ] 78 | ``` 79 | 80 | #### Custom avconv parameters 81 | 82 | If your sources require additional parameters to work correctly (most commonly because the source uses MP4 as 83 | container) you can append to the default ones by using the `avconvOptions` source parameter. Here is a complete 84 | example: 85 | 86 | ``` 87 | [ 88 | { 89 | "name": "Channel One", 90 | "provider": "Provider One", 91 | "url": "/channel1", 92 | "source": "rtmp://example.com:1935/live playpath=test live=1 pageUrl=http://example.com/foo token=bar timeout=10", 93 | "avconvOptions": { 94 | "input": [ 95 | "fflags", "+genpts" 96 | ], 97 | "output": [ 98 | "-bsf", "h264_mp4toannexb" 99 | ] 100 | } 101 | } 102 | ] 103 | ``` 104 | 105 | In the example above, the options `fflags +genpts` will be injected before the input source is specified (which means 106 | those options apply to the input, and `-bsf h264_mp4toannexb` will be injected before the output destination is 107 | specified (which means those options apply to the output). 108 | 109 | If you only need to specify output parameters you can omit the `input` key completely. 110 | 111 | #### Commonly needed parameters 112 | 113 | In most cases you don't need any extra parameters, although one often needed one is the `-bsf h264_mp4toannexb` output 114 | option (as in the example above). If you enable silly debugging mode (`-v`) and get an 115 | `H.264 bitstream malformed, no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)` 116 | error message, this is what you need. 117 | 118 | ### Running as a service 119 | 120 | You can turn the proxy into a proper daemon that can be started and stopped like other services. Start by placing your 121 | source definitions in `/etc/node-ffmpeg-mpegts-proxy/sources.json`, then follow the instructions below for your 122 | startup system. 123 | 124 | #### Systemd (Ubuntu >= 16.04, Debian >= Jessie) 125 | 126 | * Copy `support/systemd/node-ffmpeg-mpegts-proxy.service` to `/lib/systemd/system` and modify it if necessary (e.g. 127 | to change the parameters passed to it or the user it should run as) 128 | * Run `sudo systemctl enable node-ffmpeg-mpegts-proxy.service` to enable the service 129 | * Run `sudo systemctl start node-ffmpeg-mpegts-proxy.service` to start the service 130 | 131 | If you make any changes to `/lib/systemd/system/node-ffmpeg-mpegts-proxy.service` after you've enabled the service you 132 | will have to run `sudo systemctl daemon-reload` for the changes to take effect. 133 | 134 | The output from the application is logged to `/var/log/node-ffmpeg-mpegts-proxy.log` 135 | 136 | #### Upstart (Ubuntu 14.04) 137 | 138 | * Copy `support/upstart/node-ffmpeg-mpegts-proxy.conf` to `/etc/init/` and modify it if necessary (e.g. to change the 139 | parameters passed to it or the user it should run as) 140 | * Run `sudo service node-ffmpeg-mpegts-proxy start` 141 | 142 | The output from the application is logged to `/var/log/upstart/node-ffmpeg-mpegts-proxy.log` 143 | 144 | #### SysVinit (Debian Wheezy) 145 | 146 | * Copy `sysvinit/node-ffmpeg-mpegts-proxy` to `/etc/init.d`, modify it if necessary (e.g. to change the parameters 147 | passed to it or the user it should run as) 148 | * Run `sudo chmod +x /etc/init.d/node-ffmpeg-mpegts-proxy` 149 | * Run `sudo update-rc.d node-ffmpeg-mpegts-proxy defaults` to enable the service on startup 150 | * Run `sudo /etc/init.d/node-ffmpeg-mpegts-proxy start` to start the service 151 | 152 | The output from the application is logged to `/var/log/node-ffmpeg-mpegts-proxy.log` 153 | 154 | ## Running the application with Docker 155 | 156 | Assuming the absolute path to your `sources.json` file is `/tmp/sources.json` and you want to run the application 157 | on port 9128, run the following command: 158 | 159 | ```bash 160 | docker run -p 9128:9128 -v /tmp/sources.json:/home/node/node-ffmpeg-mpegts-proxy/sources.json -d jalle19/node-ffmpeg-mpegts-proxy 161 | ``` 162 | 163 | You can verify that the application is running by running `docker ps`. You can see the output from the application by 164 | running `docker logs -f `, where `` is the container ID from the `docker ps` command. 165 | 166 | ### Building your own version of the container 167 | 168 | If you need to do major customizations to the Docker image, you might wanna build it yourself with your changes: 169 | 170 | ```bash 171 | docker build -t jalle19/node-ffmpeg-mpegts-proxy . 172 | ``` 173 | 174 | ## Development environment 175 | 176 | Install nodejs and ffmpeg locally, no virtual machines required. 177 | 178 | In order to easily test the startup/service scripts there is a `Vagrantfile` which starts three separate virtual 179 | machines, one for each supported init system. 180 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------