Created
October 13, 2023 06:36
-
-
Save KentaKudo/aa432e81bc6e2164844137daf9af9370 to your computer and use it in GitHub Desktop.
A Go script to generate caption file (srt format) from text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"time" | |
) | |
const format = "15:04:05,000" | |
// configure this to change duration of each caption | |
const secondsToAdd = 3 * time.Second | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
scanner.Split(bufio.ScanLines) | |
var b strings.Builder | |
idx := 1 | |
now, err := time.Parse(format, "00:00:00,000") | |
if err != nil { | |
log.Fatalf("time.Parse: %v", err) | |
} | |
for scanner.Scan() { | |
line := scanner.Text() | |
if line == "" { | |
continue | |
} | |
b.WriteString(fmt.Sprintf("%d\n", idx)) | |
b.WriteString(fmt.Sprintf("%v --> %v\n", now.Format(format), now.Add(3*time.Second).Format(format))) | |
b.WriteString(line) | |
b.WriteString(fmt.Sprintln()) | |
b.WriteString(fmt.Sprintln()) | |
idx++ | |
now = now.Add(secondsToAdd) | |
} | |
fmt.Fprintln(os.Stdout, b.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment