79836fe99ac7c3fa16ae49181039b7bb3df59c9d
[youtube-dl] / youtube_dl / downloader / rtmp.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5 import subprocess
6 import sys
7 import time
8
9 from .common import FileDownloader
10 from ..compat import compat_str
11 from ..utils import (
12     check_executable,
13     encodeFilename,
14     format_bytes,
15     get_exe_version,
16 )
17
18
19 def rtmpdump_version():
20     return get_exe_version(
21         'rtmpdump', ['--help'], r'(?i)RTMPDump\s*v?([0-9a-zA-Z._-]+)')
22
23
24 class RtmpFD(FileDownloader):
25     def real_download(self, filename, info_dict):
26         def run_rtmpdump(args):
27             start = time.time()
28             resume_percent = None
29             resume_downloaded_data_len = None
30             proc = subprocess.Popen(args, stderr=subprocess.PIPE)
31             cursor_in_new_line = True
32             proc_stderr_closed = False
33             while not proc_stderr_closed:
34                 # read line from stderr
35                 line = ''
36                 while True:
37                     char = proc.stderr.read(1)
38                     if not char:
39                         proc_stderr_closed = True
40                         break
41                     if char in [b'\r', b'\n']:
42                         break
43                     line += char.decode('ascii', 'replace')
44                 if not line:
45                     # proc_stderr_closed is True
46                     continue
47                 mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec \(([0-9]{1,2}\.[0-9])%\)', line)
48                 if mobj:
49                     downloaded_data_len = int(float(mobj.group(1)) * 1024)
50                     percent = float(mobj.group(2))
51                     if not resume_percent:
52                         resume_percent = percent
53                         resume_downloaded_data_len = downloaded_data_len
54                     time_now = time.time()
55                     eta = self.calc_eta(start, time_now, 100 - resume_percent, percent - resume_percent)
56                     speed = self.calc_speed(start, time_now, downloaded_data_len - resume_downloaded_data_len)
57                     data_len = None
58                     if percent > 0:
59                         data_len = int(downloaded_data_len * 100 / percent)
60                     self._hook_progress({
61                         'status': 'downloading',
62                         'downloaded_bytes': downloaded_data_len,
63                         'total_bytes_estimate': data_len,
64                         'tmpfilename': tmpfilename,
65                         'filename': filename,
66                         'eta': eta,
67                         'elapsed': time_now - start,
68                         'speed': speed,
69                     })
70                     cursor_in_new_line = False
71                 else:
72                     # no percent for live streams
73                     mobj = re.search(r'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec', line)
74                     if mobj:
75                         downloaded_data_len = int(float(mobj.group(1)) * 1024)
76                         time_now = time.time()
77                         speed = self.calc_speed(start, time_now, downloaded_data_len)
78                         self._hook_progress({
79                             'downloaded_bytes': downloaded_data_len,
80                             'tmpfilename': tmpfilename,
81                             'filename': filename,
82                             'status': 'downloading',
83                             'elapsed': time_now - start,
84                             'speed': speed,
85                         })
86                         cursor_in_new_line = False
87                     elif self.params.get('verbose', False):
88                         if not cursor_in_new_line:
89                             self.to_screen('')
90                         cursor_in_new_line = True
91                         self.to_screen('[rtmpdump] ' + line)
92             proc.wait()
93             if not cursor_in_new_line:
94                 self.to_screen('')
95             return proc.returncode
96
97         url = info_dict['url']
98         player_url = info_dict.get('player_url', None)
99         page_url = info_dict.get('page_url', None)
100         app = info_dict.get('app', None)
101         play_path = info_dict.get('play_path', None)
102         tc_url = info_dict.get('tc_url', None)
103         flash_version = info_dict.get('flash_version', None)
104         live = info_dict.get('rtmp_live', False)
105         conn = info_dict.get('rtmp_conn', None)
106         protocol = info_dict.get('rtmp_protocol', None)
107         real_time = info_dict.get('rtmp_real_time', False)
108         no_resume = info_dict.get('no_resume', False)
109         continue_dl = info_dict.get('continuedl', False)
110
111         self.report_destination(filename)
112         tmpfilename = self.temp_name(filename)
113         test = self.params.get('test', False)
114
115         # Check for rtmpdump first
116         if not check_executable('rtmpdump', ['-h']):
117             self.report_error('RTMP download detected but "rtmpdump" could not be run. Please install it.')
118             return False
119
120         # Download using rtmpdump. rtmpdump returns exit code 2 when
121         # the connection was interrumpted and resuming appears to be
122         # possible. This is part of rtmpdump's normal usage, AFAIK.
123         basic_args = ['rtmpdump', '--verbose', '-r', url, '-o', tmpfilename]
124         if player_url is not None:
125             basic_args += ['--swfVfy', player_url]
126         if page_url is not None:
127             basic_args += ['--pageUrl', page_url]
128         if app is not None:
129             basic_args += ['--app', app]
130         if play_path is not None:
131             basic_args += ['--playpath', play_path]
132         if tc_url is not None:
133             basic_args += ['--tcUrl', url]
134         if test:
135             basic_args += ['--stop', '1']
136         if flash_version is not None:
137             basic_args += ['--flashVer', flash_version]
138         if live:
139             basic_args += ['--live']
140         if isinstance(conn, list):
141             for entry in conn:
142                 basic_args += ['--conn', entry]
143         elif isinstance(conn, compat_str):
144             basic_args += ['--conn', conn]
145         if protocol is not None:
146             basic_args += ['--protocol', protocol]
147         if real_time:
148             basic_args += ['--realtime']
149
150         args = basic_args
151         if not no_resume and continue_dl and not live:
152             args += ['--resume']
153         if not live and continue_dl:
154             args += ['--skip', '1']
155
156         if sys.platform == 'win32' and sys.version_info < (3, 0):
157             # Windows subprocess module does not actually support Unicode
158             # on Python 2.x
159             # See http://stackoverflow.com/a/9951851/35070
160             subprocess_encoding = sys.getfilesystemencoding()
161             args = [a.encode(subprocess_encoding, 'ignore') for a in args]
162         else:
163             subprocess_encoding = None
164
165         self._debug_cmd(args, subprocess_encoding, exe='rtmpdump')
166
167         RD_SUCCESS = 0
168         RD_FAILED = 1
169         RD_INCOMPLETE = 2
170         RD_NO_CONNECT = 3
171
172         retval = run_rtmpdump(args)
173
174         if retval == RD_NO_CONNECT:
175             self.report_error('[rtmpdump] Could not connect to RTMP server.')
176             return False
177
178         while (retval == RD_INCOMPLETE or retval == RD_FAILED) and not test and not live:
179             prevsize = os.path.getsize(encodeFilename(tmpfilename))
180             self.to_screen('[rtmpdump] %s bytes' % prevsize)
181             time.sleep(5.0)  # This seems to be needed
182             retval = run_rtmpdump(basic_args + ['-e'] + [[], ['-k', '1']][retval == RD_FAILED])
183             cursize = os.path.getsize(encodeFilename(tmpfilename))
184             if prevsize == cursize and retval == RD_FAILED:
185                 break
186             # Some rtmp streams seem abort after ~ 99.8%. Don't complain for those
187             if prevsize == cursize and retval == RD_INCOMPLETE and cursize > 1024:
188                 self.to_screen('[rtmpdump] Could not download the whole video. This can happen for some advertisements.')
189                 retval = RD_SUCCESS
190                 break
191         if retval == RD_SUCCESS or (test and retval == RD_INCOMPLETE):
192             fsize = os.path.getsize(encodeFilename(tmpfilename))
193             self.to_screen('[rtmpdump] %s bytes' % fsize)
194             self.try_rename(tmpfilename, filename)
195             self._hook_progress({
196                 'downloaded_bytes': fsize,
197                 'total_bytes': fsize,
198                 'filename': filename,
199                 'status': 'finished',
200             })
201             return True
202         else:
203             self.to_stderr('\n')
204             self.report_error('rtmpdump exited with code %d' % retval)
205             return False