I am making a simple script to add some text to a 4 seconds video, it all works fine, but sometimes it randomly doesn't add some of the text. You can find here the relevant parts of my code:
const video = ffmpeg('path/to/video.mp4')
let index = 0
let left = true
const filters = [{
filter: 'drawtext',
options: {
//fontfile:'font.ttf',
text: title,
fontsize: 30,
fontcolor: 'white',
x: '(main_w/2-text_w/2)',
y: 130,
shadowcolor: 'black',
shadowx: 2,
shadowy: 2
}
}]
for (let thought of thoughts) {
if (thought.length == 0) {
continue
}
thought = wrap(thought, {width: 35})
const strings = thought.split("\n")
let line = 0
for (const string of strings
.filter(string => string.length > 0)
.map(string => string.trim())
) {
let yoffset = 130+(130*(index+1))+(line*20)
if (yoffset < 0) {
yoffset = 0
}
console.log(string, yoffset)
filters.push({
filter: 'drawtext',
options: {
//fontfile:'font.ttf',
text: string,
fontsize: 18,
fontcolor: 'white',
x: `(main_w${left ? "*0.3" : "*0.7"}-text_w/2)`,
y: yoffset,
shadowcolor: 'black',
shadowx: 2,
shadowy: 2
}
})
line++;
}
index++;
left = !left
}
video.videoFilters(filters)
video.noAudio()
video.save('path/to/output.mp4');
The wrap function comes from the package word-wrap (const wrap = require('word-wrap');
)
Thoughts is a list of strings that aren't too long (with the wrap function they end up being like 2-4 lines).
This is inside an async function.
For some reason only a few lines appear on the output video.
Sometimes, when it doesn't do that, it also throws an error saying that one of the inputs is invalid (while processing filters).
The wrap function seems to work, and also the yoffset
, I have printed them.
If someone has an idea why, please help me solve this.
I tried chasing the text in thoughts, and for example, this works with no problems (shows the title, and the texts right, left, right, left, ...).
const thoughts = ["Nothing is on fire, fire is on things","Nothing is on fire, fire is on things","Nothing is on fire, fire is on things","Nothing is on fire, fire is on things","Nothing is on fire, fire is on things"]
The issue was solved. For anyone coming here in the future, I had a few problems. First of all, the ' symbol is used by fluent-ffmpeg when creating the ffmpeg command, meaning that when adding the sentences that had the ' symbol it would close the "text" area.
I then added something like this:
thought = thought.replace(/'/g, "//'")
It worked, but the parts with ' simply didn't appear. I haven't found a solution to make a text with ' appear yet, but I just changed the symbol since I don't believe it's too important for me at the moment.