6 from .common
import FileDownloader
15 def rtmpdump_version():
16 return get_exe_version(
17 'rtmpdump', ['--help'], r
'(?i)RTMPDump\s*v?([0-9a-zA-Z._-]+)')
20 class RtmpFD(FileDownloader
):
21 def real_download(self
, filename
, info_dict
):
22 def run_rtmpdump(args
):
25 resume_downloaded_data_len
= None
26 proc
= Popen(args
, stderr
=subprocess
.PIPE
)
27 cursor_in_new_line
= True
28 proc_stderr_closed
= False
30 while not proc_stderr_closed
:
31 # read line from stderr
34 char
= proc
.stderr
.read(1)
36 proc_stderr_closed
= True
38 if char
in [b
'\r', b
'\n']:
40 line
+= char
.decode('ascii', 'replace')
42 # proc_stderr_closed is True
44 mobj
= re
.search(r
'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec \(([0-9]{1,2}\.[0-9])%\)', line
)
46 downloaded_data_len
= int(float(mobj
.group(1)) * 1024)
47 percent
= float(mobj
.group(2))
48 if not resume_percent
:
49 resume_percent
= percent
50 resume_downloaded_data_len
= downloaded_data_len
51 time_now
= time
.time()
52 eta
= self
.calc_eta(start
, time_now
, 100 - resume_percent
, percent
- resume_percent
)
53 speed
= self
.calc_speed(start
, time_now
, downloaded_data_len
- resume_downloaded_data_len
)
56 data_len
= int(downloaded_data_len
* 100 / percent
)
58 'status': 'downloading',
59 'downloaded_bytes': downloaded_data_len
,
60 'total_bytes_estimate': data_len
,
61 'tmpfilename': tmpfilename
,
64 'elapsed': time_now
- start
,
67 cursor_in_new_line
= False
69 # no percent for live streams
70 mobj
= re
.search(r
'([0-9]+\.[0-9]{3}) kB / [0-9]+\.[0-9]{2} sec', line
)
72 downloaded_data_len
= int(float(mobj
.group(1)) * 1024)
73 time_now
= time
.time()
74 speed
= self
.calc_speed(start
, time_now
, downloaded_data_len
)
76 'downloaded_bytes': downloaded_data_len
,
77 'tmpfilename': tmpfilename
,
79 'status': 'downloading',
80 'elapsed': time_now
- start
,
83 cursor_in_new_line
= False
84 elif self
.params
.get('verbose', False):
85 if not cursor_in_new_line
:
87 cursor_in_new_line
= True
88 self
.to_screen('[rtmpdump] ' + line
)
89 if not cursor_in_new_line
:
92 except BaseException
: # Including KeyboardInterrupt
93 proc
.kill(timeout
=None)
96 url
= info_dict
['url']
97 player_url
= info_dict
.get('player_url')
98 page_url
= info_dict
.get('page_url')
99 app
= info_dict
.get('app')
100 play_path
= info_dict
.get('play_path')
101 tc_url
= info_dict
.get('tc_url')
102 flash_version
= info_dict
.get('flash_version')
103 live
= info_dict
.get('rtmp_live', False)
104 conn
= info_dict
.get('rtmp_conn')
105 protocol
= info_dict
.get('rtmp_protocol')
106 real_time
= info_dict
.get('rtmp_real_time', False)
107 no_resume
= info_dict
.get('no_resume', False)
108 continue_dl
= self
.params
.get('continuedl', True)
110 self
.report_destination(filename
)
111 tmpfilename
= self
.temp_name(filename
)
112 test
= self
.params
.get('test', False)
114 # Check for rtmpdump first
115 if not check_executable('rtmpdump', ['-h']):
116 self
.report_error('RTMP download detected but "rtmpdump" could not be run. Please install')
119 # Download using rtmpdump. rtmpdump returns exit code 2 when
120 # the connection was interrupted and resuming appears to be
121 # possible. This is part of rtmpdump's normal usage, AFAIK.
123 'rtmpdump', '--verbose', '-r', url
,
125 if player_url
is not None:
126 basic_args
+= ['--swfVfy', player_url
]
127 if page_url
is not None:
128 basic_args
+= ['--pageUrl', page_url
]
130 basic_args
+= ['--app', app
]
131 if play_path
is not None:
132 basic_args
+= ['--playpath', play_path
]
133 if tc_url
is not None:
134 basic_args
+= ['--tcUrl', tc_url
]
136 basic_args
+= ['--stop', '1']
137 if flash_version
is not None:
138 basic_args
+= ['--flashVer', flash_version
]
140 basic_args
+= ['--live']
141 if isinstance(conn
, list):
143 basic_args
+= ['--conn', entry
]
144 elif isinstance(conn
, str):
145 basic_args
+= ['--conn', conn
]
146 if protocol
is not None:
147 basic_args
+= ['--protocol', protocol
]
149 basic_args
+= ['--realtime']
152 if not no_resume
and continue_dl
and not live
:
154 if not live
and continue_dl
:
155 args
+= ['--skip', '1']
157 args
= [encodeArgument(a
) for a
in args
]
159 self
._debug
_cmd
(args
, exe
='rtmpdump')
166 started
= time
.time()
169 retval
= run_rtmpdump(args
)
170 except KeyboardInterrupt:
171 if not info_dict
.get('is_live'):
174 self
.to_screen('\n[rtmpdump] Interrupted by user')
176 if retval
== RD_NO_CONNECT
:
177 self
.report_error('[rtmpdump] Could not connect to RTMP server.')
180 while retval
in (RD_INCOMPLETE
, RD_FAILED
) and not test
and not live
:
181 prevsize
= os
.path
.getsize(tmpfilename
)
182 self
.to_screen(f
'[rtmpdump] Downloaded {prevsize} bytes')
183 time
.sleep(5.0) # This seems to be needed
184 args
= [*basic_args
, '--resume']
185 if retval
== RD_FAILED
:
186 args
+= ['--skip', '1']
187 args
= [encodeArgument(a
) for a
in args
]
188 retval
= run_rtmpdump(args
)
189 cursize
= os
.path
.getsize(tmpfilename
)
190 if prevsize
== cursize
and retval
== RD_FAILED
:
192 # Some rtmp streams seem abort after ~ 99.8%. Don't complain for those
193 if prevsize
== cursize
and retval
== RD_INCOMPLETE
and cursize
> 1024:
194 self
.to_screen('[rtmpdump] Could not download the whole video. This can happen for some advertisements.')
197 if retval
== RD_SUCCESS
or (test
and retval
== RD_INCOMPLETE
):
198 fsize
= os
.path
.getsize(tmpfilename
)
199 self
.to_screen(f
'[rtmpdump] Downloaded {fsize} bytes')
200 self
.try_rename(tmpfilename
, filename
)
201 self
._hook
_progress
({
202 'downloaded_bytes': fsize
,
203 'total_bytes': fsize
,
204 'filename': filename
,
205 'status': 'finished',
206 'elapsed': time
.time() - started
,
211 self
.report_error('rtmpdump exited with code %d' % retval
)