Changing a files metadata in Python

2019-02-15 00:45发布

问题:

Ok, I've searched around here but haven't found anything pointing to a solid answer.

I'm trying to change a files artist, filename, rating, genre etc in windows, which shows up when you view folders in 'details'.

At the moment I have the files I wish to edit in a list and I am iterating through them, but as I said I am not sure how to change it for each file in the list.

def Files(The_FileList):
'''Changes each files metadata'''
for each_file in The_FileList:

    #clueless here.

return The_FileList

needs to work with .avi/.mkv general movie files as I do alot of encoding.

I'm looking for a simple option as this is all I want to do.

Thanks

回答1:

In many cases (and in this case), metadata is file-type specific. (Some filesystems offer their own metadata, as NTFS and later do, but this particular metadata is coming from the file itself, and not from the filesystem).

To modify the metadata in the files in question, you probably can use the Mutagen library (assuming these are mp3/aac/flac/vorbis/etc. - there are probably other audio formats that have a different metadata format).



回答2:

Mutagen is actualized.

I leave an example for change 3 atributes of all the files in the directory:

import mutagen
from mutagen.mp4 import MP4
from os import scandir

ruta = './'
l_archivos = sorted([archivo.name for archivo in scandir(ruta) if archivo.is_file()])

mutagen.File(l_archivos[1])      # U: See the tags of the data

def edit_Media_Data():

    for f in range(len(l_archivos[:-1])):                 # A: A range of all the fields exept the script
        file = MP4(l_archivos[f])                         # A: Capture the file to edit
        file['©nam'] = l_archivos[f].replace('.mp4','')   # U: Take the file name and makeit the tittle
        file['©ART'] = 'Hector_Costa_Guzman'              # U: Edit the Autor
        file['©alb'] = 'Curso_Django'                     # U: Edit the Album
        file.pprint()
        file.save()