Arpith Siromoney 💬

Go: floats

My Go script to create a video from slides and audio now uses timestamps (in milliseconds) instead of slide durations.

var imgDuration float64
if (i == len(lines)-1) || (lines[i+1] == "") {
       imgDuration = float64(timestamp) / 1000
} else {
       nextLineSplit := strings.Split(lines[i+1], " ")
       nextTimestamp, err := strconv.Atoi(nextLineSplit[0])
       if err != nil {
             log.Fatal(err)
       }
       imgDuration = float64(nextTimestamp - timestamp) / 1000
}
imgDurationString := strconv.FormatFloat(imgDuration, 'f', 3, 64)

If we are on the last slide the assumption is that the slide can’t be on for more than half the show (we can trim to the size of the audio using ffmpeg). Otherwise, we look at the timestamp for the next slide and convert the difference into seconds. Note that there is no float type. Once again strconv comes in handy, (this time to get a string with the three digits past the decimal point - 64 is to indicate we are dealing with float64’s). Also note that we declared imgDuration outside the if statement because we need to use it outside the block.