Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Created March 15, 2015 11:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheharyarn/137640fbd87b05c22f41 to your computer and use it in GitHub Desktop.
Save sheharyarn/137640fbd87b05c22f41 to your computer and use it in GitHub Desktop.
Download Subtitles/Captions for MediaStorm Videos
#!/bin/env ruby
#### This Ruby Script extracts Captions as .srt files from
#### MediaStorm Videos (http://mediastorm.com)
#### Download The Video, and if you want to download captions,
#### lookup the 'Network' tab in Inspector to see the video id
#### It'll be a GET request to two xml documents on the mediastorm
#### server, namely; /timecodes/<id> and /phrases/<id>
#### Sheharyar Naseer © 2015
## Requires
require 'open-uri'
require 'nokogiri'
## Methods
def nice_time(t); t.attributes.map {|k,v| {k => v.value} } .reduce(&:merge); end
def nice_phrase(p); p.children.first.content; end
def compatible(p,t); t.count == p.count; end
def srt_time(t); "#{t}0".gsub ';', ','; end
def generate_srt(id, phrases, times)
if compatible(phrases, times)
open("#{id}.srt", 'w') do |f|
phrases.count.times do |i|
phrase = phrases[i]
time = times[i]
f.puts "#{i+1}",
"#{srt_time(time['tIn'])} --> #{srt_time(time['tOut'])}",
"#{phrase}",
""
end
end
else
puts "Not Compatible, can't generate srt"
end
end
## Video ID
id = 468 #469 #512
## Open Files
t = Nokogiri::XML open("https://player.mediastorm.com/timecodes/#{id}.xml")
p = Nokogiri::XML open("https://player.mediastorm.com/phrases/#{id}.xml")
## Get children
ts = t.xpath('//time')
ps = p.xpath('//phrase')
## Make sure they are compatible
if compatible(ts, ps)
puts "COMPATIBLE: EQUAL VALUES"
else
puts "NOT COMPATIBLE: NOT EQUAL"
end
## Convert to simple arrays/hashes
nts = ts.map { |t| nice_time t }
nps = ps.map { |p| nice_phrase p }
## Generate the srt file
generate_srt(id, nps, nts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment