Pascal Bajorat
Pascal Bajorat

Reputation: 484

iTunes MP4 Tags / Metadata with Python3 and Mutagen

I'm currently using the Mutagen module for Python to prepare the MP4 Tags of a video-file for iTunes. It works fine, but I miss one really important tag, it's called "stik" and stands for the iTunes media type.

This is my current code:

mp4_video_tags = MP4(mp4_file)

mp4_video_tags['\xa9nam'] = 'Video Name'
mp4_video_tags['\xa9alb'] = 'DVD-Name'
mp4_video_tags['\xa9gen'] = 'Video-Training'
mp4_video_tags['\xa9day'] = '2015'
mp4_video_tags['\xa9ART'] = 'Company'
mp4_video_tags['aART'] = 'Company'
mp4_video_tags['\xa9wrt'] = 'Company'
mp4_video_tags['cprt'] = 'Copyright (c) Company'
mp4_video_tags['desc'] = 'description'
mp4_video_tags['tvsh'] = 'DVD-Name'
mp4_video_tags['trkn'] = [(1, 18)]
mp4_video_tags['disk'] = [(1, 1)]
mp4_video_tags['stik'] = 10

mp4_video_tags.save()

That code works really fine, but it crashes at "mp4_video_tags['stik'] = 10", because the value of this tag can't be an integer. But according to this list: https://code.google.com/p/mp4v2/wiki/iTunesMetadata#Media_Type_%28stik%29

it must be an integer with the value of 10 for a TV Show.

I've noticed that Mutagen does not show the "stik" tag in their tag list / docs, maybe it's not supported by default: https://mutagen.readthedocs.org/en/latest/api/mp4.html

Could anyone explain me how I can set the MP4 Tag "stik" to 10 for TV Shows with Mutagen?

Upvotes: 3

Views: 4104

Answers (3)

James Snook
James Snook

Reputation: 4093

I've been having fun with this in python3 where the suggested answer doesn't appear to work. However, the following does:

mp4_tags["stik"] = [10]

I checked after setting this with exiftool and it appears to read the Media Type metadata as the expected type.

Upvotes: 2

Ben Rolleman
Ben Rolleman

Reputation: 66

I know I'm a little late to the party, but I have a gift:

mp4_tags['stik'] = str(unichr(10))

I haven't tried it but I think you can also use mp4_tags['stik'] = '\n' as well as the newline character is represented by the binary number 10.

The problem is iTunes reads the field as a binary number, you can save more or less anything you want to it. for example I tried setting mp4_tags['tvsn'] = '2' and I loaded the file into iTunes it was displayed as Season 50 which if you look on a ASCII table is how the character 2 is represented.

Seems to be a bug in the library.

Upvotes: 5

kpie
kpie

Reputation: 11120

If it cannot be an integer have you tried '10' with the quotation marks?

If that don’t work it is worth noting that python's integer object is absolutely nuts and never rolls over... it is not your run of the mill 8 bit integer.

You can use ctypes to cast to an 8 bit integer as seen here Convert a string to an 8-bit signed integer in python

from ctypes import cast, pointer, POINTER, c_char, c_int
def convert(c):
    return cast(pointer(c_char(c)), POINTER(c_int)).contents.value

Upvotes: 0

Related Questions