]>
jfr.im git - yt-dlp.git/blob - youtube_dl/postprocessor/xattrpp.py
1 from __future__
import unicode_literals
7 from .common
import PostProcessor
9 subprocess_check_output
18 class XAttrMetadataPP(PostProcessor
):
21 # More info about extended attributes for media:
22 # http://freedesktop.org/wiki/CommonExtendedAttributes/
23 # http://www.freedesktop.org/wiki/PhreedomDraft/
24 # http://dublincore.org/documents/usageguide/elements.shtml
27 # * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
28 # * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
32 """ Set extended attributes on downloaded file (if xattr support is found). """
34 # This mess below finds the best xattr tool for the job and creates a
35 # "write_xattr" function.
37 # try the pyxattr module...
40 # Unicode arguments are not supported in python-pyxattr until
42 # See https://github.com/rg3/youtube-dl/issues/5498
43 pyxattr_required_version
= '0.5.0'
44 if version_tuple(xattr
.__version
__) < version_tuple(pyxattr_required_version
):
45 self
._downloader
.report_warning(
46 'python-pyxattr is detected but is too old. '
47 'youtube-dl requires %s or above while your version is %s. '
48 'Falling back to other xattr implementations' % (
49 pyxattr_required_version
, xattr
.__version
__))
53 def write_xattr(path
, key
, value
):
54 return xattr
.setxattr(path
, key
, value
)
58 # Write xattrs to NTFS Alternate Data Streams:
59 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
60 def write_xattr(path
, key
, value
):
62 assert os
.path
.exists(path
)
64 ads_fn
= path
+ ":" + key
65 with open(ads_fn
, "wb") as f
:
68 user_has_setfattr
= check_executable("setfattr", ['--version'])
69 user_has_xattr
= check_executable("xattr", ['-h'])
71 if user_has_setfattr
or user_has_xattr
:
73 def write_xattr(path
, key
, value
):
75 cmd
= ['setfattr', '-n', key
, '-v', value
, path
]
77 cmd
= ['xattr', '-w', key
, value
, path
]
79 subprocess_check_output(cmd
)
82 # On Unix, and can't find pyxattr, setfattr, or xattr.
83 if sys
.platform
.startswith('linux'):
84 self
._downloader
.report_error(
85 "Couldn't find a tool to set the xattrs. "
86 "Install either the python 'pyxattr' or 'xattr' "
87 "modules, or the GNU 'attr' package "
88 "(which contains the 'setfattr' tool).")
90 self
._downloader
.report_error(
91 "Couldn't find a tool to set the xattrs. "
92 "Install either the python 'xattr' module, "
93 "or the 'xattr' binary.")
95 # Write the metadata to the file's xattrs
96 self
._downloader
.to_screen('[metadata] Writing metadata to file\'s xattrs')
98 filename
= info
['filepath']
102 'user.xdg.referrer.url': 'webpage_url',
103 # 'user.xdg.comment': 'description',
104 'user.dublincore.title': 'title',
105 'user.dublincore.date': 'upload_date',
106 'user.dublincore.description': 'description',
107 'user.dublincore.contributor': 'uploader',
108 'user.dublincore.format': 'format',
111 for xattrname
, infoname
in xattr_mapping
.items():
113 value
= info
.get(infoname
)
116 if infoname
== "upload_date":
117 value
= hyphenate_date(value
)
119 byte_value
= value
.encode('utf-8')
120 write_xattr(filename
, xattrname
, byte_value
)
124 except (subprocess
.CalledProcessError
, OSError):
125 self
._downloader
.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")