[downloader/http] Simplify
[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 )
16
17
18 class HlsFD(FileDownloader):
19     def real_download(self, filename, info_dict):
20         url = info_dict['url']
21         self.report_destination(filename)
22         tmpfilename = self.temp_name(filename)
23
24         ffpp = FFmpegPostProcessor(downloader=self)
25         if not ffpp.available:
26             self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
27             return False
28         ffpp.check_version()
29
30         args = [
31             encodeArgument(opt)
32             for opt in (ffpp.executable, '-y', '-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
33         args.append(encodeFilename(tmpfilename, True))
34
35         retval = subprocess.call(args)
36         if retval == 0:
37             fsize = os.path.getsize(encodeFilename(tmpfilename))
38             self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
39             self.try_rename(tmpfilename, filename)
40             self._hook_progress({
41                 'downloaded_bytes': fsize,
42                 'total_bytes': fsize,
43                 'filename': filename,
44                 'status': 'finished',
45             })
46             return True
47         else:
48             self.to_stderr('\n')
49             self.report_error('%s exited with code %d' % (ffpp.basename, retval))
50             return False
51
52
53 class NativeHlsFD(FragmentFD):
54     """ A more limited implementation that does not require ffmpeg """
55
56     FD_NAME = 'hlsnative'
57
58     def real_download(self, filename, info_dict):
59         man_url = info_dict['url']
60         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
61         manifest = self.ydl.urlopen(man_url).read()
62
63         s = manifest.decode('utf-8', 'ignore')
64         fragment_urls = []
65         for line in s.splitlines():
66             line = line.strip()
67             if line and not line.startswith('#'):
68                 segment_url = (
69                     line
70                     if re.match(r'^https?://', line)
71                     else compat_urlparse.urljoin(man_url, line))
72                 fragment_urls.append(segment_url)
73                 # We only download the first fragment during the test
74                 if self.params.get('test', False):
75                     break
76
77         ctx = {
78             'filename': filename,
79             'total_frags': len(fragment_urls),
80         }
81
82         self._prepare_and_start_frag_download(ctx)
83
84         frags_filenames = []
85         for i, frag_url in enumerate(fragment_urls):
86             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
87             success = ctx['dl'].download(frag_filename, {'url': frag_url})
88             if not success:
89                 return False
90             with open(frag_filename, 'rb') as down:
91                 ctx['dest_stream'].write(down.read())
92             frags_filenames.append(frag_filename)
93
94         self._finish_frag_download(ctx)
95
96         for frag_file in frags_filenames:
97             os.remove(frag_file)
98
99         return True