From 14114dea5910c625aed66fe3187008b04f0cb2fe Mon Sep 17 00:00:00 2001 From: Andreas Heimann Date: Wed, 10 Oct 2018 08:15:13 +0200 Subject: [PATCH] Make Strings Safe Again! (#85) First version of safeStrings so ffmpeg does not get confused by our inputs! This references #68 --- src/generate/template/config.json | 6 ++--- src/stream/overlayText.js | 13 +++++++---- src/stream/safeStrings.js | 37 +++++++++++++++++++++++++++++++ src/stream/stream.js | 5 ++--- 4 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/stream/safeStrings.js diff --git a/src/generate/template/config.json b/src/generate/template/config.json index 125f568..d82b384 100644 --- a/src/generate/template/config.json +++ b/src/generate/template/config.json @@ -41,7 +41,7 @@ }, "artist": { "enabled": true, - "label": "Artist\\: ", + "label": "Artist: ", "font_color": "#FFFFFF", "font_border": "#000000", "font_size": "10", @@ -50,7 +50,7 @@ }, "album": { "enabled": false, - "label": "Album\\: ", + "label": "Album: ", "font_color": "#FFFFFF", "font_border": "#000000", "font_size": "10", @@ -59,7 +59,7 @@ }, "song": { "enabled": true, - "label": "Song\\: ", + "label": "Song: ", "font_color": "#FFFFFF", "font_border": "#000000", "font_size": "10", diff --git a/src/stream/overlayText.js b/src/stream/overlayText.js index 5f99790..5286ca1 100644 --- a/src/stream/overlayText.js +++ b/src/stream/overlayText.js @@ -1,4 +1,5 @@ // Overlay text for the stream +const safeStrings = require('./safeStrings'); const getOverlayTextString = async (path, config, typeKey, metadata) => { // Create our overlay @@ -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}` + @@ -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}` + @@ -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}` + @@ -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}` + diff --git a/src/stream/safeStrings.js b/src/stream/safeStrings.js new file mode 100644 index 0000000..47c6089 --- /dev/null +++ b/src/stream/safeStrings.js @@ -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 +} diff --git a/src/stream/stream.js b/src/stream/stream.js index 111deed..a902374 100644 --- a/src/stream/stream.js +++ b/src/stream/stream.js @@ -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'); @@ -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` ]);