1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-02 09:12:40 +01:00

[ffmpeg] Check version lazily

Closes #3830
This commit is contained in:
pukkandan 2022-05-22 19:51:22 +05:30
parent c4a62b99f6
commit 3a85e9cee9
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

View File

@ -7,7 +7,7 @@
import time import time
from .common import AudioConversionError, PostProcessor from .common import AudioConversionError, PostProcessor
from ..compat import imghdr from ..compat import functools, imghdr
from ..utils import ( from ..utils import (
ISO639Utils, ISO639Utils,
Popen, Popen,
@ -18,6 +18,7 @@
dfxp2srt, dfxp2srt,
encodeArgument, encodeArgument,
encodeFilename, encodeFilename,
filter_dict,
float_or_none, float_or_none,
is_outdated_version, is_outdated_version,
orderedSet, orderedSet,
@ -62,14 +63,15 @@ class FFmpegPostProcessorError(PostProcessingError):
class FFmpegPostProcessor(PostProcessor): class FFmpegPostProcessor(PostProcessor):
def __init__(self, downloader=None): def __init__(self, downloader=None):
PostProcessor.__init__(self, downloader) PostProcessor.__init__(self, downloader)
self._determine_executables() self._prefer_ffmpeg = self.get_param('prefer_ffmpeg', True)
self._paths = self._determine_executables()
def check_version(self): def check_version(self):
if not self.available: if not self.available:
raise FFmpegPostProcessorError('ffmpeg not found. Please install or provide the path using --ffmpeg-location') raise FFmpegPostProcessorError('ffmpeg not found. Please install or provide the path using --ffmpeg-location')
required_version = '10-0' if self.basename == 'avconv' else '1.0' required_version = '10-0' if self.basename == 'avconv' else '1.0'
if is_outdated_version(self._versions[self.basename], required_version): if is_outdated_version(self._version, required_version):
self.report_warning(f'Your copy of {self.basename} is outdated, update {self.basename} ' self.report_warning(f'Your copy of {self.basename} is outdated, update {self.basename} '
f'to version {required_version} or newer if you encounter any errors') f'to version {required_version} or newer if you encounter any errors')
@ -82,15 +84,38 @@ def get_versions_and_features(downloader=None):
def get_versions(downloader=None): def get_versions(downloader=None):
return FFmpegPostProcessor.get_versions_and_features(downloader)[0] return FFmpegPostProcessor.get_versions_and_features(downloader)[0]
_version_cache, _features_cache = {}, {} _ffmpeg_to_avconv = {'ffmpeg': 'avconv', 'ffprobe': 'avprobe'}
def _determine_executables(self): def _determine_executables(self):
programs = ['avprobe', 'avconv', 'ffmpeg', 'ffprobe'] programs = [*self._ffmpeg_to_avconv.keys(), *self._ffmpeg_to_avconv.values()]
def get_ffmpeg_version(path, prog): location = self.get_param('ffmpeg_location')
if location is None:
return {p: p for p in programs}
if not os.path.exists(location):
self.report_warning(f'ffmpeg-location {location} does not exist! Continuing without ffmpeg')
return {}
elif os.path.isdir(location):
dirname, basename = location, None
else:
basename = os.path.splitext(os.path.basename(location))[0]
basename = next((p for p in programs if basename.startswith(p)), 'ffmpeg')
dirname = os.path.dirname(os.path.abspath(location))
if basename in self._ffmpeg_to_avconv.keys():
self._prefer_ffmpeg = True
paths = {p: os.path.join(dirname, p) for p in programs}
if basename:
paths[basename] = location
return paths
_version_cache, _features_cache = {None: None}, {}
def _get_ffmpeg_version(self, prog):
path = self._paths.get(prog)
if path in self._version_cache: if path in self._version_cache:
self._versions[prog], self._features = self._version_cache[path], self._features_cache.get(path, {}) return self._version_cache[path], self._features_cache.get(path, {})
return
out = _get_exe_version_output(path, ['-bsfs'], to_screen=self.write_debug) out = _get_exe_version_output(path, ['-bsfs'], to_screen=self.write_debug)
ver = detect_exe_version(out) if out else False ver = detect_exe_version(out) if out else False
if ver: if ver:
@ -103,67 +128,55 @@ def get_ffmpeg_version(path, prog):
mobj = re.match(regex, ver) mobj = re.match(regex, ver)
if mobj: if mobj:
ver = mobj.group(1) ver = mobj.group(1)
self._versions[prog] = self._version_cache[path] = ver self._version_cache[path] = ver
if prog != 'ffmpeg' or not out: if prog != 'ffmpeg' or not out:
return return ver, {}
mobj = re.search(r'(?m)^\s+libavformat\s+(?:[0-9. ]+)\s+/\s+(?P<runtime>[0-9. ]+)', out) mobj = re.search(r'(?m)^\s+libavformat\s+(?:[0-9. ]+)\s+/\s+(?P<runtime>[0-9. ]+)', out)
lavf_runtime_version = mobj.group('runtime').replace(' ', '') if mobj else None lavf_runtime_version = mobj.group('runtime').replace(' ', '') if mobj else None
self._features = self._features_cache[path] = { self._features_cache[path] = features = {
'fdk': '--enable-libfdk-aac' in out, 'fdk': '--enable-libfdk-aac' in out,
'setts': 'setts' in out.splitlines(), 'setts': 'setts' in out.splitlines(),
'needs_adtstoasc': is_outdated_version(lavf_runtime_version, '57.56.100', False), 'needs_adtstoasc': is_outdated_version(lavf_runtime_version, '57.56.100', False),
} }
return ver, features
self.basename = None @property
self.probe_basename = None def _versions(self):
self._paths = None return filter_dict({self.basename: self._version, self.probe_basename: self._probe_version})
self._versions = None
self._features = {}
prefer_ffmpeg = self.get_param('prefer_ffmpeg', True) @functools.cached_property
location = self.get_param('ffmpeg_location') def basename(self):
if location is None: self._version # run property
self._paths = {p: p for p in programs} return self.basename
@functools.cached_property
def probe_basename(self):
self._probe_version # run property
return self.probe_basename
def _get_version(self, kind):
executables = (kind, self._ffmpeg_to_avconv[kind])
if not self._prefer_ffmpeg:
executables = reversed(executables)
basename, version, features = next(filter(
lambda x: x[1], ((p, *self._get_ffmpeg_version(p)) for p in executables)), (None, None, {}))
if kind == 'ffmpeg':
self.basename, self._features = basename, features
else: else:
if not os.path.exists(location): self.probe_basename = basename
self.report_warning( if basename == self._ffmpeg_to_avconv[kind]:
'ffmpeg-location %s does not exist! '
'Continuing without ffmpeg.' % (location))
self._versions = {}
return
elif os.path.isdir(location):
dirname, basename = location, None
else:
basename = os.path.splitext(os.path.basename(location))[0]
basename = next((p for p in programs if basename.startswith(p)), 'ffmpeg')
dirname = os.path.dirname(os.path.abspath(location))
if basename in ('ffmpeg', 'ffprobe'):
prefer_ffmpeg = True
self._paths = {
p: os.path.join(dirname, p) for p in programs}
if basename:
self._paths[basename] = location
self._versions = {}
# NB: probe must be first for _features to be poulated correctly
executables = {'probe_basename': ('ffprobe', 'avprobe'), 'basename': ('ffmpeg', 'avconv')}
if prefer_ffmpeg is False:
executables = {k: v[::-1] for k, v in executables.items()}
for var, prefs in executables.items():
for p in prefs:
get_ffmpeg_version(self._paths[p], p)
if self._versions[p]:
setattr(self, var, p)
break
if self.basename == 'avconv':
self.deprecation_warning( self.deprecation_warning(
'Support for avconv is deprecated and may be removed in a future version. Use ffmpeg instead') f'Support for {self._ffmpeg_to_avconv[kind]} is deprecated and may be removed in a future version. Use {kind} instead')
if self.probe_basename == 'avprobe': return version
self.deprecation_warning(
'Support for avprobe is deprecated and may be removed in a future version. Use ffprobe instead') @functools.cached_property
def _version(self):
return self._get_version('ffmpeg')
@functools.cached_property
def _probe_version(self):
return self._get_version('ffprobe')
@property @property
def available(self): def available(self):
@ -171,7 +184,7 @@ def available(self):
@property @property
def executable(self): def executable(self):
return self._paths[self.basename] return self._paths.get(self.basename)
@property @property
def probe_available(self): def probe_available(self):
@ -179,7 +192,7 @@ def probe_available(self):
@property @property
def probe_executable(self): def probe_executable(self):
return self._paths[self.probe_basename] return self._paths.get(self.probe_basename)
@staticmethod @staticmethod
def stream_copy_opts(copy=True, *, ext=None): def stream_copy_opts(copy=True, *, ext=None):