]>
jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/hls.py
1 from __future__
import unicode_literals
7 from .common
import FileDownloader
8 from .fragment
import FragmentFD
10 from ..compat
import compat_urlparse
11 from ..postprocessor
.ffmpeg
import FFmpegPostProcessor
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
)
24 ffpp
= FFmpegPostProcessor(downloader
=self
)
25 if not ffpp
.available
:
26 self
.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
32 for opt
in (ffpp
.executable
, '-y', '-i', url
, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
33 args
.append(encodeFilename(tmpfilename
, True))
37 retval
= subprocess
.call(args
)
39 fsize
= os
.path
.getsize(encodeFilename(tmpfilename
))
40 self
.to_screen('\r[%s] %s bytes' % (args
[0], fsize
))
41 self
.try_rename(tmpfilename
, filename
)
43 'downloaded_bytes': fsize
,
51 self
.report_error('%s exited with code %d' % (ffpp
.basename
, retval
))
55 class NativeHlsFD(FragmentFD
):
56 """ A more limited implementation that does not require ffmpeg """
60 def real_download(self
, filename
, info_dict
):
61 man_url
= info_dict
['url']
62 self
.to_screen('[%s] Downloading m3u8 manifest' % self
.FD_NAME
)
63 manifest
= self
.ydl
.urlopen(man_url
).read()
65 s
= manifest
.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(man_url
, line
))
74 fragment_urls
.append(segment_url
)
75 # We only download the first fragment during the test
76 if self
.params
.get('test', False):
81 'total_frags': len(fragment_urls
),
84 self
._prepare
_and
_start
_frag
_download
(ctx
)
87 for i
, frag_url
in enumerate(fragment_urls
):
88 frag_filename
= '%s-Frag%d' % (ctx
['tmpfilename'], i
)
89 success
= ctx
['dl'].download(frag_filename
, {'url': frag_url}
)
92 with open(frag_filename
, 'rb') as down
:
93 ctx
['dest_stream'].write(down
.read())
94 frags_filenames
.append(frag_filename
)
96 self
._finish
_frag
_download
(ctx
)
98 for frag_file
in frags_filenames
: