Merge branch 'jukebox' of https://github.com/remitamine/youtube-dl into remitamine...
[youtube-dl] / 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     handle_youtubedl_headers,
17 )
18
19
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)
25
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.')
29             return False
30         ffpp.check_version()
31
32         args = [ffpp.executable, '-y']
33
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'])
38             args += [
39                 '-headers',
40                 ''.join('%s: %s\r\n' % (key, val) for key, val in headers.items())]
41
42         args += ['-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc']
43
44         args = [encodeArgument(opt) for opt in args]
45         args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
46
47         self._debug_cmd(args)
48
49         retval = subprocess.call(args)
50         if retval == 0:
51             fsize = os.path.getsize(encodeFilename(tmpfilename))
52             self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
53             self.try_rename(tmpfilename, filename)
54             self._hook_progress({
55                 'downloaded_bytes': fsize,
56                 'total_bytes': fsize,
57                 'filename': filename,
58                 'status': 'finished',
59             })
60             return True
61         else:
62             self.to_stderr('\n')
63             self.report_error('%s exited with code %d' % (ffpp.basename, retval))
64             return False
65
66
67 class NativeHlsFD(FragmentFD):
68     """ A more limited implementation that does not require ffmpeg """
69
70     FD_NAME = 'hlsnative'
71
72     def real_download(self, filename, info_dict):
73         man_url = info_dict['url']
74         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
75         manifest = self.ydl.urlopen(man_url).read()
76
77         s = manifest.decode('utf-8', 'ignore')
78         fragment_urls = []
79         for line in s.splitlines():
80             line = line.strip()
81             if line and not line.startswith('#'):
82                 segment_url = (
83                     line
84                     if re.match(r'^https?://', line)
85                     else compat_urlparse.urljoin(man_url, line))
86                 fragment_urls.append(segment_url)
87                 # We only download the first fragment during the test
88                 if self.params.get('test', False):
89                     break
90
91         ctx = {
92             'filename': filename,
93             'total_frags': len(fragment_urls),
94         }
95
96         self._prepare_and_start_frag_download(ctx)
97
98         frags_filenames = []
99         for i, frag_url in enumerate(fragment_urls):
100             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
101             success = ctx['dl'].download(frag_filename, {'url': frag_url})
102             if not success:
103                 return False
104             down, frag_sanitized = sanitize_open(frag_filename, 'rb')
105             ctx['dest_stream'].write(down.read())
106             down.close()
107             frags_filenames.append(frag_sanitized)
108
109         self._finish_frag_download(ctx)
110
111         for frag_file in frags_filenames:
112             os.remove(encodeFilename(frag_file))
113
114         return True