]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/hls.py
Add note about mplayer2 and backporters.
[youtubedl] / youtube_dl / downloader / hls.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5 import subprocess
6
7 from .common import FileDownloader
8 from .fragment import FragmentFD
9
10 from ..compat import compat_urlparse
11 from ..postprocessor.ffmpeg import FFmpegPostProcessor
12 from ..utils import (
13 encodeArgument,
14 encodeFilename,
15 sanitize_open,
16 )
17
18
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)
24
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.')
28 return False
29 ffpp.check_version()
30
31 args = [ffpp.executable, '-y']
32
33 if info_dict['http_headers'] and re.match(r'^https?://', url):
34 # Trailing \r\n after each HTTP header is important to prevent warning from ffmpeg/avconv:
35 # [http @ 00000000003d2fa0] No trailing CRLF found in HTTP header.
36 args += [
37 '-headers',
38 ''.join('%s: %s\r\n' % (key, val) for key, val in info_dict['http_headers'].items())]
39
40 args += ['-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc']
41
42 args = [encodeArgument(opt) for opt in args]
43 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
44
45 self._debug_cmd(args)
46
47 retval = subprocess.call(args)
48 if retval == 0:
49 fsize = os.path.getsize(encodeFilename(tmpfilename))
50 self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
51 self.try_rename(tmpfilename, filename)
52 self._hook_progress({
53 'downloaded_bytes': fsize,
54 'total_bytes': fsize,
55 'filename': filename,
56 'status': 'finished',
57 })
58 return True
59 else:
60 self.to_stderr('\n')
61 self.report_error('%s exited with code %d' % (ffpp.basename, retval))
62 return False
63
64
65 class NativeHlsFD(FragmentFD):
66 """ A more limited implementation that does not require ffmpeg """
67
68 FD_NAME = 'hlsnative'
69
70 def real_download(self, filename, info_dict):
71 man_url = info_dict['url']
72 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
73 manifest = self.ydl.urlopen(man_url).read()
74
75 s = manifest.decode('utf-8', 'ignore')
76 fragment_urls = []
77 for line in s.splitlines():
78 line = line.strip()
79 if line and not line.startswith('#'):
80 segment_url = (
81 line
82 if re.match(r'^https?://', line)
83 else compat_urlparse.urljoin(man_url, line))
84 fragment_urls.append(segment_url)
85 # We only download the first fragment during the test
86 if self.params.get('test', False):
87 break
88
89 ctx = {
90 'filename': filename,
91 'total_frags': len(fragment_urls),
92 }
93
94 self._prepare_and_start_frag_download(ctx)
95
96 frags_filenames = []
97 for i, frag_url in enumerate(fragment_urls):
98 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
99 success = ctx['dl'].download(frag_filename, {'url': frag_url})
100 if not success:
101 return False
102 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
103 ctx['dest_stream'].write(down.read())
104 down.close()
105 frags_filenames.append(frag_sanitized)
106
107 self._finish_frag_download(ctx)
108
109 for frag_file in frags_filenames:
110 os.remove(encodeFilename(frag_file))
111
112 return True