Skip to content

Commit

Permalink
add config file, ability to override ffmpeg binary
Browse files Browse the repository at this point in the history
  • Loading branch information
dmamills committed Mar 30, 2019
1 parent f5ad4eb commit 4c02d08
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
25 changes: 25 additions & 0 deletions stream/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"FFMPEG_PATH": "",
"STREAM_URL": "rtmp://localhost/live/opensourceradio",

"FONT_SIZE": "10",
"FONT_COLOR": "#FFFFFF",
"FONT_BORDER": "#000000",

"VIDEO_WIDTH": "1280",
"VIDEO_HEIGHT": "720",
"VIDEO_CODEC": "libx264",
"VIDEO_BIT_RATE": "2500k",

"AUDIO_BIT_RATE": "128k",
"AUDIO_SAMPLE_RATE": "44100",
"AUDIO_CODEC": "aac",

"NUM_THREADS": "2",
"PRESET": "superfast",
"CRF": "28",
"BUFFER_SIZE": "2500k",
"FRAMES_PER_SECOND": "24",

"OVERLAY_TITLE": "opensourceradio"
}
15 changes: 9 additions & 6 deletions stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const musicMetadata = require('music-metadata');

const stream = require('./src/stream');
const { findCurrentSchedule } = require('./src/schedule');
const { printMetadata, printHeader, getNextSong, printSchedule, writeAppState, loadAppState } = require('./src/utils');
const { printMetadata, printHeader, getNextSong, printSchedule, writeAppState, loadAppState, getConfig } = require('./src/utils');

const videoPath = `${__dirname}/assets/video/dock.mp4`;
const audioPath = `${__dirname}/assets/audio/`;
const STREAM_URL = 'rtmp://localhost/live/opensourceradio';
const { STREAM_URL } = getConfig();

const appState = {
currentSchedule: null,
Expand Down Expand Up @@ -35,7 +36,7 @@ const playSong = () => {
.then(metadata => {
printMetadata(metadata);
return stream(STREAM_URL, videoPath, currentAudioPath, metadata)
});
}).catch(onSongError);
}

const onSongFinished = msg => {
Expand All @@ -50,13 +51,14 @@ const onSongFinished = msg => {

const onSongError = err => {
console.log(chalk.red('ffmpeg stream error:'), err);
console.log('Quitting process...');
process.exit(-1);


//WRITE STATE TO FILE?
const writeState = Object.create({}, appState);
writeState.lastSongPlayed = getNextSong(writeState.playlist, writeState.lastSongPlayed);
writeState.songCount++;

//WRITE STATE TO FILE?

writeAppState(writeState)
.then(() => {
process.exit(-1);
Expand Down Expand Up @@ -100,6 +102,7 @@ const radioInterval = () => {

console.log(chalk.magenta(`Welcome to opensource radio. 📻`));
console.log(chalk.magenta('Starting server...'))

loadAppState()
.then(state => {
console.log(chalk.blue('Loaded app state from file.'));
Expand Down
9 changes: 8 additions & 1 deletion stream/src/stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ const ffmpeg = require('fluent-ffmpeg');
const progress = require('cli-progress');
const chalk = require('chalk');

const { printFfmpegHeader } = require('../utils');
const { printFfmpegHeader, getConfig } = require('../utils');
const addOptions = require('./options');
const addFilters = require('./filters');

const { FFMPEG_PATH } = getConfig();

if(FFMPEG_PATH && FFMPEG_PATH !== "") {
console.log(chalk.magenta('Setting custom ffmpeg path: ', FFMPEG_PATH));
ffmpeg.setFfmpegPath(FFMPEG_PATH);
}

const makeProgressBar = () => {
return new progress.Bar({ format: 'Audio Progress {bar} {percentage}% | Time Playing: {duration_formatted} |' }, progress.Presets.shades_classic);
}
Expand Down
29 changes: 16 additions & 13 deletions stream/src/stream/options.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const WIDTH = 1280;
const HEIGHT = 720;
const { getConfig } = require('../utils');

const VIDEO_CODEC = 'libx264';
const VIDEO_BIT_RATE = '2500k';
const AUDIO_BIT_RATE = '128k';
const {
VIDEO_WIDTH,
VIDEO_HEIGHT,
VIDEO_CODEC,
VIDEO_BIT_RATE,

const AUDIO_SAMPLE_RATE= '44100';
const AUDIO_CODEC = 'aac'
AUDIO_BIT_RATE,
AUDIO_SAMPLE_RATE,
AUDIO_CODEC,

const NUM_THREADS = '2';
const PRESET = 'superfast';
const CRF = '28';
const BUFFER_SIZE = '2500k';
const FRAMES_PER_SECOND = '24';
NUM_THREADS,
PRESET,
CRF,
BUFFER_SIZE,
FRAMES_PER_SECOND
} = getConfig();

const addOptions = (metadata) => {

Expand All @@ -30,7 +33,7 @@ const addOptions = (metadata) => {
`-pix_fmt yuv420p`
];

outputOptions.push(`-s ${WIDTH}x${HEIGHT}`);
outputOptions.push(`-s ${VIDEO_WIDTH}x${VIDEO_HEIGHT}`);

outputOptions.push(`-b:v ${VIDEO_BIT_RATE}`);
outputOptions.push(`-minrate ${VIDEO_BIT_RATE}`);
Expand Down
14 changes: 10 additions & 4 deletions stream/src/stream/overlay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { getConfig } = require('../utils');

const {
FONT_SIZE,
FONT_COLOR,
FONT_BORDER,
OVERLAY_TITLE
} = getConfig();

const FONT_PATH = `${process.cwd()}/assets/font/scp.ttf`;
const FONT_SIZE = '10'
const FONT_COLOR = '#FFFFFF';
const FONT_BORDER = "#000000";
const X_POSITION = 2;

const sanitizeText = str => {
Expand All @@ -28,7 +34,7 @@ const addOverlay = metadata => {
const overlayTextItems = [];
let yPosition = 5;

overlayTextItems.push(createOverlayText('opensourceradio', X_POSITION, yPosition));
overlayTextItems.push(createOverlayText(OVERLAY_TITLE, X_POSITION, yPosition));
yPosition += 8;

if(common.artist) {
Expand Down
6 changes: 3 additions & 3 deletions stream/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs');
const moment = require('moment');
const chalk = require('chalk');
const getFullPath = name => { return `${__dirname}/../assets/audio/${name}`; }
const pad = n => n < 10 ? `0${n}` : n;
const getConfig = () => require('../config.json');

const pad = n => n < 10 ? `0${n}` : n;
const TIME_FORMAT = 'MMM DD YYYY hh:mm a';

const printMetadata = metadata => {
Expand Down Expand Up @@ -91,7 +91,6 @@ function loadAppState() {
}

module.exports = {
getFullPath,
printHeader,
printMetadata,
printSchedule,
Expand All @@ -101,4 +100,5 @@ module.exports = {
writeAppState,
loadAppState,
TIME_FORMAT,
getConfig
};

0 comments on commit 4c02d08

Please sign in to comment.