Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Last active May 11, 2020 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdwab/5a98038af9fd9bb0a1d943b004b73cab to your computer and use it in GitHub Desktop.
Save mrdwab/5a98038af9fd9bb0a1d943b004b73cab to your computer and use it in GitHub Desktop.
R script to fix manually edited srt files to upload to YouTube. Specifically, it converts the timestamps from SRT or SBV files to make sure the end and start times don't overlap.
#!/usr/local/bin/r
suppressMessages(library(docopt))
suppressMessages(library(glue))
options(useFancyQuotes = FALSE)
doc <- "Usage: subfix.r [FILE] [-h]
-h --help show this help text"
opt <- docopt(doc)
# Step 1: Fix the SRT
srt <- as.character(opt$FILE)
file.copy(srt, glue(srt, ".bak"), overwrite = TRUE)
new.file <- glue(substr(srt, 1, nchar(srt)-4), "_fixed.srt")
cmd <- glue("~/Apps/ffmpeg/ffmpeg -fix_sub_duration -i {input} {output}",
input = srt, output = new.file)
system(cmd)
# Step 2: Fix the times
x <- readLines(new.file)
## Which lines need to be fixed
### Start time fix
inds <- grep("^[0-9]+$", x)+1
y <- x[inds]
### End time fix
z <- which(substr(y, 10, 12) == "999")
substring(y, 10, 12) <- sprintf("%03d", as.numeric(substr(y, 10, 12))+1)
if (length(z) > 0) {
substr(y[z-1], 27, 29) <- "998"
substr(y[z], 10, 12) <- "999"
}
x[inds] <- y
writeLines(x, new.file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment