]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/downloader/hls.py
Fix imports and general cleanup
[youtube-dl.git] / youtube_dl / downloader / hls.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5 import subprocess
6
7 from ..postprocessor.ffmpeg import FFmpegPostProcessor
8 from .common import FileDownloader
9 from ..compat import (
10     compat_urlparse,
11     compat_urllib_request,
12 )
13 from ..utils import (
14     check_executable,
15     encodeFilename,
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         args = [
26             '-y', '-i', url, '-f', 'mp4', '-c', 'copy',
27             '-bsf:a', 'aac_adtstoasc',
28             encodeFilename(tmpfilename, for_subprocess=True)]
29
30         for program in ['avconv', 'ffmpeg']:
31             if check_executable(program, ['-version']):
32                 break
33         else:
34             self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
35             return False
36         cmd = [program] + args
37
38         ffpp = FFmpegPostProcessor(downloader=self)
39         ffpp.check_version()
40
41         retval = subprocess.call(cmd)
42         if retval == 0:
43             fsize = os.path.getsize(encodeFilename(tmpfilename))
44             self.to_screen('\r[%s] %s bytes' % (cmd[0], fsize))
45             self.try_rename(tmpfilename, filename)
46             self._hook_progress({
47                 'downloaded_bytes': fsize,
48                 'total_bytes': fsize,
49                 'filename': filename,
50                 'status': 'finished',
51             })
52             return True
53         else:
54             self.to_stderr('\n')
55             self.report_error('%s exited with code %d' % (program, retval))
56             return False
57
58
59 class NativeHlsFD(FileDownloader):
60     """ A more limited implementation that does not require ffmpeg """
61
62     def real_download(self, filename, info_dict):
63         url = info_dict['url']
64         self.report_destination(filename)
65         tmpfilename = self.temp_name(filename)
66
67         self.to_screen(
68             '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
69         data = self.ydl.urlopen(url).read()
70         s = data.decode('utf-8', 'ignore')
71         segment_urls = []
72         for line in s.splitlines():
73             line = line.strip()
74             if line and not line.startswith('#'):
75                 segment_url = (
76                     line
77                     if re.match(r'^https?://', line)
78                     else compat_urlparse.urljoin(url, line))
79                 segment_urls.append(segment_url)
80
81         is_test = self.params.get('test', False)
82         remaining_bytes = self._TEST_FILE_SIZE if is_test else None
83         byte_counter = 0
84         with open(tmpfilename, 'wb') as outf:
85             for i, segurl in enumerate(segment_urls):
86                 self.to_screen(
87                     '[hlsnative] %s: Downloading segment %d / %d' %
88                     (info_dict['id'], i + 1, len(segment_urls)))
89                 seg_req = compat_urllib_request.Request(segurl)
90                 if remaining_bytes is not None:
91                     seg_req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1))
92
93                 segment = self.ydl.urlopen(seg_req).read()
94                 if remaining_bytes is not None:
95                     segment = segment[:remaining_bytes]
96                     remaining_bytes -= len(segment)
97                 outf.write(segment)
98                 byte_counter += len(segment)
99                 if remaining_bytes is not None and remaining_bytes <= 0:
100                     break
101
102         self._hook_progress({
103             'downloaded_bytes': byte_counter,
104             'total_bytes': byte_counter,
105             'filename': filename,
106             'status': 'finished',
107         })
108         self.try_rename(tmpfilename, filename)
109         return True