Solved Automatically create .srt file for each video in a folder

November 15, 2013 at 21:55:13
Specs: Windows 7
Hey Everyone!!

I'm trying to accomplish a task I feel is best suited for a batch script, and even though it should be short.. I am unsure of how to go about it.

I have a folder full of thousands of video files (mostly flv and mp4) that we queue in VLC media player to broadcast on our public access channel. We're trying to create intro and outro subtitles like MTV used to do.. back when they actually played music videos.

Here's what I need to accomplish..
For each video in the folder, I need a subtitle file (.srt) to be created with the same file name. The contents for each .srt should read:

  1
  00:00:00,500 --> 00:00:03,500
  *title*

  2
  (Duration minus 3.5 seconds) --> (Duration minus .5 seconds)
  *title*

I'm sure the above looks confusing.. to explain, *title* ideally needs to be the title field of the video's metadata. If that's not doable, I can manually write in the names in each srt file. That doesn't seem to be as time consuming as solving the durations of each video. The second subtitle to appear for each clip should show just prior to the end of the clip. As of now, I have no clue how I'm going to retrieve the durations of each mp4 or flv, do the calculations to subtract 3.5 and .5 seconds and format the results in .srt timecode, and end up with an filename.srt that looks like this for a 2 minute video:

  1
  00:00:00,500 --> 00:00:03,500
  *title*

  2
  00:01:56,500 --> 00:01:59,500
  *title*

...or even better, if the title field in the tag info can be retrieved:

  1
  00:00:00,500 --> 00:00:03,500
  Two Minute Video

  2
  00:01:56,500 --> 00:01:59,500
  Two Minute Video

...anyone know a good place for me to start with this?

Any help is Greatly appreciated!!

message edited by justthinkaboutit


See More: Automatically create .srt file for each video in a folder

Report •


#1
November 18, 2013 at 19:08:50
✔ Best Answer
CMD doesn't read metadata, so you'd need to find a third party program that does.

Explorer might, assuming you've got the right setup (i.e. you've installed CCCP) and you're limiting yourself to mp4's. So you just need to pick a language that can talk to Explorer. Like PowerShell.

function FormatTS([timespan]$ts) {
  "{0:00}:{1:00}:{2:00},{3:000}" -f ($ts.Days * 24 + $ts.Hours), 
                                    $ts.Minutes, 
                                    $ts.Seconds, 
                                    $ts.Milliseconds
}

$subStart = [timespan]::FromMilliseconds(3500)
$subEnd = [timespan]::FromMilliseconds(500)
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace("C:\Some\Path")
$items = $folder.Items()
$items.Filter(0x100C0, "*.mp4")
$titleNo = $lenNo = 0

foreach ($i in $items) {
  if ($titleNo -eq 0 -or $lenNo -eq 0) {
    for ($a = 0; $a -le 266; $a++) {
      if ($folder.getDetailsOf($i, $a)) {
        switch ($folder.getDetailsOf($items, $a)) {
          "Title"  { $titleNo = $a }
          "Length" { $lenNo = $a }
        }
      }
    } 
  }
  $title = "UNTITLED"; $len = 0
  $title = $folder.getDetailsOf($i, $titleNo)
  $l = $folder.getDetailsOf($i, $lenNo) -split ":"
  $len = [timespan]::FromHours($l[0]) +
         [timespan]::FromMinutes($l[1]) + 
         [timespan]::FromSeconds($l[2])
  $txt ="1`n00:00:00,500 --> 00:00:03,500`n{0}`n`n2`n{1} --> {2}`n{0}`n" `
    -f $title, (FormatTS($len - $subStart)), (FormatTS($len - $subEnd))
  [IO.File]::WriteAllText([IO.Path]::ChangeExtension($i.Path, ".srt"), $txt)
}

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question