首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:从srt (字幕)文件中提取时间

R:从srt (字幕)文件中提取时间
EN

Stack Overflow用户
提问于 2016-04-10 16:02:09
回答 1查看 1.1K关注 0票数 2

我需要计算每一行字幕的语音速率,。srt (字幕)文件的内容如下所示:

代码语言:javascript
复制
1
00:00:19,000 --> 00:00:21,989
I'm Annita McVeigh and welcome to Election Today where we'll bring you

2
00:00:22,000 --> 00:00:23,989
the latest from the campaign trail, plus debate and analysis.

3
00:00:24,000 --> 00:00:28,989
The Liberal Democrats promise to protect the pay of millions

例如,花了4秒989毫秒才说出“自由民主党承诺保护数百万美元”这10个字。这10个字的平均语速为每字498.9毫秒。

如何读取srt文件,以便可以将startTime、endTime、textString和wordCount作为列,并将副标题行作为下面的行使用?

代码语言:javascript
复制
startTime<-c("00:00:19,000", "00:00:22,000", "00:00:24,000")

endTime<-c("00:00:21,989", "00:00:23,989", "00:00:28,989")

textString<-c("I'm Annita McVeigh and welcome to Election Today where we'll bring you", "the latest from the campaign trail, plus debate and analysis.", "The Liberal Democrats promise to protect the pay of millions")

wordCount<-c(12,10,10)

rate.df<-data.frame(startTime, endTime, textString, wordCount)

当时间以小时:分钟:第二,毫秒的形式出现时,我如何从R中的startTime中减去endTime?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-10 16:36:00

这里有一个可能的解决方案(代码非常清楚):

代码语言:javascript
复制
text="

1
00:00:19,000 --> 00:00:21,989
I'm Annita McVeigh and welcome to Election Today where we'll bring you

2
00:00:22,000 --> 00:00:23,989
the latest from the campaign trail, 
plus debate 
and analysis.



3
00:00:24,000 --> 00:00:28,989
The Liberal Democrats promise to protect 
the pay of millions"

con<-textConnection(text)
lines <- readLines(con) 

# the previous lines of code are just to replicate you case, and
# they should be replaced by the following single line in the real case
# lines <- readLines(srtFileName)

listOfEntries <- 
lapply(split(1:length(lines),cumsum(grepl("^\\s*$",lines))),function(blockIdx){
    block <- lines[blockIdx]
    block <- block[!grepl("^\\s*$",block)]
    if(length(block) == 0){
      return(NULL)
    }
    if(length(block) < 3){
      warning("a block not respecting srt standards has been found")
    }
    return(data.frame(id=block[1], 
                      times=block[2], 
                      textString=paste0(block[3:length(block)],collapse="\n"),
                      stringsAsFactors = FALSE))
  })
m <- do.call(rbind,listOfEntries)


# split start and end times
tmp <- do.call(rbind,strsplit(m[,'times'],' --> '))
m$startTime <- tmp[,1]
m$endTime <- tmp[,2]

# parse times
tmp <- do.call(rbind,lapply(strsplit(m$startTime,':|,'),as.numeric))
m$fromSeconds  <- tmp %*% c(60*60,60,1,1/1000)

tmp <- do.call(rbind,lapply(strsplit(m$endTime,':|,'),as.numeric))
m$toSeconds  <- tmp %*% c(60*60,60,1,1/1000)

# compute time difference in seconds
m$timeDiffInSecs <- m$toSeconds - m$fromSeconds

# word count
m$wordCount <- vapply(gregexpr("\\W+",m$textString),length,0) + 1

# or if you consider "I'm" a single word you can remove the occurrencies of ', e.g. :
#m$wordCount <- vapply(gregexpr("\\W+",gsub("'","",m$textString)),length,0) + 1

m$millisecsPerWord <- m$timeDiffInSecs * 1000 / m$wordCount 

结果:

代码语言:javascript
复制
> m
  id                         times                                                             textString
2  1 00:00:19,000 --> 00:00:21,989 I'm Annita McVeigh and welcome to Election Today where we'll bring you
3  2 00:00:22,000 --> 00:00:23,989      the latest from the campaign trail, \nplus debate \nand analysis.
6  3 00:00:24,000 --> 00:00:28,989         The Liberal Democrats promise to protect \nthe pay of millions
     startTime      endTime fromSeconds toSeconds timeDiffInSecs wordCount millisecsPerWord
2 00:00:19,000 00:00:21,989          19    21.989          2.989        14         213.5000
3 00:00:22,000 00:00:23,989          22    23.989          1.989        11         180.8182
6 00:00:24,000 00:00:28,989          24    28.989          4.989        10         498.9000
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36532051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档