R 語言套件 - SRTtools

簡介

CRAN\_Status\_Badge CRAN\_Status\_Badge

R 語言套件 SRTtools 可以調整副檔名為srt的影片字幕檔的時間軸、樣式和位置。


套件安裝

install.packages("SRTtools")
library(SRTtools)

範例

讀取srt檔案

srt_path <- system.file("extdata", "movie.srt", package="SRTtools")
srt <- srt.read(srt_path)
> srt
   [1] "1"                                                   "00:00:25,525 --> 00:00:29,904"                      
   [3] "<i>This is the Asgardian"                            "refugee vessel </i>Statesman<i>.</i>"               
   [5] ""                                                    "2"                                                  
   [7] "00:00:32,657 --> 00:00:36,119"                       "<i>We are under assault."                           

讓字幕時間軸延遲0.5秒

srt_later <- srt.shift(srt, time_shifted = 0.5)
> srt_later
   [1] "1"                                                   "00:00:26,025 --> 00:00:30,404"                      
   [3] "<i>This is the Asgardian"                            "refugee vessel </i>Statesman<i>.</i>"               
   [5] ""                                                    "2"                                                  
   [7] "00:00:33,157 --> 00:00:36,618"                       "<i>We are under assault."                           

提早字幕時間軸0.5秒

srt_earlier <- srt.shift(srt, time_shifted = -0.5)
> srt_earlier
   [1] "1"                                                   "00:00:25,025 --> 00:00:29,404"                      
   [3] "<i>This is the Asgardian"                            "refugee vessel </i>Statesman<i>.</i>"               
   [5] ""                                                    "2"                                                  
   [7] "00:00:32,157 --> 00:00:35,618"                       "<i>We are under assault."                           

另存為’movie_new.srt’

srt.write(srt_earlier, filename = "movie_new.srt")

讀取字幕檔文字內容

> srt.content(srt)
   [1] "<i>This is the Asgardian"                            "refugee vessel </i>Statesman<i>.</i>"               
   [3] "<i>We are under assault."                            "I repeat, we are under assault.</i>"                
   [5] "<i>The engines are dead, life support failing.</i>"  "<i>Requesting aid from any vessel within range.</i>"
   [7] "<i>We are 22 jump points out of Asgard.</i>"         "<i>Our crew is made up of Asgardian families."  

調整字幕內含有 ‘Wow’ 的字幕顏色和位置

> srt.style(srt, line = srt.search(srt,"Wow"), pos = 'middle-center', style = c('b','i'), col = 'red')
 [418] "791"                                                                              
 [419] "01:00:28,545 --> 01:00:29,379"                                                    
 [420] "the stone."                                                                       
 [421] ""                                                                                 
 [422] "792"                                                                              
 [423] "01:00:41,975 --> 01:00:44,394"                                                    
 [424] "{\\an5}<b><i><font color='red'>Wow, you're a seriously loyal piece</b></i></font>"
 [425] "{\\an5}<b><i><font color='red'>of outerwear, aren't you?</b></i></font>"          
 [426] ""                                                                                 
 [427] "793"                                                                              
 [428] "01:00:44,561 --> 01:00:46,563"                                                    
 [429] "Yeah, speaking of loyalty..."    

插入新字幕至第一句字幕位置

> srt.insert(srt, index = 1, time = "00:00:00,000 --> 00:00:30,000", text = "Added by SRTtools!")
   [1] "1"                                                             "00:00:00,000 --> 00:00:30,000"                                
   [3] "Added by SRTtools!"                                             ""                                                             
   [5] "2"                                                             "00:00:26,360 --> 00:00:28,192"                                
   [7] "This is the Asgardian"                                         ""                                                             
   [9] "3"                                                             "00:00:28,278 --> 00:00:30,736"                                
   [11] "refugee vessel Statesman."                                     ""                                                             

Remove the second dialog

> srt
   [1] "1"                               "00:00:26,360 --> 00:00:28,192"   "This is the Asgardian"           ""                               
   [5] "2"                               "00:00:28,278 --> 00:00:30,736"   "refugee vessel Statesman."       ""                               
   [9] "3"                               "00:00:33,492 --> 00:00:34,778"   "We are under assault."           ""                               
   [13] "4"                               "00:00:34,868 --> 00:00:36,951"   "I repeat, we are under assault."
> srt.remove(srt, index = 2)
   [1] "1"                               "00:00:26,360 --> 00:00:28,192"   "This is the Asgardian"           ""                     
   [5] "2"                               "00:00:33,492 --> 00:00:34,778"   "We are under assault."           ""            
   [9] "3"                               "00:00:34,868 --> 00:00:36,951"   "I repeat, we are under assault." ""