Search code examples
pythonwindowsmp4pywin32

python3 - Changing values of file details on Windows (pywin32)


I am trying to change values of certain details for mp4 files on Windows. I'm trying to use pywin32 for this. If we right click a file > Properties > Details, we see some extra file details such as Title, Subtitle, Rating etc (as seen in the picture): enter image description here

Through some looking-around, I've found that this can be done using pywin32. Below the script that I currently have:

import pythoncom, os
from win32com.propsys import propsys
from win32com.shell import shellcon

pk = propsys.PSGetPropertyKeyFromName("System.Title")

ps = propsys.SHGetPropertyStoreFromParsingName(os.path.abspath('file.mp4'))

details = ps.GetValue(pk).GetValue()
print(details)

newValue = propsys.PROPVARIANTType('ok')
ps.SetValue(pk, newValue)
ps.Commit()

This is using System.Title from the Win32 api. It prints the details correctly, but when I try to change the value, it throws the following error:

pywintypes.com_error: (-2147287035, 'Access Denied.', None, None)

Any help is appreciated.


Solution

  • I am currently facing the same problem. After reading the source code, I have finally found the solution.

    Upon reading the documentation for the C++ version, I discovered that the function SHGetPropertyStoreFromParsingName takes an argument called Flags. You can refer to the documentation here: SHGetPropertyStoreFromParsingName c++

    If you want to read and edit, you should create a ps object like this:

    ps = propsys.SHGetPropertyStoreFromParsingName(destinationpath, None, 2)
    

    Here, 2 represents GPS_READWRITE as mentioned in the documentation for GetPropertystoreFlags.

    However, in the propsys.pyi documentation, the order of the arguments is incorrect. It shows that "Flags" appears as the second argument, when in reality, it should be the third argument.