]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/downloader/hls.py
Initiate a new release.
[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 ..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 encodeArgument,
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 ffpp = FFmpegPostProcessor(downloader=self)
26 program = ffpp._executable
27 if program is None:
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 = [
33 encodeArgument(opt)
34 for opt in (program, '-y', '-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
35 args.append(encodeFilename(tmpfilename, True))
36
37 retval = subprocess.call(args)
38 if retval == 0:
39 fsize = os.path.getsize(encodeFilename(tmpfilename))
40 self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
41 self.try_rename(tmpfilename, filename)
42 self._hook_progress({
43 'downloaded_bytes': fsize,
44 'total_bytes': fsize,
45 'filename': filename,
46 'status': 'finished',
47 })
48 return True
49 else:
50 self.to_stderr('\n')
51 self.report_error('%s exited with code %d' % (program, retval))
52 return False
53
54
55 class NativeHlsFD(FileDownloader):
56 """ A more limited implementation that does not require ffmpeg """
57
58 def real_download(self, filename, info_dict):
59 url = info_dict['url']
60 self.report_destination(filename)
61 tmpfilename = self.temp_name(filename)
62
63 self.to_screen(
64 '[hlsnative] %s: Downloading m3u8 manifest' % info_dict['id'])
65 data = self.ydl.urlopen(url).read()
66 s = data.decode('utf-8', 'ignore')
67 segment_urls = []
68 for line in s.splitlines():
69 line = line.strip()
70 if line and not line.startswith('#'):
71 segment_url = (
72 line
73 if re.match(r'^https?://', line)
74 else compat_urlparse.urljoin(url, line))
75 segment_urls.append(segment_url)
76
77 is_test = self.params.get('test', False)
78 remaining_bytes = self._TEST_FILE_SIZE if is_test else None
79 byte_counter = 0
80 with open(tmpfilename, 'wb') as outf:
81 for i, segurl in enumerate(segment_urls):
82 self.to_screen(
83 '[hlsnative] %s: Downloading segment %d / %d' %
84 (info_dict['id'], i + 1, len(segment_urls)))
85 seg_req = compat_urllib_request.Request(segurl)
86 if remaining_bytes is not None:
87 seg_req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1))
88
89 segment = self.ydl.urlopen(seg_req).read()
90 if remaining_bytes is not None:
91 segment = segment[:remaining_bytes]
92 remaining_bytes -= len(segment)
93 outf.write(segment)
94 byte_counter += len(segment)
95 if remaining_bytes is not None and remaining_bytes <= 0:
96 break
97
98 self._hook_progress({
99 'downloaded_bytes': byte_counter,
100 'total_bytes': byte_counter,
101 'filename': filename,
102 'status': 'finished',
103 })
104 self.try_rename(tmpfilename, filename)
105 return True