]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/hls.py
aa58b52abb5998ba8879e6eba3a1d974484467e2
1 from __future__
import unicode_literals
7 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
8 from .common
import FileDownloader
11 compat_urllib_request
,
18 class HlsFD(FileDownloader
):
19 def real_download(self
, filename
, info_dict
):
20 url
= info_dict
['url']
21 self
.report_destination(filename
)
22 tmpfilename
= self
.temp_name(filename
)
25 '-y', '-i', url
, '-f', 'mp4', '-c', 'copy',
26 '-bsf:a', 'aac_adtstoasc',
27 encodeFilename(tmpfilename
, for_subprocess
=True)]
29 ffpp
= FFmpegPostProcessor(downloader
=self
)
30 program
= ffpp
._executable
32 self
.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
35 cmd
= [program
] + args
37 retval
= subprocess
.call(cmd
)
39 fsize
= os
.path
.getsize(encodeFilename(tmpfilename
))
40 self
.to_screen('\r[%s] %s bytes' % (cmd
[0], fsize
))
41 self
.try_rename(tmpfilename
, filename
)
43 'downloaded_bytes': fsize
,
51 self
.report_error('%s exited with code %d' % (program
, retval
))
55 class NativeHlsFD(FileDownloader
):
56 """ A more limited implementation that does not require ffmpeg """
58 def real_download(self
, filename
, info_dict
):
59 url
= info_dict
['url']
60 self
.report_destination(filename
)
61 tmpfilename
= self
.temp_name(filename
)
64 '[hlsnative] %s: Downloading m3u8 manifest' % info_dict
['id'])
65 data
= self
.ydl
.urlopen(url
).read()
66 s
= data
.decode('utf-8', 'ignore')
68 for line
in s
.splitlines():
70 if line
and not line
.startswith('#'):
73 if re
.match(r
'^https?://', line
)
74 else compat_urlparse
.urljoin(url
, line
))
75 segment_urls
.append(segment_url
)
77 is_test
= self
.params
.get('test', False)
78 remaining_bytes
= self
._TEST
_FILE
_SIZE
if is_test
else None
80 with open(tmpfilename
, 'wb') as outf
:
81 for i
, segurl
in enumerate(segment_urls
):
83 '[hlsnative] %s: Downloading segment %d / %d' %
84 (info_dict
['id'], i
+ 1, len(segment_urls
)))
85 seg_req
= compat_urllib_request
.Request(segurl
)
86 if remaining_bytes
is not None:
87 seg_req
.add_header('Range', 'bytes=0-%d' % (remaining_bytes
- 1))
89 segment
= self
.ydl
.urlopen(seg_req
).read()
90 if remaining_bytes
is not None:
91 segment
= segment
[:remaining_bytes
]
92 remaining_bytes
-= len(segment
)
94 byte_counter
+= len(segment
)
95 if remaining_bytes
is not None and remaining_bytes
<= 0:
99 'downloaded_bytes': byte_counter
,
100 'total_bytes': byte_counter
,
101 'filename': filename
,
102 'status': 'finished',
104 self
.try_rename(tmpfilename
, filename
)