Merge branch 'shahid' 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 )
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         self._debug_cmd(args)
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' % (ffpp.basename, retval))
52             return False
53
54
55 class NativeHlsFD(FragmentFD):
56     """ A more limited implementation that does not require ffmpeg """
57
58     FD_NAME = 'hlsnative'
59
60     def real_download(self, filename, info_dict):
61         man_url = info_dict['url']
62         self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
63         manifest = self.ydl.urlopen(man_url).read()
64
65         s = manifest.decode('utf-8', 'ignore')
66         fragment_urls = []
67         for line in s.splitlines():
68             line = line.strip()
69             if line and not line.startswith('#'):
70                 segment_url = (
71                     line
72                     if re.match(r'^https?://', line)
73                     else compat_urlparse.urljoin(man_url, line))
74                 fragment_urls.append(segment_url)
75                 # We only download the first fragment during the test
76                 if self.params.get('test', False):
77                     break
78
79         ctx = {
80             'filename': filename,
81             'total_frags': len(fragment_urls),
82         }
83
84         self._prepare_and_start_frag_download(ctx)
85
86         frags_filenames = []
87         for i, frag_url in enumerate(fragment_urls):
88             frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
89             success = ctx['dl'].download(frag_filename, {'url': frag_url})
90             if not success:
91                 return False
92             with open(frag_filename, 'rb') as down:
93                 ctx['dest_stream'].write(down.read())
94             frags_filenames.append(frag_filename)
95
96         self._finish_frag_download(ctx)
97
98         for frag_file in frags_filenames:
99             os.remove(frag_file)
100
101         return True