Skip to content

Instantly share code, notes, and snippets.

@derkzomer
Created December 22, 2023 08:10
Show Gist options
  • Save derkzomer/ce79b6f4995d7fcd3e9fe51e360a7515 to your computer and use it in GitHub Desktop.
Save derkzomer/ce79b6f4995d7fcd3e9fe51e360a7515 to your computer and use it in GitHub Desktop.
const base = {
"timeline": {
"cache": false,
"background": "#000000",
"fonts": [{
"src": "https://shotstack-assets.s3.amazonaws.com/fonts/NotoSans-Regular.ttf"
}],
"tracks": [
{
"clips": []
},
{
"clips": [{
"asset": {
"type": "",
"src": "",
"volume": 1
},
"start": 0,
"length": 0
}]
}
]
},
"output": {
"format": "mp4",
"size": {
"width": 1280,
"height": 720
},
"destinations": [{
"provider": "shotstack",
"exclude": true
}]
}
};
module.exports.generateTemplate = async (videoWidth, videoHeight, url, duration, captions) => {
let clip;
let fontSize;
if (videoHeight >= videoWidth) {
if (videoHeight <= 720) fontSize = 24;
if (videoHeight <= 1080) fontSize = 36;
if (videoHeight > 1080) fontSize = 48;
} else {
if (videoHeight <= 720) fontSize = 28;
if (videoHeight <= 1080) fontSize = 40;
if (videoHeight > 1080) fontSize = 52;
}
const clips = [];
for (let i = 0; i < captions.length; i += 1) {
const wordCount = captions[i].text.split(' ').length;
let heightScale;
if (wordCount <= 20) {
heightScale = 1;
} else if (wordCount > 20 && wordCount <= 32) {
heightScale = 1.25;
} else if (wordCount > 32 && wordCount <= 45) {
heightScale = 1.5;
} else if (wordCount > 45 && wordCount <= 60) {
heightScale = 1.75;
} else if (wordCount > 60) {
heightScale = 2;
}
clip.asset.html = `<span>${captions[i].text}</span>`;
clip.start = captions[i].start;
clip.length = captions[i].length - (1 / 25);
clips.push(clip);
}
base.timeline.tracks[0].clips.push(...clips);
base.timeline.tracks[1].clips[0].asset.type = 'video';
base.timeline.tracks[1].clips[0].asset.src = videoUrl;
base.timeline.tracks[1].clips[0].length = duration;
if (videoHeight <= 2180) {
base.output.size.width = videoWidth;
base.output.size.height = videoHeight;
} else {
base.output.size.width = 3840;
base.output.size.height = 2180;
}
return base;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment