Created
March 15, 2015 11:05
-
-
Save sheharyarn/137640fbd87b05c22f41 to your computer and use it in GitHub Desktop.
Download Subtitles/Captions for MediaStorm Videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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