]>
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
16 handle_youtubedl_headers
,
20 class HlsFD(FileDownloader
):
21 def real_download(self
, filename
, info_dict
):
22 url
= info_dict
['url']
23 self
.report_destination(filename
)
24 tmpfilename
= self
.temp_name(filename
)
26 ffpp
= FFmpegPostProcessor(downloader
=self
)
27 if not ffpp
.available
:
28 self
.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
32 args
= [ffpp
.executable
, '-y']
34 if info_dict
['http_headers'] and re
.match(r
'^https?://', url
):
35 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
36 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
37 headers
= handle_youtubedl_headers(info_dict
['http_headers'])
40 ''.join('%s: %s\r\n' % (key
, val
) for key
, val
in headers
.items())]
42 args
+= ['-i', url
, '-c', 'copy']
43 if self
.params
.get('hls_use_mpegts', False):
44 args
+= ['-f', 'mpegts']
46 args
+= ['-f', 'mp4', '-bsf:a', 'aac_adtstoasc']
48 args
= [encodeArgument(opt
) for opt
in args
]
49 args
.append(encodeFilename(ffpp
._ffmpeg
_filename
_argument
(tmpfilename
), True))
53 proc
= subprocess
.Popen(args
, stdin
=subprocess
.PIPE
)
56 except KeyboardInterrupt:
57 # subprocces.run would send the SIGKILL signal to ffmpeg and the
58 # mp4 file couldn't be played, but if we ask ffmpeg to quit it
59 # produces a file that is playable (this is mostly useful for live
61 proc
.communicate(b
'q')
64 fsize
= os
.path
.getsize(encodeFilename(tmpfilename
))
65 self
.to_screen('\r[%s] %s bytes' % (args
[0], fsize
))
66 self
.try_rename(tmpfilename
, filename
)
68 'downloaded_bytes': fsize
,
76 self
.report_error('%s exited with code %d' % (ffpp
.basename
, retval
))
80 class NativeHlsFD(FragmentFD
):
81 """ A more limited implementation that does not require ffmpeg """
85 def real_download(self
, filename
, info_dict
):
86 man_url
= info_dict
['url']
87 self
.to_screen('[%s] Downloading m3u8 manifest' % self
.FD_NAME
)
88 manifest
= self
.ydl
.urlopen(man_url
).read()
90 s
= manifest
.decode('utf-8', 'ignore')
92 for line
in s
.splitlines():
94 if line
and not line
.startswith('#'):
97 if re
.match(r
'^https?://', line
)
98 else compat_urlparse
.urljoin(man_url
, line
))
99 fragment_urls
.append(segment_url
)
100 # We only download the first fragment during the test
101 if self
.params
.get('test', False):
105 'filename': filename
,
106 'total_frags': len(fragment_urls
),
109 self
._prepare
_and
_start
_frag
_download
(ctx
)
112 for i
, frag_url
in enumerate(fragment_urls
):
113 frag_filename
= '%s-Frag%d' % (ctx
['tmpfilename'], i
)
114 success
= ctx
['dl'].download(frag_filename
, {'url': frag_url}
)
117 down
, frag_sanitized
= sanitize_open(frag_filename
, 'rb')
118 ctx
['dest_stream'].write(down
.read())
120 frags_filenames
.append(frag_sanitized
)
122 self
._finish
_frag
_download
(ctx
)
124 for frag_file
in frags_filenames
:
125 os
.remove(encodeFilename(frag_file
))