-
-
Save mic159/b71a8efdad7f67916ff0 to your computer and use it in GitHub Desktop.
python-librtmp problem
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
import sys | |
import librtmp | |
import requests | |
from lxml import etree | |
PAGE_URL = 'http://www.crunchyroll.com/parasyte-the-maxim-/episode-1-metamorphosis-662583' | |
XML_URL = 'http://www.crunchyroll.com/xml/?req=RpcApiVideoPlayer_GetStandardConfig&media_id=662583' | |
response = requests.post(XML_URL, data={'current_page': PAGE_URL}) | |
document = etree.XML(response.text.encode('utf-8')) | |
file = document.xpath('//stream_info/file/text()')[0] | |
host = document.xpath('//stream_info/host/text()')[0] | |
def download_video(): | |
print 'rtmpdump -r "{host}" -y "{file}" -o video2.flv'.format(file=file, host=host) | |
connection = librtmp.RTMP(host, playpath=file, timeout=60) | |
connection.connect() | |
stream = connection.create_stream() | |
while True: | |
data = stream.read(1024) | |
if not data: | |
break | |
yield data | |
def sizeof_fmt(num, suffix='B'): | |
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: | |
if abs(num) < 1024.0: | |
return "%3.1f%s%s" % (num, unit, suffix) | |
num /= 1024.0 | |
return "%.1f%s%s" % (num, 'Yi', suffix) | |
with open('video.flv', 'wb') as fle: | |
size = 0 | |
for block in download_video(): | |
size += len(block) | |
fle.write(block) | |
sys.stdout.write('{} \r'.format(sizeof_fmt(size))) | |
print '' | |
print 'Complete!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment