Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Make Strings Safe Again! (#85)
Browse files Browse the repository at this point in the history
First version of safeStrings so ffmpeg does not get confused by our inputs!

This references #68
  • Loading branch information
Andreas Heimann authored and torch2424 committed Oct 10, 2018
1 parent 33753a3 commit 14114de
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/generate/template/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"artist": {
"enabled": true,
"label": "Artist\\: ",
"label": "Artist: ",
"font_color": "#FFFFFF",
"font_border": "#000000",
"font_size": "10",
Expand All @@ -50,7 +50,7 @@
},
"album": {
"enabled": false,
"label": "Album\\: ",
"label": "Album: ",
"font_color": "#FFFFFF",
"font_border": "#000000",
"font_size": "10",
Expand All @@ -59,7 +59,7 @@
},
"song": {
"enabled": true,
"label": "Song\\: ",
"label": "Song: ",
"font_color": "#FFFFFF",
"font_border": "#000000",
"font_size": "10",
Expand Down
13 changes: 9 additions & 4 deletions src/stream/overlayText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Overlay text for the stream
const safeStrings = require('./safeStrings');

const getOverlayTextString = async (path, config, typeKey, metadata) => {
// Create our overlay
Expand All @@ -15,8 +16,9 @@ const getOverlayTextString = async (path, config, typeKey, metadata) => {
// Check if we have a title option
if (overlayConfigObject.title && overlayConfigObject.title.enabled) {
const itemObject = overlayConfigObject.title;
const safeText = safeStrings.forFilter( itemObject.text );
let itemString =
`drawtext=text='${itemObject.text}'` +
`drawtext=text='${safeText}'` +
`:fontfile=${fontPath}` +
`:fontsize=(w * ${itemObject.font_size / 300})` +
`:bordercolor=${itemObject.font_border}` +
Expand All @@ -34,8 +36,9 @@ const getOverlayTextString = async (path, config, typeKey, metadata) => {
// Check if we have an artist option
if (overlayConfigObject.artist && overlayConfigObject.artist.enabled) {
const itemObject = overlayConfigObject.artist;
const safeText = safeStrings.forFilter( itemObject.label + metadata.common.artist);
let itemString =
`drawtext=text='${itemObject.label}${metadata.common.artist}'` +
`drawtext=text='${safeText}'` +
`:fontfile=${fontPath}` +
`:fontsize=(w * ${itemObject.font_size / 300})` +
`:bordercolor=${itemObject.font_border}` +
Expand All @@ -49,8 +52,9 @@ const getOverlayTextString = async (path, config, typeKey, metadata) => {
// Check if we have an album option
if (overlayConfigObject.album && overlayConfigObject.album.enabled) {
const itemObject = overlayConfigObject.album;
const safeText = safeStrings.forFilter( itemObject.label + metadata.common.album);
let itemString =
`drawtext=text='${itemObject.label}${metadata.common.album}'` +
`drawtext=text='${safeText}'` +
`:fontfile=${fontPath}` +
`:fontsize=(w * ${itemObject.font_size / 300})` +
`:bordercolor=${itemObject.font_border}` +
Expand All @@ -64,8 +68,9 @@ const getOverlayTextString = async (path, config, typeKey, metadata) => {
// Check if we have an artist option
if (overlayConfigObject.song && overlayConfigObject.song.enabled) {
const itemObject = overlayConfigObject.song;
const safeText = safeStrings.forFilter( itemObject.label + metadata.common.title);
let itemString =
`drawtext=text='${itemObject.label}${metadata.common.title}'` +
`drawtext=text='${safeText}'` +
`:fontfile=${fontPath}` +
`:fontsize=(w * ${itemObject.font_size / 300})` +
`:bordercolor=${itemObject.font_border}` +
Expand Down
37 changes: 37 additions & 0 deletions src/stream/safeStrings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Function to make paths safe that are used in an input
var safeStringInput = function(string) {
var safeString = string;

// Safen the string
// \ will be \\
safeString.replace(/\\/g, '\\\\');

// : will be \:
safeString.replace(/\:/g, '\\:');

// Return safe string
return safeString;
}

// Function to make string safe that are used in a filter
var safeStringFilter = function(string) {
var safeString = string;

// Safen the string
// \ will be \\
safeString.replace(/\\/g, '\\\\');

// : will be \:
safeString.replace(/\:/g, '\\:');

// ' will be \'
safeString.replace(/\'/g, '\\\'');

// Return safe string
return safeString;
}

module.exports = {
forInput: safeStringInput,
forFilter: safeStringFilter
}
5 changes: 2 additions & 3 deletions src/stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const upath = require('upath');
const progress = require('cli-progress');

// Get our Services and helper fucntions
const safeStrings = require('./safeStrings');
const historyService = require('../history.service');
const supportedFileTypes = require('../supportedFileTypes');
const getRandomFileWithExtensionFromPath = require('./randomFile');
Expand Down Expand Up @@ -139,10 +140,8 @@ module.exports = async (path, config, outputLocation, endCallback, errorCallback
ffmpegCommand = ffmpegCommand.setFfmpegPath(config.ffmpeg_path);
}

// Create a ffmpeg safe string
doubleSlashOptimizedVideo = optimizedVideo.replace(/\\/g, '\\\\').replace(/:/g, '\\:');
// Add the video input
ffmpegCommand = ffmpegCommand.input(doubleSlashOptimizedVideo).inputOptions([
ffmpegCommand = ffmpegCommand.input(optimizedVideo).inputOptions([
// Loop the video infinitely
`-stream_loop -1`
]);
Expand Down

0 comments on commit 14114de

Please sign in to comment.