]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/hls.py
1 from __future__
import unicode_literals
7 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
8 from .common
import FileDownloader
11 compat_urllib_request
,
19 class HlsFD(FileDownloader
):
20 def real_download(self
, filename
, info_dict
):
21 url
= info_dict
['url']
22 self
.report_destination(filename
)
23 tmpfilename
= self
.temp_name(filename
)
25 ffpp
= FFmpegPostProcessor(downloader
=self
)
26 if not ffpp
.available
:
27 self
.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
33 for opt
in (ffpp
.executable
, '-y', '-i', url
, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
34 args
.append(encodeFilename(tmpfilename
, True))
36 retval
= subprocess
.call(args
)
38 fsize
= os
.path
.getsize(encodeFilename(tmpfilename
))
39 self
.to_screen('\r[%s] %s bytes' % (args
[0], fsize
))
40 self
.try_rename(tmpfilename
, filename
)
42 'downloaded_bytes': fsize
,
50 self
.report_error('%s exited with code %d' % (ffpp
.basename
, retval
))
54 class NativeHlsFD(FileDownloader
):
55 """ A more limited implementation that does not require ffmpeg """
57 def real_download(self
, filename
, info_dict
):
58 url
= info_dict
['url']
59 self
.report_destination(filename
)
60 tmpfilename
= self
.temp_name(filename
)
63 '[hlsnative] %s: Downloading m3u8 manifest' % info_dict
['id'])
64 data
= self
.ydl
.urlopen(url
).read()
65 s
= data
.decode('utf-8', 'ignore')
67 for line
in s
.splitlines():
69 if line
and not line
.startswith('#'):
72 if re
.match(r
'^https?://', line
)
73 else compat_urlparse
.urljoin(url
, line
))
74 segment_urls
.append(segment_url
)
76 is_test
= self
.params
.get('test', False)
77 remaining_bytes
= self
._TEST
_FILE
_SIZE
if is_test
else None
79 with open(tmpfilename
, 'wb') as outf
:
80 for i
, segurl
in enumerate(segment_urls
):
82 '[hlsnative] %s: Downloading segment %d / %d' %
83 (info_dict
['id'], i
+ 1, len(segment_urls
)))
84 seg_req
= compat_urllib_request
.Request(segurl
)
85 if remaining_bytes
is not None:
86 seg_req
.add_header('Range', 'bytes=0-%d' % (remaining_bytes
- 1))
88 segment
= self
.ydl
.urlopen(seg_req
).read()
89 if remaining_bytes
is not None:
90 segment
= segment
[:remaining_bytes
]
91 remaining_bytes
-= len(segment
)
93 byte_counter
+= len(segment
)
94 if remaining_bytes
is not None and remaining_bytes
<= 0:
98 'downloaded_bytes': byte_counter
,
99 'total_bytes': byte_counter
,
100 'filename': filename
,
101 'status': 'finished',
103 self
.try_rename(tmpfilename
, filename
)