How to add track number metadata for an MP4 file using Mutagen?

Viewed 223

Every other tag seems to work e.g. title, cover art, artist but I encounter an error for the track number tag [trkn] every time

import mutagen
from mutagen import MP4, MP4Cover

filePath = 'LoadFiles/UserUpload/test.mp4'
mp4_meta = MP4(filePath)

print("Enter the following")
title = input("Title: ")
mp4_meta['\xa9nam'] = title #works

trackno = input("Track No.: ")
mp4_meta['trkn'] = trackno #nope

mp4_meta.save()

In the mutagen documentation it states:

Tuples of ints (multiple values per key are supported):

‘trkn’ – track number, total tracks

What's the fix to this?

2 Answers

You need to provide a tuple for this field, including the total number of tracks.

Try the following:

trackno = input("Track No: ")
totaltracks = input("Total No of Tracks: ")
mp4_meta['trkn'] = (trackno, totaltracks)

You actually need to provide an iterable of tuples. Same as lys' answer except for the last line (where a list containing a single tuple of 2 ints is set as the trkn value):

trackno = input("Track No: ")
totaltracks = input("Total No of Tracks: ")
mp4_meta['trkn'] = [(trackno, totaltracks)]
Related