1

How can you import a SubRip file?

I tried plain Import[] but it did not work.

Unfortunately I can't attach a SRT example, but you can find SubRip files on edX.

Here is a description: https://fileinfo.com/extension/srt

b3m2a1 note:

Here's a sample, coming from here:

1 
00:02:17,440 --> 00:02:20,375
Senator, we're making our final approach into Coruscant.

2 
00:02:20,476 --> 00:02:22,501
Very good, Lieutenant.
b3m2a1
  • 46,870
  • 3
  • 92
  • 239
james
  • 3,043
  • 13
  • 29

1 Answers1

7

Absent any data, from that image, here's what I'd try:

srtImport[f_String?FileExistsQ] :=
 srtImport[Import[f, "Text"]]

srtImport[s_String] :=
 StringCases[s,
  Shortest[
    n : NumberString ~~ Except["\n", WhitespaceCharacter] ... ~~ "\n" ~~

     ts : Except[WhitespaceCharacter] .. ~~ " --> " ~~ 
     ts2 : Except[WhitespaceCharacter] .. ~~ Except["\n", WhitespaceCharacter] ... ~~ 
     "\n" ~~ desc___ ~~ ("\n\n"|EndOfString)
    ] :>
   n -> <|
     "Segment" -> n,
     "StartTime" -> TimeObject@StringReplace[ts, "," -> "."],
     "EndTime" -> TimeObject@StringReplace[ts2, "," -> "."],
     "Description" -> desc
     |>
  ]

Here is that applied to the sample:

srtSamp = "1
  00:02:17,440 --> 00:02:20,375
  Senator, we're making our final approach into Coruscant.

  2
  00:02:20,476 --> 00:02:22,501
  Very good, Lieutenant.";

srtImport[srtSamp] // Association // Dataset

SRT example

b3m2a1
  • 46,870
  • 3
  • 92
  • 239