Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tweaking Movie Subtitles with R

DZone's Guide to

Tweaking Movie Subtitles with R

· Big Data Zone
Free Resource

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity. 

I use R to fix subtitles that are not in sync with my movies. For the example below the subs were showing too early - so I added some time to each sequence in the srt file. For simplicity I used exactly 1 second in the below example.

You'll see that I use my function dl_from_dropbox(), on which I wrote a post previously, to get the example file!

setwd(tempdir())
options(digits = 12)
options(digits.secs = 3)
 
### get subtitle example file:
dl_from_dropbox <- function(x, key) {
                        require(RCurl)
                        bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x),
                                            ssl.verifypeer = FALSE)
                        con <- file(x, open = "wb")
                        writeBin(bin, con)
                        close(con)
                        message(noquote(paste(x, "read into", getwd())))                       
                        }
 
dl_from_dropbox("Game_of_Thrones_S3_E1_engl.srt", "wojo9k8v8cezs9g")
shell.exec("Game_of_Thrones_S3_E1_engl.srt") #I use the MS text-editor to view srt files
 
# https://www.dropbox.com/s/wojo9k8v8cezs9g/Game_of_Thrones_S3_E1_engl.srt
###
 
### tweak the file by changing the time - i.e., I add 1 sec to all sequences here:
t <- readLines("Game_of_Thrones_S3_E1_engl.srt")
tt <- unlist(strsplit(t, " --> ")) #split time start/end
 
x <- grep("\\d{2}:\\d{2}:\\d{2},\\d{3}", t) #ids of time data in t
y <- sort(c(x, x+1)) #ids of time data in tt
 
ttt <- gsub(",",".", tt[y]) #replace decimal comma
 
(a <- strptime(ttt, format="%H:%M:%OS", tz="GMT")) #convert to date/time
(b <- as.numeric(a)) #convert to number
 
c <- 1 #add 1 sec
 
(d <- as.POSIXct(as.numeric(b+c+1e-6), origin="1970-01-01", tz="GMT")) #convert back
(e <- format(d, "%H:%M:%OS")) #re-format
(f <- gsub("\\.", ",", e)) #replace decimal point
 
id_t1 <- seq(1, length(y), 2)
id_t2 <- seq(0, length(y), 2)
 
(g <- paste0(f[id_t1], " --> ", f[id_t2])) #bring into original form
 
t_new <- t 
t_new[x] <- g #insert new sequences into original data
print(t_new)
 
### save to new file:
write(t_new, "Game_of_Thrones_S3_E1_engl_new.srt")
shell.exec("Game_of_Thrones_S3_E1_engl_new.srt") #I use the MS text-editor to view srt files

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version.

Topics:

Published at DZone with permission of Kay Cichini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}