mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-12-22 06:00:00 +00:00
[XAttrMetadata] Refactor and document dependencies
This commit is contained in:
parent
43d7f5a5d0
commit
6f7563beb7
5 changed files with 81 additions and 118 deletions
|
@ -273,6 +273,7 @@ ## DEPENDENCIES
|
||||||
* [**secretstorage**](https://github.com/mitya57/secretstorage) - For accessing the Gnome keyring while decrypting cookies of Chromium-based browsers on Linux. Licensed under [BSD-3-Clause](https://github.com/mitya57/secretstorage/blob/master/LICENSE)
|
* [**secretstorage**](https://github.com/mitya57/secretstorage) - For accessing the Gnome keyring while decrypting cookies of Chromium-based browsers on Linux. Licensed under [BSD-3-Clause](https://github.com/mitya57/secretstorage/blob/master/LICENSE)
|
||||||
* [**brotli**](https://github.com/google/brotli)\* or [**brotlicffi**](https://github.com/python-hyper/brotlicffi) - [Brotli](https://en.wikipedia.org/wiki/Brotli) content encoding support. Both licensed under MIT <sup>[1](https://github.com/google/brotli/blob/master/LICENSE) [2](https://github.com/python-hyper/brotlicffi/blob/master/LICENSE) </sup>
|
* [**brotli**](https://github.com/google/brotli)\* or [**brotlicffi**](https://github.com/python-hyper/brotlicffi) - [Brotli](https://en.wikipedia.org/wiki/Brotli) content encoding support. Both licensed under MIT <sup>[1](https://github.com/google/brotli/blob/master/LICENSE) [2](https://github.com/python-hyper/brotlicffi/blob/master/LICENSE) </sup>
|
||||||
* [**certifi**](https://github.com/certifi/python-certifi)\* - Provides Mozilla's root certificate bundle. Licensed under [MPLv2](https://github.com/certifi/python-certifi/blob/master/LICENSE)
|
* [**certifi**](https://github.com/certifi/python-certifi)\* - Provides Mozilla's root certificate bundle. Licensed under [MPLv2](https://github.com/certifi/python-certifi/blob/master/LICENSE)
|
||||||
|
* [**xattr**](https://github.com/xattr/xattr), [**pyxattr**](https://github.com/iustin/pyxattr) or [**setfattr**](http://savannah.nongnu.org/projects/attr) - For writing xattr metadata on Linux. Licensed under [MIT](https://github.com/xattr/xattr/blob/master/LICENSE.txt), [LGPL2.1](https://github.com/iustin/pyxattr/blob/master/COPYING) and [GPLv2+](http://git.savannah.nongnu.org/cgit/attr.git/tree/doc/COPYING) respectively
|
||||||
* [**AtomicParsley**](https://github.com/wez/atomicparsley) - For embedding thumbnail in mp4/m4a if mutagen/ffmpeg cannot. Licensed under [GPLv2+](https://github.com/wez/atomicparsley/blob/master/COPYING)
|
* [**AtomicParsley**](https://github.com/wez/atomicparsley) - For embedding thumbnail in mp4/m4a if mutagen/ffmpeg cannot. Licensed under [GPLv2+](https://github.com/wez/atomicparsley/blob/master/COPYING)
|
||||||
* [**rtmpdump**](http://rtmpdump.mplayerhq.hu) - For downloading `rtmp` streams. ffmpeg will be used as a fallback. Licensed under [GPLv2+](http://rtmpdump.mplayerhq.hu)
|
* [**rtmpdump**](http://rtmpdump.mplayerhq.hu) - For downloading `rtmp` streams. ffmpeg will be used as a fallback. Licensed under [GPLv2+](http://rtmpdump.mplayerhq.hu)
|
||||||
* [**mplayer**](http://mplayerhq.hu/design7/info.html) or [**mpv**](https://mpv.io) - For downloading `rstp` streams. ffmpeg will be used as a fallback. Licensed under [GPLv2+](https://github.com/mpv-player/mpv/blob/master/Copyright)
|
* [**mplayer**](http://mplayerhq.hu/design7/info.html) or [**mpv**](https://mpv.io) - For downloading `rstp` streams. ffmpeg will be used as a fallback. Licensed under [GPLv2+](https://github.com/mpv-player/mpv/blob/master/Copyright)
|
||||||
|
|
|
@ -75,6 +75,15 @@
|
||||||
websockets = None
|
websockets = None
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
import xattr # xattr or pyxattr
|
||||||
|
except ImportError:
|
||||||
|
xattr = None
|
||||||
|
else:
|
||||||
|
if hasattr(xattr, 'set'): # pyxattr
|
||||||
|
xattr._yt_dlp__identifier = 'pyxattr'
|
||||||
|
|
||||||
|
|
||||||
all_dependencies = {k: v for k, v in globals().items() if not k.startswith('_')}
|
all_dependencies = {k: v for k, v in globals().items() if not k.startswith('_')}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1422,7 +1422,7 @@ def _dict_from_options_callback(
|
||||||
dest='parse_metadata', metavar='FIELDS REGEX REPLACE', action='append', nargs=3,
|
dest='parse_metadata', metavar='FIELDS REGEX REPLACE', action='append', nargs=3,
|
||||||
help='Replace text in a metadata field using the given regex. This option can be used multiple times')
|
help='Replace text in a metadata field using the given regex. This option can be used multiple times')
|
||||||
postproc.add_option(
|
postproc.add_option(
|
||||||
'--xattrs',
|
'--xattrs', '--xattr',
|
||||||
action='store_true', dest='xattrs', default=False,
|
action='store_true', dest='xattrs', default=False,
|
||||||
help='Write metadata to the video file\'s xattrs (using dublin core and xdg standards)')
|
help='Write metadata to the video file\'s xattrs (using dublin core and xdg standards)')
|
||||||
postproc.add_option(
|
postproc.add_option(
|
||||||
|
|
|
@ -12,68 +12,52 @@
|
||||||
|
|
||||||
|
|
||||||
class XAttrMetadataPP(PostProcessor):
|
class XAttrMetadataPP(PostProcessor):
|
||||||
#
|
"""Set extended attributes on downloaded file (if xattr support is found)
|
||||||
# More info about extended attributes for media:
|
|
||||||
# http://freedesktop.org/wiki/CommonExtendedAttributes/
|
More info about extended attributes for media:
|
||||||
# http://www.freedesktop.org/wiki/PhreedomDraft/
|
http://freedesktop.org/wiki/CommonExtendedAttributes/
|
||||||
# http://dublincore.org/documents/usageguide/elements.shtml
|
http://www.freedesktop.org/wiki/PhreedomDraft/
|
||||||
#
|
http://dublincore.org/documents/usageguide/elements.shtml
|
||||||
# TODO:
|
|
||||||
# * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
|
TODO:
|
||||||
# * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
|
* capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
|
||||||
#
|
* figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
|
||||||
|
"""
|
||||||
|
|
||||||
|
XATTR_MAPPING = {
|
||||||
|
'user.xdg.referrer.url': 'webpage_url',
|
||||||
|
# 'user.xdg.comment': 'description',
|
||||||
|
'user.dublincore.title': 'title',
|
||||||
|
'user.dublincore.date': 'upload_date',
|
||||||
|
'user.dublincore.description': 'description',
|
||||||
|
'user.dublincore.contributor': 'uploader',
|
||||||
|
'user.dublincore.format': 'format',
|
||||||
|
}
|
||||||
|
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
""" Set extended attributes on downloaded file (if xattr support is found). """
|
mtime = os.stat(info['filepath']).st_mtime
|
||||||
|
|
||||||
# Write the metadata to the file's xattrs
|
|
||||||
self.to_screen('Writing metadata to file\'s xattrs')
|
self.to_screen('Writing metadata to file\'s xattrs')
|
||||||
|
|
||||||
filename = info['filepath']
|
|
||||||
mtime = os.stat(filename).st_mtime
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
xattr_mapping = {
|
for xattrname, infoname in self.XATTR_MAPPING.items():
|
||||||
'user.xdg.referrer.url': 'webpage_url',
|
|
||||||
# 'user.xdg.comment': 'description',
|
|
||||||
'user.dublincore.title': 'title',
|
|
||||||
'user.dublincore.date': 'upload_date',
|
|
||||||
'user.dublincore.description': 'description',
|
|
||||||
'user.dublincore.contributor': 'uploader',
|
|
||||||
'user.dublincore.format': 'format',
|
|
||||||
}
|
|
||||||
|
|
||||||
num_written = 0
|
|
||||||
for xattrname, infoname in xattr_mapping.items():
|
|
||||||
|
|
||||||
value = info.get(infoname)
|
value = info.get(infoname)
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
if infoname == 'upload_date':
|
if infoname == 'upload_date':
|
||||||
value = hyphenate_date(value)
|
value = hyphenate_date(value)
|
||||||
|
write_xattr(info['filepath'], xattrname, value.encode('utf-8'))
|
||||||
byte_value = value.encode('utf-8')
|
|
||||||
write_xattr(filename, xattrname, byte_value)
|
|
||||||
num_written += 1
|
|
||||||
|
|
||||||
except XAttrUnavailableError as e:
|
except XAttrUnavailableError as e:
|
||||||
raise PostProcessingError(str(e))
|
raise PostProcessingError(str(e))
|
||||||
|
|
||||||
except XAttrMetadataError as e:
|
except XAttrMetadataError as e:
|
||||||
if e.reason == 'NO_SPACE':
|
if e.reason == 'NO_SPACE':
|
||||||
self.report_warning(
|
self.report_warning(
|
||||||
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
|
||||||
+ (('Some ' if num_written else '') + 'extended attributes are not written.').capitalize())
|
'Some extended attributes are not written')
|
||||||
elif e.reason == 'VALUE_TOO_LONG':
|
elif e.reason == 'VALUE_TOO_LONG':
|
||||||
self.report_warning(
|
self.report_warning('Unable to write extended attributes due to too long values.')
|
||||||
'Unable to write extended attributes due to too long values.')
|
|
||||||
else:
|
else:
|
||||||
msg = 'This filesystem doesn\'t support extended attributes. '
|
tip = ('You need to use NTFS' if compat_os_name == 'nt'
|
||||||
if compat_os_name == 'nt':
|
else 'You may have to enable them in your "/etc/fstab"')
|
||||||
msg += 'You need to use NTFS.'
|
raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')
|
||||||
else:
|
|
||||||
msg += '(You may have to enable them in your /etc/fstab)'
|
|
||||||
raise PostProcessingError(str(e))
|
|
||||||
|
|
||||||
self.try_utime(filename, mtime, mtime)
|
self.try_utime(info['filepath'], mtime, mtime)
|
||||||
return [], info
|
return [], info
|
||||||
|
|
111
yt_dlp/utils.py
111
yt_dlp/utils.py
|
@ -4673,87 +4673,56 @@ def _get_pixel(idx):
|
||||||
|
|
||||||
|
|
||||||
def write_xattr(path, key, value):
|
def write_xattr(path, key, value):
|
||||||
# This mess below finds the best xattr tool for the job
|
# Windows: Write xattrs to NTFS Alternate Data Streams:
|
||||||
try:
|
# http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
|
||||||
# try the pyxattr module...
|
if compat_os_name == 'nt':
|
||||||
import xattr
|
assert ':' not in key
|
||||||
|
assert os.path.exists(path)
|
||||||
|
|
||||||
if hasattr(xattr, 'set'): # pyxattr
|
try:
|
||||||
# Unicode arguments are not supported in python-pyxattr until
|
with open(f'{path}:{key}', 'wb') as f:
|
||||||
# version 0.5.0
|
f.write(value)
|
||||||
# See https://github.com/ytdl-org/youtube-dl/issues/5498
|
except OSError as e:
|
||||||
pyxattr_required_version = '0.5.0'
|
raise XAttrMetadataError(e.errno, e.strerror)
|
||||||
if version_tuple(xattr.__version__) < version_tuple(pyxattr_required_version):
|
return
|
||||||
# TODO: fallback to CLI tools
|
|
||||||
raise XAttrUnavailableError(
|
|
||||||
'python-pyxattr is detected but is too old. '
|
|
||||||
'yt-dlp requires %s or above while your version is %s. '
|
|
||||||
'Falling back to other xattr implementations' % (
|
|
||||||
pyxattr_required_version, xattr.__version__))
|
|
||||||
|
|
||||||
|
# UNIX Method 1. Use xattrs/pyxattrs modules
|
||||||
|
from .dependencies import xattr
|
||||||
|
|
||||||
|
setxattr = None
|
||||||
|
if getattr(xattr, '_yt_dlp__identifier', None) == 'pyxattr':
|
||||||
|
# Unicode arguments are not supported in pyxattr until version 0.5.0
|
||||||
|
# See https://github.com/ytdl-org/youtube-dl/issues/5498
|
||||||
|
if version_tuple(xattr.__version__) >= (0, 5, 0):
|
||||||
setxattr = xattr.set
|
setxattr = xattr.set
|
||||||
else: # xattr
|
elif xattr:
|
||||||
setxattr = xattr.setxattr
|
setxattr = xattr.setxattr
|
||||||
|
|
||||||
|
if setxattr:
|
||||||
try:
|
try:
|
||||||
setxattr(path, key, value)
|
setxattr(path, key, value)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise XAttrMetadataError(e.errno, e.strerror)
|
raise XAttrMetadataError(e.errno, e.strerror)
|
||||||
|
return
|
||||||
|
|
||||||
except ImportError:
|
# UNIX Method 2. Use setfattr/xattr executables
|
||||||
if compat_os_name == 'nt':
|
exe = ('setfattr' if check_executable('setfattr', ['--version'])
|
||||||
# Write xattrs to NTFS Alternate Data Streams:
|
else 'xattr' if check_executable('xattr', ['-h']) else None)
|
||||||
# http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
|
if not exe:
|
||||||
assert ':' not in key
|
raise XAttrUnavailableError(
|
||||||
assert os.path.exists(path)
|
'Couldn\'t find a tool to set the xattrs. Install either the python "xattr" or "pyxattr" modules or the '
|
||||||
|
+ ('"xattr" binary' if sys.platform != 'linux' else 'GNU "attr" package (which contains the "setfattr" tool)'))
|
||||||
|
|
||||||
ads_fn = path + ':' + key
|
value = value.decode('utf-8')
|
||||||
try:
|
try:
|
||||||
with open(ads_fn, 'wb') as f:
|
p = Popen(
|
||||||
f.write(value)
|
[exe, '-w', key, value, path] if exe == 'xattr' else [exe, '-n', key, '-v', value, path],
|
||||||
except OSError as e:
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||||
raise XAttrMetadataError(e.errno, e.strerror)
|
except OSError as e:
|
||||||
else:
|
raise XAttrMetadataError(e.errno, e.strerror)
|
||||||
user_has_setfattr = check_executable('setfattr', ['--version'])
|
stderr = p.communicate_or_kill()[1].decode('utf-8', 'replace')
|
||||||
user_has_xattr = check_executable('xattr', ['-h'])
|
if p.returncode:
|
||||||
|
raise XAttrMetadataError(p.returncode, stderr)
|
||||||
if user_has_setfattr or user_has_xattr:
|
|
||||||
|
|
||||||
value = value.decode('utf-8')
|
|
||||||
if user_has_setfattr:
|
|
||||||
executable = 'setfattr'
|
|
||||||
opts = ['-n', key, '-v', value]
|
|
||||||
elif user_has_xattr:
|
|
||||||
executable = 'xattr'
|
|
||||||
opts = ['-w', key, value]
|
|
||||||
|
|
||||||
cmd = ([encodeFilename(executable, True)]
|
|
||||||
+ [encodeArgument(o) for o in opts]
|
|
||||||
+ [encodeFilename(path, True)])
|
|
||||||
|
|
||||||
try:
|
|
||||||
p = Popen(
|
|
||||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
|
||||||
except OSError as e:
|
|
||||||
raise XAttrMetadataError(e.errno, e.strerror)
|
|
||||||
stdout, stderr = p.communicate_or_kill()
|
|
||||||
stderr = stderr.decode('utf-8', 'replace')
|
|
||||||
if p.returncode != 0:
|
|
||||||
raise XAttrMetadataError(p.returncode, stderr)
|
|
||||||
|
|
||||||
else:
|
|
||||||
# On Unix, and can't find pyxattr, setfattr, or xattr.
|
|
||||||
if sys.platform.startswith('linux'):
|
|
||||||
raise XAttrUnavailableError(
|
|
||||||
"Couldn't find a tool to set the xattrs. "
|
|
||||||
"Install either the python 'pyxattr' or 'xattr' "
|
|
||||||
"modules, or the GNU 'attr' package "
|
|
||||||
"(which contains the 'setfattr' tool).")
|
|
||||||
else:
|
|
||||||
raise XAttrUnavailableError(
|
|
||||||
"Couldn't find a tool to set the xattrs. "
|
|
||||||
"Install either the python 'xattr' module, "
|
|
||||||
"or the 'xattr' binary.")
|
|
||||||
|
|
||||||
|
|
||||||
def random_birthday(year_field, month_field, day_field):
|
def random_birthday(year_field, month_field, day_field):
|
||||||
|
|
Loading…
Reference in a new issue