]> git.frykholm.com Git - svtplaydump.git/blame - svtplaydump.py
oops
[svtplaydump.git] / svtplaydump.py
CommitLineData
ca2553c7 1#!/usr/bin/env python
56181f0a 2# -*- coding: utf-8 -*-
ca2553c7
MF
3#
4# (C) Copyright 2010 Mikael Frykholm <mikael@frykholm.com>
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>
18#
19# Changelog:
56181f0a 20# 0.3 added apple streaming playlist parsing and decryption
ca2553c7
MF
21# 0.2 added python 2.4 urlparse compatibility
22# 0.1 initial release
23
24from BeautifulSoup import BeautifulSoup
25from subprocess import *
89a00fa0
MF
26import re
27import json
56181f0a
MF
28from Crypto.Cipher import AES
29import struct
72beea17
MF
30import argparse
31import feedparser
ca2553c7
MF
32try:
33 import urlparse
34except ImportError:
35 pass
36import urllib2
37try:
38 import urllib2.urlparse as urlparse
39except ImportError:
40 pass
41import sys
42
72beea17
MF
43def main(url):
44 page = urllib2.urlopen(url).read()
a7502370
MF
45 soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
46 videoid = re.findall("svt_article_id=(.*)[&]*",page)[0]
47 flashvars = json.loads(urllib2.urlopen("http://www.svt.se/wd?widgetId=248134&sectionId=1024&articleId=%s&position=0&format=json&type=embed&contextSectionId=1024"%videoid).read())
56181f0a 48 try:
a7502370 49 title = soup.find('meta',{'property':'og:title'}).attrMap['content']
ca2553c7
MF
50 except:
51 title = "unnamed"
52 if 'dynamicStreams' in flashvars:
53 url = flashvars['dynamicStreams'][0].split('url:')[1].split('.mp4,')[0] +'.mp4'
54 filename = title+".mp4"
55 print Popen(["rtmpdump",u"-o"+filename,"-r", url], stdout=PIPE).communicate()[0]
56 if 'pathflv' in flashvars:
57 rtmp = flashvars['pathflv'][0]
58 filename = title+".flv"
59 print Popen(["mplayer","-dumpstream","-dumpfile",filename, rtmp], stdout=PIPE).communicate()[0]
89a00fa0 60 if 'video' in flashvars:
56181f0a
MF
61 for reference in flashvars['video']['videoReferences']:
62 if reference['url'].endswith("m3u8"):
63 url=reference['url']
64 download_from_playlist(url, title+'.ts')
ca2553c7
MF
65 else:
66 print "Could not find any streams"
67 return
72beea17 68 return title+'.ts'
56181f0a
MF
69def download_from_playlist(url, title):
70 playlist = parse_playlist(urllib2.urlopen(url).read())
71 videourl = sorted(playlist, key=lambda k: int(k['BANDWIDTH']))[-1]['url']
72 segments, metadata = parse_segment_playlist(urllib2.urlopen(videourl).read())
73 if "EXT-X-KEY" in metadata:
74 key = urllib2.urlopen(metadata["EXT-X-KEY"]['URI'].strip('"')).read()
75 decrypt=True
76 else:
77 decrypt=False
78 with open("%s"%title,"w") as ofile:
79 segment=0
72beea17 80 size = 0
56181f0a 81 for url in segments:
56181f0a 82 ufile = urllib2.urlopen(url)
72beea17
MF
83 print "\r{} MB".format(size/1024/1024),
84 sys.stdout.flush()
56181f0a
MF
85 if decrypt:
86 iv=struct.pack("IIII",segment,0,0,0)
87 decryptor = AES.new(key, AES.MODE_CBC, iv)
88 while(True):
89 buf = ufile.read(1024)
90 if buf:
91 if decrypt:
92 buf = decryptor.decrypt(buf)
93 ofile.write(buf)
72beea17 94 size += len(buf)
56181f0a
MF
95 else:
96 ufile.close()
97 break
98 segment += 1
99
100def parse_playlist(playlist):
101 assert playlist.startswith("#EXTM3U")
102 playlist = playlist.splitlines()[1:]
103 items=[]
104 for (metadata_string,url) in zip(playlist[0::2], playlist[1::2]):
105 md = dict()
106 assert 'EXT-X-STREAM-INF' in metadata_string.split(':')[0]
107 for item in metadata_string.split(':')[1].split(','):
108 if '=' in item:
109 md.update([item.split('='),])
110 md['url']=url
111 items.append(md)
112 return items
113
114def parse_segment_playlist(playlist):
115 assert playlist.startswith("#EXTM3U")
116 PATTERN = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
117 segments = []
118 next_is_url=False
119 metadata = {}
120 for row in playlist.splitlines():
121 if next_is_url:
122 segments.append(row)
123 next_is_url=False
124 continue
125 if 'EXTINF' in row:
126 next_is_url=True
127 if "EXT-X-KEY" in row:
128 row = row.split(':',1)[1] #skip first part
129 parts = PATTERN.split(row)[1:-1] #do magic re split and keep quoting
130 metadata["EXT-X-KEY"] = dict([part.split('=',1) for part in parts if '=' in part]) #throw away the commas and make dict of the pairs
131 return(segments, metadata)
132
ca2553c7 133if __name__ == "__main__":
72beea17
MF
134 parser = argparse.ArgumentParser()
135 parser.add_argument("-r", "--rss", help="Download all files in rss",
136 action="store_true")
137 parser.add_argument("url")
138 args = parser.parse_args()
139 if args.rss:
140 d = feedparser.parse(args.url)
141 for e in d.entries:
142 print("Downloading: %s"%e.title)
143 filename = main(e.link)
144 print Popen(["avconv","-i",filename,"-vcodec","copy","-acodec","copy", filename+'.mkv'], stdout=PIPE).communicate()[0]
145 #print(e.description)
146 else:
7c760a95 147 filename = main(args.url)
72beea17 148 print("Saved to {}".format(filename))