]> jfr.im git - yt-dlp.git/blob - youtube_dl/downloader/hls.py
[postprocessor/ffmpeg] Always use the 'file:' protocol for filenames (fixes #6874)
[yt-dlp.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 .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 )
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 if not ffpp.available:
27 self.report_error('m3u8 download detected but ffmpeg or avconv could not be found. Please install one.')
28 return False
29 ffpp.check_version()
30
31 args = [
32 encodeArgument(opt)
33 for opt in (ffpp.executable, '-y', '-i', url, '-f', 'mp4', '-c', 'copy', '-bsf:a', 'aac_adtstoasc')]
34 args.append(encodeFilename(ffpp._ffmpeg_filename_argument(tmpfilename), True))
35
36 self._debug_cmd(args)
37
38 retval = subprocess.call(args)
39 if retval == 0:
40 fsize = os.path.getsize(encodeFilename(tmpfilename))
41 self.to_screen('\r[%s] %s bytes' % (args[0], fsize))
42 self.try_rename(tmpfilename, filename)
43 self._hook_progress({
44 'downloaded_bytes': fsize,
45 'total_bytes': fsize,
46 'filename': filename,
47 'status': 'finished',
48 })
49 return True
50 else:
51 self.to_stderr('\n')
52 self.report_error('%s exited with code %d' % (ffpp.basename, retval))
53 return False
54
55
56 class NativeHlsFD(FragmentFD):
57 """ A more limited implementation that does not require ffmpeg """
58
59 FD_NAME = 'hlsnative'
60
61 def real_download(self, filename, info_dict):
62 man_url = info_dict['url']
63 self.to_screen('[%s] Downloading m3u8 manifest' % self.FD_NAME)
64 manifest = self.ydl.urlopen(man_url).read()
65
66 s = manifest.decode('utf-8', 'ignore')
67 fragment_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(man_url, line))
75 fragment_urls.append(segment_url)
76 # We only download the first fragment during the test
77 if self.params.get('test', False):
78 break
79
80 ctx = {
81 'filename': filename,
82 'total_frags': len(fragment_urls),
83 }
84
85 self._prepare_and_start_frag_download(ctx)
86
87 frags_filenames = []
88 for i, frag_url in enumerate(fragment_urls):
89 frag_filename = '%s-Frag%d' % (ctx['tmpfilename'], i)
90 success = ctx['dl'].download(frag_filename, {'url': frag_url})
91 if not success:
92 return False
93 down, frag_sanitized = sanitize_open(frag_filename, 'rb')
94 ctx['dest_stream'].write(down.read())
95 down.close()
96 frags_filenames.append(frag_sanitized)
97
98 self._finish_frag_download(ctx)
99
100 for frag_file in frags_filenames:
101 os.remove(encodeFilename(frag_file))
102
103 return True