This is the code accompanying the video "Building a YouTube timestamp generator with BuildShip".
The workflow requires a custom block to reformat the captions in the MM:SS format:
// Function to convert seconds to YouTube timestamp format (MM:SS)
export default function convertSecondsToYouTubeTimestamps({ captions }) {
// Function to convert seconds to timestamp
// Example: 128 -> 2:08
const secondsToTimestamp = num => `${Math.floor(num / 3600) ? `${Math.floor(num / 3600)}:`.padStart(3, '0') : ''}${Math.floor((num % 3600) / 60).toString().padStart(2, '0')}:${(num % 60).toString().padStart(2, '0')}`;
// Reformat captions using the new format.
const formattedCaptions = captions.map(subtitle => ({
text: subtitle.text, // Retaining the subtitle text
timestamp: secondsToTimestamp(parseFloat(subtitle.start)) // Converting start time to YouTube timestamp
}));
// Shorten an array by removing items until its JSON length fits within 5000 characters
const trimArrayToJSON = array => JSON.stringify(array).length <= 5000 ? array : trimArrayToJSON(array.filter((_, i) => (i + 1) % 3 !== 0));
// Shorten the captions if necessary
return trimArrayToJSON(formattedCaptions);
}
The Chat block also requires this prompt:
Give me section markers with timestamps to add to my YouTube video description. Use the exact format:
0:00 First section
0:42 Second section
Use the JSON provided to determine the exact timestamps for the titles. Use only timestamps in the JSON.
Make the titles general and cover large parts of the video. Don't use every timestamp. You only need to make 5-10 timestamps total.
Top comments (0)