Search code examples
iosobjective-cjsonimagempmovieplayercontroller

How do I set .mp4 image


I'm trying to set the background image of an mp4, but there doesn't seem to be any solutions that work. How do I set the image of it from the NSData I have?

Data is setup like this:

// Basically the JSON data gives a url to a .mp4
NSArray *preview = [JSON valueForKeyPath:@"results.previewUrl"];
_previewData = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:[preview objectAtIndex:0]]];

Solution

  • To be clear, you're not setting "the background image" of an audio file. You're editing the metadata of it (thanks for clarifying in the comments).

    You can use a library such as MP4Audio to do this without having to write the logic yourself:

    To set the metadata for an audio file:

    ANMovie* file = [[ANMovie alloc] initWithFile:@"test.m4a"]; // or .mp4
    NSData* jpegCover = UIImageJPEGRepresentation(image);
    ANMetadata* metadata = [[ANMetadata alloc] init];
    metadata.albumCover = [[ANMetadataImage alloc] initWithImageData:jpegCover type:ANMetadataImageTypeJPG];
    [file setMovieMetadata:metadata];
    [file close];